Skip to content

Instantly share code, notes, and snippets.

@bencevans
Created June 16, 2013 19:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bencevans/5793122 to your computer and use it in GitHub Desktop.
Save bencevans/5793122 to your computer and use it in GitHub Desktop.
Tomahawk (DBUS) CLI
#!/bin/bash
# Credit to azzid for Spotify version - http://ubuntuforums.org/showthread.php?t=1797848
# 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 tomahawk_cmd
{
dbus-send --print-reply --dest=org.mpris.MediaPlayer2.tomahawk /org/mpris/MediaPlayer2 org.mpris.MediaPlayer2.Player.$1 1> /dev/null
}
function tomahawk_query
{
qdbus org.mpris.MediaPlayer2.tomahawk /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|<tomahawk 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` tomahawk
fi
case "$1" in
play)
tomahawk_cmd Play
;;
pause)
tomahawk_cmd Pause
;;
playpause)
tomahawk_cmd PlayPause
;;
next)
tomahawk_cmd Next
;;
previous)
tomahawk_cmd Previous
;;
stop)
tomahawk_cmd Stop
;;
tomahawk:user:*)
tomahawk_cmd "OpenUri string:$1"
tomahawk_cmd Play
;;
tomahawk:*:*)
tomahawk_cmd "OpenUri string:$1"
;;
playstatus)
tomahawk_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