Controlling Spotify through Applescript & Quicksilver.

Update Available: http://www.jacktams.co.uk/2009/10/09/spotify-applescripts-updated/

picture-2I have almost solely being listen to music through spotify for the last couple of weeks, sure it doesn't replace iTunes but its damn good at what it does, all you can eat music for free. http://www.spotify.com/

The Problem:
Spotify can use the built in shortcuts for play/pause, forward and reverse, but if like me you use a different keyboard and effectively 'dock' you Macbook its not much use. Enter Quicksilver, the perennial quick-launch and whiz-kid short-cutter for mac. http://bit.ly/yrlr7

I already have a load of triggers set up within quicksilver for everything from make an event in iCal to post a tweet or send email.

So getting Spotify in there, it turns out Applescript is here to help, using the System Events helper you can select a menu item without actually clicking it. Below is the code for the main actions you would want in Spotify. Simply save the apple script, then attach it to a trigger in Quicksilver. Hey Presto, magic! For more details on setting up triggers check the Quicksilver wiki, it explains it alot better than I ever could. http://bit.ly/134dnL

Play Next

tell application "Spotify" to activate
tell application "System Events"
   tell process "Spotify"
      click menu item 3 of menu 1 of menu bar item 5 of menu bar 1
   end tell
end tell

Play Previous

tell application "Spotify" to activate
tell application "System Events"
   tell process "Spotify"
      click menu item 4 of menu 1 of menu bar item 5 of menu bar 1
   end tell
end tell

Play/Pause

tell application "Spotify" to activate
tell application "System Events"
   tell process "Spotify"
      click menu item 1 of menu 1 of menu bar item 5 of menu bar 1
   end tell
end tell

Notes:
Don't forget to activate assitive device support see http://www.macspeech.com/extensions/faq/kb.php?article=48

18 Comments to “Controlling Spotify through Applescript & Quicksilver.”

  1. seventy6 21 May 2009 at 4:45 pm #

    cheers. been looking for something like this for ages. perfect!

  2. stubee 16 June 2009 at 10:16 pm #

    Any luck with a search function? Too much to ask?

  3. Jack 17 June 2009 at 9:51 pm #

    I don’t think search is possible, because the app isn’t in cocoa. The only reason the above stuff works is because you can access an menus of an app from Applescript. But not all the interface is available to you.

  4. Peter Dickson 20 June 2009 at 12:01 pm #

    Hey Jack!

    I’ve recently bought my first Mac… a MacBook!

    Found Quicksilver and Spotify and have followed your instructions, with no joy. :(

    I had to create ~/Library/Application Support/Quicksilver/Actions folder. Is that right? It contains two files now… PlayNextSpotify.scpt and PlayPauseSpotify.scpt. I’ve relaunched QS and attached the Actions to triggers.

    If you can help, thanks!

    Great blog, btw.

    cheers

    Pete

  5. Tom Long 29 June 2009 at 7:59 pm #

    Peter Dickson, the problem might not be related to Quicksilver. For this scriot to work, you need to have a feature called ‘Access for assistive devices’ enabled. You can turn it on with the checkbox at the bottom of the Universal Access pane of System Preferences.

  6. Jack 29 June 2009 at 9:39 pm #

    Oops, I completely forgot about the assistive devices thing. Yes, that would explain why the script doesn’t work.

  7. Paul 21 July 2009 at 10:04 am #

    Hi Jack, nice tip! Would this work with the “Extra Scripts” quicksilver plugin? That way I wouldn’t have to make a ton of trigger shortcuts, which I’d have to remember…

  8. Parker 29 July 2009 at 5:48 pm #

    Hi all,

    I’ve gotten the scripts to work with keyboard shortcuts via quicksilver, but I’m each time I use them Spotify becomes the active application. I’m new to applescript, but I was wondering if there was a command that would reset the application after running the script.

    Thanks!

  9. Tim Jones 14 August 2009 at 5:20 pm #

    Parker,

    Try storing the name of the active application before activating spotify thus:

    tell application “System Events” to set appList to ¬
    name of application processes whose frontmost is true

    set activeApp to item 1 of appList

    tell application “Spotify” to activate
    tell application “System Events”
    tell process “Spotify”
    click menu item 1 of menu 1 of menu bar item 5 of menu bar 1
    end tell
    end tell

    tell application activeApp to activate

    Regards,

    Tim

  10. Ivan 21 August 2009 at 6:06 pm #

    Hi!
    Very good scripts.
    I have the same keys for iTunes and Spotify, so when I’m using iTunes, and I press F8 (forward), Spotify starts even if it wasn’t opened. That’s is very annoying, so I search for a simple applescript command.
    If Spotify is opened, then go to the next song in Spotify.
    That I have get it work with the following Script:

    tell application “System Events”
    set MyList to (name of every process)
    end tell
    if (MyList contains “Spotify”) is true then
    tell application “Spotify” to activate
    tell application “System Events”
    tell process “Spotify”
    click menu item 3 of menu 1 of menu bar item 5 of menu bar 1
    end tell
    end tell
    end if

    Is for forward, but can be right for the other scripts.

    I hope this helps

  11. Peck 11 September 2009 at 2:15 am #

    Thanks for the script and all the additions. I combined tim’s and ivan’s script to make this which should be easier for newbies at applescript (like me!) to just copy and paste.

    tell application “System Events”
    set MyList to (name of every process)
    end tell

    tell application “System Events” to set appList to ¬
    name of application processes whose frontmost is true

    set activeApp to item 1 of appList

    if (MyList contains “Spotify”) is true then
    tell application “Spotify” to activate
    tell application “System Events”
    tell process “Spotify”
    click menu item 1 of menu 1 of menu bar item 5 of menu bar 1
    end tell
    end tell
    tell application “System Events”
    set visible of process “Spotify” to false
    end tell
    end if

    tell application activeApp to activate

  12. Blind Rocket 27 September 2009 at 3:59 pm #

    I’ve written a tip how to search on Spotify with Quicksilver:
    http://blindrocket.blogspot.com/2009/09/searching-spotify-with-quicksilver.html

  13. [...] refresh is needed. Sometime ago a released several little Applescript helpers that let you control spotify through Quicksilver triggers. However they had a little tendency to make spotify the front and therefore active app when you [...]

  14. [...] Spotify Controller using Applescript & Quicksilver – Jack Tams has put together a short tutorial on how to use Applescript and Quicksilver to trigger shortcuts to control Spotify. This is handy when use a different keyboard or dock you Macbook. [...]

  15. Jho 3 May 2010 at 10:50 am #

    After the spotify update the menus have changed slightly and the currect menu bar item is now 6 (For Play/Pause : click menu item 1 of menu 1 of menu bar item 6 of menu bar 1)

  16. Jack 27 May 2010 at 2:28 pm #

    Updated Scripts Available here: http://www.jacktams.co.uk/2010/04/28/spotify-applescripts-version-0-4-3/

  17. [...] Via. [...]

  18. [...] Via. [...]


Leave a Reply