Quickly Extract a Shortened URL for Anything on the iTunes App Store

app_store.pngI download and try lots of apps from the App Store. When I like one enough that I want to broadcast my find to the world, my first stop is Twitter.

Transforming the long and complicated URL from the App Store into an acceptable Twitter form, however, is a somewhat convoluted process. First open up a plain text TextEdit window and drag the app’s icon from iTunes onto the document. Next, open up bit.ly (or your favorite URL shortening service) in Safari, paste in the URL open in TextEdit to shorten it. Copy your new short link to the clipboard and you’re set to post to Twitter. (And then you can close the bit.ly page and quit TextEdit without saving…)

This process is prohibitively cumbersome. I cobbled together the following AppleScript to ease the pain of posting shortened App Store URLs to Twitter.

on open appURL
	tell application "Finder"
		set ClipURL to location of internet location file (appURL as text)
		ignoring case
			if ((characters 1 through 4 of ClipURL as string) is not "http") then
				return "Malformed URL."
			else
				set curlCMD to ¬
					"curl --stderr /dev/null \"http://bit.ly/api?url=" & ClipURL & "\""

				set bitlyURL to (do shell script curlCMD)

				set the clipboard to bitlyURL
				move appURL to the trash

				tell application "GrowlHelperApp"
					set defaultNotification to "URL Shortened"
					set myAllNotesList to {defaultNotification}
					register as application defaultNotification all notifications myAllNotesList ¬
						default notifications {defaultNotification} icon of application "Safari.app"
					notify with name defaultNotification title ¬
						defaultNotification description ¬
						"Your bit.ly URL is on the clipboard and ready to paste." application name defaultNotification

				end tell

			end if
		end ignoring
	end tell
end open

How to Use the Script

To set up the script, first copy the code into Script Editor and save it as an Application somewhere you’ll have easy access to it (the Desktop is a good place). Once your script is saved, using it is easy. Drag your link from iTunes out onto the desktop. Now drag it one more time onto the droplet. The URL will be shortened and put on the clipboard and the webloc from iTunes will be moved to the trash.

It’s really useful, although a little difficult to explain. This short video illustrates what the script does.

I created this script using two sources. AppleScript droplets cannot handle URLs dropped straight from Safari (or iTunes) so it’s necessary to drop them into the Finder first. To get the droplet to recognize the .webloc file I used the code posted here by an unknown forum member. To convert the URL using bit.ly, I used Jonathan Berger’s TextExpander script.

If you know a better way to get App Store URLs, let me know in the comments. For the moment, I’m quite happy with this method.