Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

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

2 Answers 2

up vote 4 down vote accepted

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

$ wmctrl -lx | grep "last.fm.Last.fm" | 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 Last.fm 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:

#!/bin/bash

grabnextline=0
albumart="$(mktemp)"

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/Last.fm/Last.fm.log | grep -Eo "http://.*\.(jpg|png)" | sed 's/174s/64s/' | sed 's/jpg/png/')"
        notify-send "$title" --icon "$albumart"
        grabnextline=0
    fi
    if [[ "$line" = *ScrobblerManager::nowPlaying* ]]; then
        grabnextline=1
    fi
done < <( /usr/bin/lastfm 2>&1 )

rm "$albumart"

exit

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 list.fm . 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

 
discard

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.