Ask Ubuntu is a question and answer site for Ubuntu users and developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Here's the deal: I use the lastfm package to listen my Last.FM (duh)... And I want to show some notify-osd notifications when a new music starts.

As long the program don't supports it, I want to create a shell script that will monitor the window title of the lastfm player and send a notification when it changes.

What do you think? How can I start?

share|improve this question
up vote 4 down vote accepted

You can check the title of an open window using wmctrl:

$ wmctrl -lx | grep "" | grep -Po "(?<=$HOSTNAME ).*$"
Insomnium – Death Walked the Earth

Watching the title using this method would require the script to periodically poll for changes, though, which would be messy and inefficient.

A smarter approach would be to wait for some kind of event generated by the player. A D-Bus interface or similar API would be ideal, but I don't see any documentation suggesting one exists. As a next-best alternative, you can try watching the terminal output for information on what the player is doing:



while read line; do
    if [ $grabnextline = 1 ]; then
        title="$(echo "$line" | cut -d\" -f2)"
        wget -O "$albumart" "$(grep -A 10 "<title>$(echo "$title" | awk -F ' – ' '{print $2}')" ~/.local/share/ | grep -Eo "http://.*\.(jpg|png)" | sed 's/174s/64s/' | sed 's/jpg/png/')"
        notify-send "$title" --icon "$albumart"
    if [[ "$line" = *ScrobblerManager::nowPlaying* ]]; then
done < <( /usr/bin/lastfm 2>&1 )

rm "$albumart"


It's still an ugly hack, but it gets the job done.

share|improve this answer
Great approach! Thank you! :) – TiuTalk Mar 6 '11 at 4:13

Which player do you use for . Many application use dbus to notify events. You would need to catch them in script.

Like following page has a example on how to use catch Rhythmbox song change.

Trace Rhythmbox track change using dbus

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.