Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spotify + AppleScript: Add current track to playlist

I've found some scripts how to control Spotify through AppleScript. But they only include things like play/pause, next, previous, stop. I was searching for some references on Spotify for AppleScript, but I didn't find any.

Is it possible to add the current playing track in Spotify to a specific playlist with AppleScript, and then, assign a system wide shortcut for that script?

like image 800
23tux Avatar asked Oct 01 '12 14:10

23tux


2 Answers

This actually is possible, provided you have an Internet connection.

Although the Spotify AppleScript API doesn't natively support adding songs to a playlist, you can call their Web API (which does support this feature) from your script.

It would look something like this:

do shell script "curl -i -X POST 'https://api.spotify.com/v1/users/{USER_NAME}/playlists/" & playlist_id & "/tracks?uris=" & track_id & "' -H 'Authorization: Bearer " & oauth_id & "' -H 'Accept: application/json'"

where

  1. playlist_id is the ID from the URL for your playlist (whether it is public or private) in the format https://open.spotify.com/user/{USER_NAME}/playlist/playlist_id
  2. track_id is the Spotify URI for a song (which you can get using the AppleScript API: set track_id to id of the current track, for example)
  3. oauth_id is the authorization key you get from OAuth when you register and authorize your application (this is the tricky one); also remember to request private playlist modification scope when you authorize if you want to be able to use this with private playlists

As for the universal shortcut, I created a systemwide service in Automator that simply ran my AppleScript and assigned it to a shortcut in System Preferences.

like image 197
AstroCB Avatar answered Nov 15 '22 05:11

AstroCB


1) Every single scriptable app is inherently documented. In AppleScript Editor, choose Open Dictionary… from the File menu and choose the app.

2) No, you can't modify playlists using Spotify's scripting interface.

like image 25
iKenndac Avatar answered Nov 15 '22 05:11

iKenndac