Remote Control SpotifyShare
“I don’t think necessity is the mother of invention. Invention, in my opinion, arises directly from idleness, possibly laziness, to save myself trouble.”
In Murphy’s continuing efforts to avoid leaving the sofa he’s using an iPhone to kick off Spotify on the Mac upstairs and have it stream over Airfoil to an Apple TV connected to a stereo downstairs. It works, it’s pretty simple, and once it’s started there are other options for controlling it.
The key is that Spotify recently added some basic AppleScript support to their Mac app. Not as much as we’d like to see, but enough to get us started. Spotify says they’re just experimenting with AppleScript at this point. (There were workarounds before Spotify added support) We’ll be watching for further enhancements.
This short and simple AppleScript (view entire script) is all you need to get started. It launches Airfoil (an application for redirecting audio from your Mac to remote speakers via Airport Express or Apple TV) and selects Spotify as the audio source application. Then it selects a playlist in Spotify and initiates playback. That’s it. Let’s take a look at the script.
The lines in the first block set Spotify as the application Airfoil will pull audio from. As long as you’ve got Spotify in your Applications folder you can copy those lines exactly. The line in the next block references your speaker id, which you probably don’t know. You can get the id by running a very simple script that looks like this. That line will query your remote speaker for its id and display it in the results pane of the Applescript Editor. Be sure to use the name of your Apple TV or Airport Express in the quotes. Once you’ve got the id you shouldn’t need to run that script again.
The script pauses to make sure your Mac doesn’t get ahead of the launching applications. Spotify hasn’t provided playlist selection hooks for Applescript yet, but they do provide something almost as good. In the Spotify application you can right-click a playlist or track and get an identifier, just like you see in the screenshot at the top of the post. You want to select Copy Spotify URI. Notice the open location line in the script, it’s not inside the Spotify tell – end tell section because it’s not in the Spotify Applescript dictionary. Regardless, it works to select a playlist and that’s all we really need. The last part of the script tells Spotify to start playing.
So – now we just need the script to run. Murphy is using a Mail rule. He sends himself an email with a subject Mail is watching for. Mail runs the script whenever a message with that subject comes in. Once Spotify is up and running the Speakers app that comes with Airfoil can be used to pause or skip to the next track.
I’d rather have an application on my iPhone, maybe something like TextExpander, that could list Applescript TextExpander snippets on my Mac and kick them off that way. But I don’t know of an app like that. The email kick-off works fine, and it’s quick and easy to use, but at times I might have to use another app to wake the Mac up.
In the past I’ve used Mail to run Applescripts on my Mac. It started with putting my Mac to sleep by email but I’ve also used the strategy for kicking off iTunes (before the Remote app) and for file retrieval.
If you’re looking for more information on Applescript and Airfoil they’ve got some examples on their site.
Interesting: Initially I made a script that opened a Spotify playlist and didn’t include the line to commence playback. It stopped working after a day, I don’t know why. It would play the one track and then stop. The day before it jumped to whatever playlist had been set as the current playlist in the Spotify application and continued playback. Not sure what happened there.