bashdbusrhythmbox

How to continuously monitor rhythmbox for track change using bash


I'd like to do the same thing as is described here, but using shell scripting (preferably in bash) instead of python. It seems like such a thing should be possible using dbus-monitor, but I'm not very familiar with dbus, and it's not clear to me how to take the concepts described in the solution to the python question and apply them to the dbus-monitor tool.


Solution

  • Here's the simplest way I could find:

    #!/bin/bash
    
    interface=org.gnome.Rhythmbox.Player
    member=playingUriChanged
    
    # listen for playingUriChanged DBus events,
    # each time we enter the loop, we just got an event
    # so handle the event, e.g. by printing the artist and title
    # see rhythmbox-client --print-playing-format for more output options
    
    dbus-monitor --profile "interface='$interface',member='$member'" |
    while read -r line; do
        printf "Now playing: "
        rhythmbox-client --print-playing
    done
    

    It produces output like this:

    Now playing: Daft Punk - Overture
    Now playing: Daft Punk - The Grid
    

    It also prints the currently playing song when started up. If that is not what you want, look at the contents of $line and see if it contains NameAcquired or playingUriChanged. If it contains NameAcquired, skip it.

    The main difference between the Python version and this bash version is that the Python version uses DBus to get the playing song information. I couldn't find a good way to do that using bash, but rhythmbox-client --print-playing seems to work well.