Skip to content

Instantly share code, notes, and snippets.

@tormath1
Last active May 4, 2023 14:43
Show Gist options
  • Save tormath1/5ab9bbdcd0af8346084a34f1cf1b6e0e to your computer and use it in GitHub Desktop.
Save tormath1/5ab9bbdcd0af8346084a34f1cf1b6e0e to your computer and use it in GitHub Desktop.
control spotify from firefox through mpris on sway
#!/usr/bin/env python
# goal of this script is to control (play / pause) multimedia played
# on a firefox tab through dbus
# requirements:
# * dbus (session)
import dbus
import sys
if len(sys.argv) < 2:
print(f"usage: {sys.argv[0]} play-pause|next|previous")
exit(1)
action = sys.argv[1]
# used to identify a Dbus firefox instance
CHROMIUM_INSTANCE_PATTERN = "org.mpris.MediaPlayer2.firefox"
session = dbus.SessionBus()
# we first need to find out the "instance" of firefox we want to control
dbus = session.get_object(
"org.freedesktop.DBus",
"/org/freedesktop/DBus",
)
names = dbus.ListNames()
mpris= None
for name in names:
if not CHROMIUM_INSTANCE_PATTERN in name:
continue
mpris = session.get_object(
name,
"/org/mpris/MediaPlayer2",
)
dbus_interface = "org.mpris.MediaPlayer2.Player"
if mpris:
match action:
case "play-pause":
mpris.PlayPause(dbus_interface=dbus_interface)
case "previous":
mpris.Previous(dbus_interface=dbus_interface)
case "next":
mpris.Next(dbus_interface=dbus_interface)
bindsym XF86AudioPlay exec ~/.scripts/mpris-wrapper.py play-pause
bindsym XF86AudioNext exec ~/.scripts/mpris-wrapper.py next
bindsym XF86AudioPrev exec ~/.scripts/mpris-wrapper.py previous
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment