Skip to content

Instantly share code, notes, and snippets.

@ramnes
Created June 9, 2015 09:52
Show Gist options
  • Save ramnes/ccd4ef2b8a0981c08f92 to your computer and use it in GitHub Desktop.
Save ramnes/ccd4ef2b8a0981c08f92 to your computer and use it in GitHub Desktop.
#!/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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment