Spotify shortcut on linux

Using dbus to map shortcuts to Spotify

June 6, 2014

I’m using Opensuse with KDE, and I love this environnement. Also, I love to listen to my music on Spotify using the linux (beta) client, but I didn’t knew how to define shortcuts to control the app. It’s actually really easy to achieve using dbus.

Ok, so first: What is dbus? This is the answer from the official site:

D-Bus is a message bus system, a simple way for applications to talk to one another.[...]

Hum, it sounds great. Now, I don’t want to dive into details in this article. I just want a simple shortcut to my spotify app. How can I do? Let’s see if Spotify let us call the app through dbus.

I use qdbus to explore my apps. Or even better, qdbusviewer which gives me an UI on top of qdbus. In the search field, I enter “spotify” and find a package name “com.spotify.qt”.

Spotify in qdbusviewer

Spotify in qdbusviewer

At this point, you will have to expore the possibilities in the right panel. And we can test it directly! If you expand org.freedesktop.MediaPlayer2, you will find a list of available methods. Double-click on the one you want to test, and let the magic happen. Using Method: PlayPause will toggle your music for example.

Exposed methods in qdbusviewer

Exposed methods in qdbusviewer

Now, we just have to map it with kde shortcuts. In system settings > shortcut > custom, you can add a new global custom shortcut, using dbus command. There, in trigger, you define your shortcut keys, and in action its effect. In RemoteApplications, specify the package name, in Remote object, the object at top-level of the method you want to use from qdbusviewer, and finally, in Function, type the method you want to use. You can see the parrallel between qdbusviewer and these settings in the two images below.

PlayPause method, on object org.mrpis.MediaPlayer2

PlayPause method, on object org.mrpis.MediaPlayer2

KDE shortcut settings

KDE shortcut settings

That’s it!

NB: I hesitated to write this post on this blog. Making a choice on what to publish is hard: should I write on a specific subject, or anything? Should I write only on things that is kind of new, or is it good to write something that everyone can find easily on web? Finally, I made a choice regarding these questions: I consider this blog as a logbook.