github email
Controlling Lollypop from the command line
Oct 28, 2017
2 minutes read

If you are a Linux user and a music collection nerd, you’ll find that you don’t have that many choices, more so since the days of good old Amarok 1.x.

One of the applications to handle your collection (and not just play it) is Lollypop (yes, nice site!).

Though it defines some shortcuts, they are only available when the main window has focus. So to control it from your window manager you need to dig into it’s dbus implementation.

To see what methods it provides, just run:

dbus-send --print-reply --type=method_call --dest='org.gnome.Lollypop' /org/mpris/MediaPlayer2 org.freedesktop.DBus.Introspectable.Introspect

(You can use whatever dbus client you like of course).

For a shortcut to Play/Pause, just bind the following to a key combination:

dbus-send --print-reply --type=method_call --dest='org.gnome.Lollypop' /org/mpris/MediaPlayer2 org.mpris.MediaPlayer2.Player.PlayPause

There is a also a method to call up and get song data (metadata):

dbus-send --print-reply --type=method_call --dest='org.gnome.Lollypop' /org/mpris/MediaPlayer2 org.freedesktop.DBus.Properties.Get string:'org.mpris.MediaPlayer2.Player' string:'Metadata'

You can use the above in a simple python script to get a readable Now Playing output:

#!/usr/bin/python3

import dbus
session_bus = dbus.SessionBus()
lollypop_bus = session_bus.get_object("org.gnome.Lollypop","/org/mpris/MediaPlayer2")
lollypop_properties = dbus.Interface(lollypop_bus,"org.freedesktop.DBus.Properties")
metadata = lollypop_properties.Get("org.mpris.MediaPlayer2.Player", "Metadata")

title = metadata['xesam:title']
artist = metadata['xesam:albumArtist'][0]

# Some radio stations only have artist and a dot for title
if title == ".": 
    print('{0}'.format(artist))
else:
    print('{0} by {1}'.format(title, artist))

(Replace albumArtist with Artist if your collection’s tags don’t fit).


Back to posts