Open a Spotify URI with AppleScript

Picture the scene: you receive a Spotify URI from a friend. It looks something like spotify:track:69GFzoDcAFrW6CD5tBEMTK and your computer doesn’t linkify it. So to discover the music, you’ve got to select all, copy to clipboard, switch to the Spotify app, focus the search box, paste and hit ENTER. Well not any more you don’t.

Here’s an AppleScript that when configured in Automator will allow you to play a Spotify URI by simply selecting it and choosing an option from the context menu (assuming you have a Mac):

Set up

  1. Open Automator and create a new Service
  2. Drag a Run AppleScript action from the list to the right-hand pane
  3. Ensure the dropdowns above are configured so that “Service receives selected text in any application”
  4. Paste in the script (see below)
  5. Save it with a nice name, e.g. “Spotify URI”
  6. Check that it’s visible on the Services sub-menu of the context menu (invoked from anywhere)

The script

Here’s everything that needs to be in the action in Automator:

on run {input, parameters}
	-- As this is run via Automator with "service receives text in any application"
	-- we don't need to do anything to copy from the clipboard.
	set clipped to trimEnd(input as text)
	-- Figure out if the text is long enough to be a Spotify URI, and then if
	-- whether it looks like one, and if so, tell Spotify to play it.
	set validuri to false
	if length of clipped > 24 then
		set prtcl to characters 1 thru 24 of clipped as string
		if (prtcl is "") then
			set validuri to true
			set prtcl to characters 1 thru 8 of clipped as string
			if (prtcl is "spotify:") then
				set validuri to true
			end if
		end if
	end if
	-- If we're good to go, go!
	-- Thanks to
	if validuri is true then
		tell application "Spotify" to play track clipped
	end if
	return input
end run
-- Remove cruft from the end of the selected text.
-- Thanks to
on trimEnd(str)
	local str, whiteSpace
		set str to str as string
		set whiteSpace to {character id 10, return, space, tab}
			repeat while str's last character is in whiteSpace
				set str to str's text 1 thru -2
			end repeat
			return str
		on error number -1728
			return ""
		end try
	on error eMsg number eNum
		error "Can't trimEnd: " & eMsg number eNum
	end try
end trimEnd

And this is what it does:

  1. Retrieve as input whatever text is highlighted when the service runs
  2. Trim any whitespace from the end (the trimEnd() subroutine is kindly taken from Brati’s Lover)
  3. If the input qualifies as either type of Spotify URI, set a flag
  4. If the flag is true, send the input to Spotify’s play track method (thanks to Jack)

How to use

  1. Highlight a Spotify URI or HTTP Link
  2. Right-click on the selected text to show the context menu
  3. Drill down to Services > Spotify URI (or whatever you called it)
  4. The music begins!


A nice side-effect of this exercise is that it works for both spotify: Spotify URIs and HTTP Links. Applications such as Skype automatically linkify HTTP URLs and clicking them opens the web player – irritating if you’re using the desktop application. Now, the same workflow (highlight, context menu, run script) will work for both.

So far I’ve tested with links to tracks, albums and playlists and they all work just fine.

This was done on Mac OS X 10.9.4.
Here’s a zip of the Automator file:

Leave a Reply

Your email address will not be published. Required fields are marked *


You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">