diff --git a/README.md b/README.md index 39b704f..76e536e 100644 --- a/README.md +++ b/README.md @@ -50,4 +50,5 @@ This comes with absolutely no guarantee of support or correct function, although * `yearbox.html` - DokuWiki-type yearbox prototype for Minoteaur (I think this actually contains an off-by-one error somewhere; it isn't what's actually in use). * `arbitrary-politics-graphs` - all you need to run your own election campaign. * `heavbiome` - some work on biome generation with Perlin noise. -* `block_scope.py` - Python uses function scoping rather than block scoping. Some dislike this. I made a decorator to switch to block scoping. \ No newline at end of file +* `block_scope.py` - Python uses function scoping rather than block scoping. Some dislike this. I made a decorator to switch to block scoping. +* `mpris_smart_toggle.py` - playerctl play-pause sometimes does not play or pause the media I want played or paused (it seems to use some arbitrary selection order). This does it somewhat better by tracking the last thing which was playing. \ No newline at end of file diff --git a/mpris_smart_toggle.py b/mpris_smart_toggle.py new file mode 100755 index 0000000..f6b6db2 --- /dev/null +++ b/mpris_smart_toggle.py @@ -0,0 +1,25 @@ +#!/usr/bin/env python +from mpris2 import get_players_uri, Player +from os.path import expanduser +savepath = expanduser("~/.local/share/last_media.txt") +players = list(get_players_uri()) +last = None +try: + with open(savepath) as f: + last = f.read() +except: + pass + +players = list(get_players_uri()) +for player_uri in players: + player = Player(dbus_interface_info={"dbus_uri": player_uri}) + if player.PlaybackStatus == "Playing": + player.Pause() + with open(savepath, "w") as f: + f.write(player_uri) + break +else: + if last in players: + Player(dbus_interface_info={"dbus_uri": last}).Play() + else: + player.Play() \ No newline at end of file