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

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

Play Previous

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

Play/Pause

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

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

18 thoughts on “Controlling Spotify through Applescript & Quicksilver.

  1. Jack Post author

    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.

  2. Peter Dickson

    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

  3. Tom Long

    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.

  4. Jack Post author

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

  5. Paul

    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…

  6. Parker

    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!

  7. Tim Jones

    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

  8. Ivan

    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

  9. Peck

    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

  10. Pingback: Spotify Applescripts – Updated

  11. Pingback: Apple Mac and iPhone Apps for Spotify | Pansentient League - a Spotify Blog

  12. Jho

    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)

  13. Pingback: stev.ie/ » Blog Archive

  14. Pingback: stev.ie/ » Blog Archive

Leave a Reply