Web and Native Technologies

Spotify and Applescript make one hell of a team

I was recently creating a sample jukebox CLI where I can play music through the terminal. It was right there and then that it hit me! Why don’t I use the Spotify API? I immediately took it a step further and tried to use the spotify API to get the hottest tracks as JSON data, parse it and play it on the spotify mac desktop application! Everything was all going perfectly until I ran into some unexpected problems. I was only able to open up the web player using the JSON data. My original plan was to be able to use the desktop application, so I knew I had to go a little deeper. I had to mix my knowledge of ruby with some shell commands to be able to run shell instructions through my ruby program. Instead of relying on the JSON parse data, I went for a little more manual approach. I learned a little of applescript while I was at it, so I can use it to create a simple script that will let me talk to the applications on my local computer. In order to do this, I first had to see if Spotify was scriptable, and luckily it was. After exploring the hidden contents of the app, I was able to find a dictionary file with all the instructions that I could use with apple script. Using this data, I was able to create a simple script that let me play the song on the desktop client as well as pause and play the given song at any given time.

The little script I used to play the song was:

osascript -e 'tell application "Spotify" to play track "track_url_goes_here"'

To pause a playing song:

osascript -e 'tell application "Spotify" to pause'

And of course, to resume it:

osascript -e 'tell application "Spotify" to play'

Running these scripts from the ruby program is as easy as:

def resume_song
  puts `osascript -e 'tell application "Spotify" to play'`
end

As you can see, interacting with the computer is super easy if you are using applescript. Using resume_song as an example, you can easily see how to incorporate that logic into playing music on your desktop client app or even getting fancier and authenticating the user in order to get their profile information.

A simple example of how you would put all this together:

def parse_and_play
  # Parse JSON data here and get the result url in 'track_url'
  puts `osascript -e 'tell application "Spotify" to play track "#{track_url}"'`
end

The backticks are essential as that is how you can interact with your shell inside of a ruby program. You can also use %x{script goes here}

Ultimately, I was able to successfully open up spotify on my computer, and play any song from the top 200 most streamed songs from the spotify charts. This is, if I do say so myself, one of the coolest things I have ever done with ruby.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s