-- Invoke Spotify simply by opening a Spotify playlist or artist URL
open location "http://open.spotify.com/artist/4Zml6rzsSiVkfIIWJh6EWX"
-- Make sure we have the spotify-application on front and in focus
tell application "Spotify"
activate
end tell
-- Now navigate to the song we want to play and play it
tell application "System Events"
-- Three times tabulator = move selection to the songs list
key code 48
key code 48
key code 48
-- Wait for a while the spotify app to catch up
delay (2)
-- enter key = play selected song
key code 36
-- right key with command = next song (random assumed)
key code 124 using command down
end tell
Other assumptions:
- "enable access to assistive devices" is enabled on the "Universal Access" -settings
- Spotify URL:s properly associated to your favourite browser (tested only with firefox)
- Invoking a random song requires you to set the shuffle on manually on the Spotify app
- Naturally this generates new browser tabs on each time the script is invoked
- As the key events are used, and even with a noticeable wait in between, this can cause really weird results if something is happening onscreen either interactively or autonomously.
- There is no straightforward way of knowing if Spotify is already playing something. Toggling the playback off, if invoked second time (useful when used with RFID tags) isn't therefore easy.. or is there a apparent solution other than closing the app?
Again, the script is not really a rocket science and most of the elements were just copy&pasted from invaluable Doug's AppleScripts. It is just fun to simply being able to control other apps in OSX!