Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Linux bash script to control MOC or any MPRIS-enabled player. The script should be placed somewhere in the $PATH (~/bin; /usr/local/bin etc), and a few keyboard shortcuts added in your WM or via xbindkeys (if used). See help for available actions. If several players are running at the same time, MOC is always given priority, and then the MPRIS p…
#!/bin/bash
case $1 in
"help"|"-h"|"?"|"-?")
echo -e "Usage:
$(basename $0) help
show this help
$(basename $0) up
volume up by 5%
$(basename $0) down
volume down by 5%
$(basename $0) next
next track
$(basename $0) prev
previous track
$(basename $0) play
play current / selected track
$(basename $0) playfirst
play first track in the current playlist (MOC only)
$(basename $0) pause
pause current track
$(basename $0) ppause
toggle pause
$(basename $0) stop
stop current track
$(basename $0) sf
seek forward by 5 seconds
$(basename $0) sb
seek back by 5 seconds
$(basename $0) raise
show player (new x-terminal-emulator window/tab for MOC)
$(basename $0) quit
quit the player (shutdown the server for MOC)
"
exit 0
;;
esac
if [ "$(pgrep mocp)" ];
then
player=mocp
else
player=$(mdbus2 -p |grep org.mpris.MediaPlayer|sort -k 2|head -1|cut -d\ -f1|cut -c24-)
fi
mpris_run() {
VOL=$(dbus-send --print-reply --dest=org.mpris.MediaPlayer2.$player /org/mpris/MediaPlayer2 org.freedesktop.DBus.Properties.Get string:'org.mpris.MediaPlayer2.Player' string:'Volume'|tail -1|cut -c25-28)
case $1 in
up)
dbus-send --print-reply --dest=org.mpris.MediaPlayer2.$player /org/mpris/MediaPlayer2 org.freedesktop.DBus.Properties.Set string:'org.mpris.MediaPlayer2.Player' string:'Volume' variant:double:$(echo "$VOL + 0.1"|bc)
;;
down)
dbus-send --print-reply --dest=org.mpris.MediaPlayer2.$player /org/mpris/MediaPlayer2 org.freedesktop.DBus.Properties.Set string:'org.mpris.MediaPlayer2.Player' string:'Volume' variant:double:$(echo "$VOL - 0.1"|bc)
;;
next)
dbus-send --print-reply --dest=org.mpris.MediaPlayer2.$player /org/mpris/MediaPlayer2 org.mpris.MediaPlayer2.Player.Next
;;
prev)
dbus-send --print-reply --dest=org.mpris.MediaPlayer2.$player /org/mpris/MediaPlayer2 org.mpris.MediaPlayer2.Player.Previous
;;
play)
dbus-send --print-reply --dest=org.mpris.MediaPlayer2.$player /org/mpris/MediaPlayer2 org.mpris.MediaPlayer2.Player.Play
;;
pause)
dbus-send --print-reply --dest=org.mpris.MediaPlayer2.$player /org/mpris/MediaPlayer2 org.mpris.MediaPlayer2.Player.Pause
;;
ppause)
dbus-send --print-reply --dest=org.mpris.MediaPlayer2.$player /org/mpris/MediaPlayer2 org.mpris.MediaPlayer2.Player.PlayPause
;;
stop)
dbus-send --print-reply --dest=org.mpris.MediaPlayer2.$player /org/mpris/MediaPlayer2 org.mpris.MediaPlayer2.Player.Stop
;;
sf)
dbus-send --print-reply --dest=org.mpris.MediaPlayer2.$player /org/mpris/MediaPlayer2 org.mpris.MediaPlayer2.Player.Seek int64:5000000
;;
sb)
dbus-send --print-reply --dest=org.mpris.MediaPlayer2.$player /org/mpris/MediaPlayer2 org.mpris.MediaPlayer2.Player.Seek int64:-5000000
;;
raise)
dbus-send --print-reply=literal --session --reply-timeout=500 --dest=org.mpris.MediaPlayer2.$player /org/mpris/MediaPlayer2 org.mpris.MediaPlayer2.Raise
;;
quit)
dbus-send --print-reply=literal --session --reply-timeout=500 --dest=org.mpris.MediaPlayer2.$player /org/mpris/MediaPlayer2 org.mpris.MediaPlayer2.Quit
;;
esac
}
moc_run() {
case $1 in
up)
mocp -v +5
;;
down)
mocp -v -5
;;
next)
mocp -f
;;
prev)
mocp -r
;;
play)
if [ "$(mocp -i)" == "State: STOP" ]
then mocp -p
else mocp -U
fi
;;
playfirst)
mocp -p
;;
pause)
mocp -P
;;
ppause)
mocp -G
;;
stop)
mocp -j 0s && mocp -P
;;
sf)
mocp -k 5
;;
sb)
mocp -k -5
;;
raise)
x-terminal-emulator -e mocp
;;
quit)
mocp -x
;;
esac
}
if [ $player == mocp ];
then
moc_run $1
else
mpris_run $1
fi
exit 0
@kosivantsov

This comment has been minimized.

Copy link
Owner Author

commented Nov 3, 2018

Linux bash script to provide global shortcut control for MOC or any MPRIS-enabled player.
The script should be placed somewhere in the $PATH (~/bin; /usr/local/bin etc).
Then a few keyboard shortcuts could be added in your WM or via xbindkeys (if used). See help for available actions.
If several players are running at the same time, MOC is always given priority, and then the MPRIS player which was launched the earliest. Each time the script is run, it checks for running players and sends the command to the one that matches the above criterium.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.