I wrote a script some time ago to be able to control the mediaplayer spotify from a script. I've found it really useful, for example when I wanted to map my keyboard media keys to work with spotify.
I thought someone else would appreciate the script aswell, so here it is:
Enjoy! And if you do like it, please drop a line here so that I'll know about it.Code:#!/bin/bash # Collect DBUS_SESSION_BUS_ADDRESS from running process function set_dbus_adress { USER=$1 PROCESS=$2 PID=`pgrep -o -u $USER $PROCESS` ENVIRON=/proc/$PID/environ if [ -e $ENVIRON ] then export `grep -z DBUS_SESSION_BUS_ADDRESS $ENVIRON` else echo "Unable to set DBUS_SESSION_BUS_ADDRESS." exit 1 fi } function spotify_cmd { dbus-send --print-reply --dest=org.mpris.MediaPlayer2.spotify /org/mpris/MediaPlayer2 org.mpris.MediaPlayer2.Player.$1 1> /dev/null } function spotify_query { qdbus org.mpris.MediaPlayer2.spotify /org/mpris/MediaPlayer2 org.freedesktop.DBus.Properties.Get org.mpris.MediaPlayer2.Player PlaybackStatus } function quit_message { echo "Usage: `basename $0` {play|pause|playpause|next|previous|stop|playstatus|<spotify URI>}" exit 1 } # Count arguments, must be 1 if [ "$#" -ne "1" ] then echo -e "You must supply exactly one argument!\n" quit_message fi # Check if DBUS_SESSION is set if [ -z $DBUS_SESSION_BUS_ADDRESS ] then #echo "DBUS_SESSION_BUS_ADDRESS not set. Guessing." set_dbus_adress `whoami` spotify fi case "$1" in play) spotify_cmd Play ;; pause) spotify_cmd Pause ;; playpause) spotify_cmd PlayPause ;; next) spotify_cmd Next ;; previous) spotify_cmd Previous ;; stop) spotify_cmd Stop ;; spotify:user:*) spotify_cmd "OpenUri string:$1" spotify_cmd Play ;; spotify:*:*) spotify_cmd "OpenUri string:$1" ;; playstatus) spotify_query ;; *) echo -e "Bad argument.\n" quit_message ;; esac exit 0
Edit/FYI: They (spotify developers) changed something with the latest release breaking the 'playstatus' command, but hopefully they fix it with the next release.



Adv Reply



Bookmarks