Facebook Twitter Gplus Google Maps RSS

TinyURL

If you have not checked out SmileOnMyMac‘s Text Expander, you really should. Whether you enable just the TidBITS AutoCorrect Dictionary for TextExpander Snippetor you use it to manage email signatures, common phrases, corrections, instruction blurbs or more. One of the cool features is a snippet from David Smalley to putTinyURL capabilities at your fingertips. While this is a great start, his script is too involved to make it fast enough to replace the text and let you carry on with your typing.

I’ve created two options, both with a faster, sleeker call to TinyURL’s core API and a little error checking to save you from pasting in a huge XML error. Open up TextExpander and add a new Snippet, select Applescript, then paste one of the two following options in. Use the abbreviation tinyurl. Close TextExpander, copy a URL to the clipboard and type tinyurl. Voila!

Option 1: Error checking and a kill dialog

set the ClipURL to (the clipboard as string)

set q to display dialog ClipURL with title ¬
	"TinyURL Link" buttons {"TinyURL", "Wrong URL"} ¬
	default button 2 giving up after 2

ignoring case
	if button returned of q is "Wrong URL" or ¬
		((characters 1 through 4 of ClipURL as string) is not "http") then
		return "Malformed URL."
	else
		set curlCMD to ¬
			"curl --stderr /dev/null "http://tinyurl.com/api-create.php?url=" & ClipURL & """

		-- Run the script and get the result:
		set tinyURL to (do shell script curlCMD)

		return tinyURL
	end if
end ignoring

Option 2: Just Error checking

set the ClipURL to (the clipboard as string)

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://tinyurl.com/api-create.php?url=" & ClipURL & """

		-- Run the script and get the result:
		set tinyURL to (do shell script curlCMD)

		return tinyURL
	end if
end ignoring