citadel

My dotfiles, scripts and nix configs
git clone git://jb55.com/citadel
Log | Files | Refs | README | LICENSE

spotify-playpause (1026B)


      1 #!/usr/bin/env bash
      2 
      3 set -eou pipefail
      4 
      5 playstatus() {
      6 	dbus-send --print-reply --dest="$1" /org/mpris/MediaPlayer2 org.freedesktop.DBus.Properties.Get string:org.mpris.MediaPlayer2.Player string:PlaybackStatus | sed -En 's,.*string "([^"]+)".*,\1,p'
      7 }
      8 
      9 playpause() {
     10 	dbus-send --print-reply --dest="$1" /org/mpris/MediaPlayer2 org.mpris.MediaPlayer2.Player.PlayPause
     11 }
     12 
     13 bus=$(dbus-send \
     14       --print-reply=literal \
     15       --dest=org.freedesktop.DBus \
     16       --type=method_call \
     17       /org/freedesktop/DBus \
     18       org.freedesktop.DBus.ListNames |
     19       tr ' ' '\n' |
     20       grep 'org.mpris.MediaPlayer2' |
     21       grep -v -e qutebrowser -e chromium
     22     )
     23 
     24 count=$(wc -l <<<"$bus")
     25 
     26 if [ $count -eq 1 ]; then
     27 	playpause "$bus"
     28 	exit 0
     29 fi
     30 
     31 # auto-detect any source that is playing and then pause that
     32 for b in $bus
     33 do
     34 	if [ "$(playstatus "$b")" = "Playing" ]; then
     35 		playpause "$b"
     36 		exit 0
     37 	fi
     38 done
     39 
     40 bus=$(<<<"$bus" sed 's,^org.mpris.MediaPlayer2.,,g' | dmenu -i -p "playpause" -l 7)
     41 
     42 playpause "org.mpris.MediaPlayer2.$bus"