Skip to content

Instantly share code, notes, and snippets.

@tanelpuhu
Last active December 16, 2015 16:19
Show Gist options
  • Save tanelpuhu/5461979 to your computer and use it in GitHub Desktop.
Save tanelpuhu/5461979 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
from dbus.mainloop.glib import DBusGMainLoop
import subprocess
import gobject
import dbus
appName = 'Spotify Media Keys'
def handleMediaKey(app, *keys):
if app != appName:
return
method = None
for key in keys:
if key == 'Play':
method = 'PlayPause'
elif key in ['Stop', 'Next', 'Previous']:
method = key
if method:
subprocess.call([
'dbus-send',
'--print-reply',
'--dest=org.mpris.MediaPlayer2.spotify',
'/org/mpris/MediaPlayer2',
'org.mpris.MediaPlayer2.Player.' + method
])
if __name__ == '__main__':
DBusGMainLoop(set_as_default=True)
bus = dbus.Bus(dbus.Bus.TYPE_SESSION)
mediaKeys = bus.get_object('org.gnome.SettingsDaemon', '/org/gnome/SettingsDaemon/MediaKeys')
mediaKeys.GrabMediaPlayerKeys(appName, 0, dbus_interface='org.gnome.SettingsDaemon.MediaKeys')
mediaKeys.connect_to_signal('MediaPlayerKeyPressed', handleMediaKey)
gobject.MainLoop().run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment