Page 1 of 3 123 LastLast
Results 1 to 10 of 21

Thread: A bash script to control spotify for linux via dbus

  1. #1
    Join Date
    Apr 2007
    Location
    linköping, sweden
    Beans
    119
    Distro
    Ubuntu 12.04 Precise Pangolin

    A bash script to control spotify for linux via dbus

    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:

    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
    Enjoy! And if you do like it, please drop a line here so that I'll know about it.

    Edit/FYI: They (spotify developers) changed something with the latest release breaking the 'playstatus' command, but hopefully they fix it with the next release.
    Attached Files Attached Files
    Last edited by azzid; October 12th, 2011 at 02:47 PM.

  2. #2
    Join Date
    Feb 2009
    Location
    Finland
    Beans
    2
    Distro
    Xubuntu 8.10 Intrepid Ibex

    Talking Re: A bash script to control spotify for linux via dbus

    I like it... a lot

    Now I can easily change songs from my Thinkpad's with Fn and arrow keys. Thanks!
    Attached Images Attached Images

  3. #3
    Join Date
    Nov 2007
    Beans
    8

    Re: A bash script to control spotify for linux via dbus

    Just what I was looking for; works perfectly. Thanks!!

  4. #4
    Join Date
    Mar 2010
    Location
    Cranfield, UK
    Beans
    37
    Distro
    Ubuntu 10.10 Maverick Meerkat

    Re: A bash script to control spotify for linux via dbus

    Sorry to bother but I don't have much experience in this.
    Do I just execute the script?
    Do I have to edit the script according to the multimedia keys of my keyboard? In which part?

    Thanks.

  5. #5
    Join Date
    Apr 2007
    Location
    linköping, sweden
    Beans
    119
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: A bash script to control spotify for linux via dbus

    Quote Originally Posted by repunante View Post
    Sorry to bother but I don't have much experience in this.
    Do I just execute the script?
    Do I have to edit the script according to the multimedia keys of my keyboard? In which part?

    Thanks.
    You can try the script in a bash terminal, you'll have to supply an argument to describe if you want the script to press 'play', 'pause' etc.

    When you understand how to use the script you can use the keyboard shortcut setting GUI in the menu of ubuntu to map different keys to different executions of the script.

    You should not have to alter the script, just supply it with different arguments.

  6. #6
    Join Date
    Mar 2010
    Location
    Cranfield, UK
    Beans
    37
    Distro
    Ubuntu 10.10 Maverick Meerkat

    Re: A bash script to control spotify for linux via dbus

    Quote Originally Posted by azzid View Post
    You can try the script in a bash terminal, you'll have to supply an argument to describe if you want the script to press 'play', 'pause' etc.

    When you understand how to use the script you can use the keyboard shortcut setting GUI in the menu of ubuntu to map different keys to different executions of the script.

    You should not have to alter the script, just supply it with different arguments.

    I don't have a clue about writing/editing scripts, I will make some research on my own and I will try to make it work.
    Thanks for the help.

  7. #7
    Join Date
    Dec 2006
    Beans
    13
    Distro
    Ubuntu

    Re: A bash script to control spotify for linux via dbus


  8. #8
    Join Date
    Mar 2010
    Location
    Cranfield, UK
    Beans
    37
    Distro
    Ubuntu 10.10 Maverick Meerkat

    Re: A bash script to control spotify for linux via dbus

    Quote Originally Posted by nuclear_eclipse View Post

    Thanks!

  9. #9
    Join Date
    Apr 2011
    Beans
    2
    Distro
    Ubuntu 11.04 Natty Narwhal

    Re: A bash script to control spotify for linux via dbus

    Thanks alot!!

    Quote Originally Posted by jachy View Post
    I like it... a lot

    Now I can easily change songs from my Thinkpad's with Fn and arrow keys. Thanks!
    Amen!!

  10. #10
    Join Date
    Oct 2007
    Location
    Denmark
    Beans
    30
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: A bash script to control spotify for linux via dbus

    Wow! Thanks alot, just what I've been searching for!

Page 1 of 3 123 LastLast

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •