Spotify Applescripts – 0.5.2

Its been a while since I updated the Applescripts for Spotify, it looks like Spotify now supports Applescript directly. So I present the new updated scripts here. Credit Aaron Lidman who first brought this to my attention.

Play Pause

tell application "System Events"
	set MyList to (name of every process)
end tell
if (MyList contains "Spotify") is true then
	tell application "Spotify" to playpause
end if

Next Track

tell application "System Events"
	set MyList to (name of every process)
end tell
if (MyList contains "Spotify") is true then
	tell application "Spotify" to next track
end if

Previous Track

tell application "System Events"
	set MyList to (name of every process)
end tell
if (MyList contains "Spotify") is true then
	tell application "Spotify" to previous track
end if

24 Replies to “Spotify Applescripts – 0.5.2”

  1. Just what I was looking for, thanks! If you plan on updating these anymore, you might just want to create a github/bitbucket repo with the scripts in it. I followed links in number of blog posts to get to the latest version, having them all point to a repo with the latest would make it easy.

  2. Thank you so much for this! I recently upgraded to a mechanical keyboard but it lacked the multimedia keys. So using these in conjunction with an application that lets me use function keys to run applescripts, now the lost functionality is restored! πŸ˜€

  3. Thank you for such wonderful scripts! Spotify integrates seamlessly with the mac. AppleScript + QuickSilver helps make easy shortcuts. I am an avid Last.fm user. Scrobbling works beautifully with Spotify. The client UI is well thought out. And I have been using Spotify only for 2 days. I will be buying the monthly subscription very soon!

  4. Huge thanks for this, almost went back to using Itunes just because I couldn’t control spotify through Quicksilver. Scripts work great.

  5. Hello there! I came here searching for, well, exactly what your post contains. Thanks for sharing the knowledge!

    Still, I have a question: Is there any way to speed up the execution of the script, when it is fired through a keyboard shortcut as a Service?

    I had already created a script similar to yours, but it lacked the search if Spotify is running. When I fired it through a keyboard shortcut sometimes it took it about 6-7 seconds to execute. I added your “check if running” code, but it doesn’t seem to improve things very much..

    Any possible explanation? Thanks in advance, I’m fairly new to applescripting πŸ™‚

  6. @modacular I don’t think theres a way of speeding up, theres two bottlenecks that you have using the scripts. The script engine starting and the script actually being run.

    Because the scripts effectively simulate your cursor, I can’t see a way of speeding it up much.

    I have found that under my usage it gets quicker the more you use the scripts but thats more a byproduct of the way HFS+ moves files around the disk to make things faster, rather than anything proactive.

  7. Thanks for your reply!

    As an addition to your last sentence, I’ve noticed that once the script is fired for the first time, if you invoke it once again afterwards, it gets executed almost immediately.

    Although I am generally covered by the current situation (the “latency” that is), out of plain curiosity I would like to explore what happens when, for example, the play/pause keyboard button is pressed, in order to understand why the command is executed immediately.

    (In the same context, I’ve also noticed that in the Beatport Pro application, in which there is a dedicated preference section for assigning hot keys, your custom shortcuts are executed immediately, regardless of which application is in front.)

    So, can you point me to some source online that will provide better understanding of what happens underneath when a shortcut is pressed? πŸ™‚

  8. When you a looking at more “native” type apps you are able to register a piece of code that listens for a specific key-code of combinations of code, that code is than executed more or less immediately.

    The spotify applescripts, are using a interpreted/scripting language that is triggered by a key-combination. It doesn’t really listen, quicksilver in this case is just executing a script when it picks up the key combination. So with the scripts you have the listener for the keycode but rather than that listener interacting with spotify directly, it interacts with the script telling it to run – thus you get latency.

    You will find most of this documented at either developer.apple.com or https://developer.apple.com/library/mac/#documentation/AppleScript/Conceptual/AppleScriptX/AppleScriptX.html

    https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/EventOverview/HandlingKeyEvents/HandlingKeyEvents.html

  9. The scripts work fine when I click play in the AppleScript Editor, however when I create a trigger with a shortcut, they don’t work, any idea what’s wrong? I had the same shortcuts before with another trigger and they were working fine.

  10. Rather than toggling play/pause, you can substitute the following to explicitly pause:
    tell application “Spotify” to pause
    And use the following to explicitly play:
    tell application “Spotify” to play

    Furthermore, you can also set the volume level, if desired:
    set volume output volume 0

    Very nice!

  11. Correction:
    To explicitly play, use the following:
    tell application “Spotify”
    if player state is paused then play
    end tell

Leave a Reply