How To Shorten URLs Anywhere With Automator

It’s become common practice to use services such as to shorten URLs, but this is usually done using a bookmarklet script, which only works in web browsers. This Automator (s aapl) service allows you (on OS X 10.6 Snow Leopard) to select a URL in any application and shorten it using the API.

First of all, you’ll need to open up Automator (Applications > Automator) and create a new Service by double-clicking the Service icon. If you don’t see this option when Automator starts, go to File > New and the new workflow screen will show.

At the top of the right-hand panel, there are two drop-down lists. Change the first to say URLs and leave the second on Any Application. Leave the Replaces selected text box unchecked.

Now, in the left-hand column, under the Utilities category, find Run Applescript and drag it over into the workflow on the right.

In the right-hand panel, replace the placeholder text in the Run Applescript action by copying and pasting the following script:

on run {input}
set inputResult to (input as string)
ignoring case

if (((characters 1 thru 4 of inputResult) as string) is not equal to "http") then
display dialog "Invalid URL"

set curlCMD to "curl --stderr /dev/null \"" & input & "\""
set tinyURL to (do shell script curlCMD)
return tinyURL

end if
end ignoring
end run

Next thing to do is add a Copy to Clipboard action from the Utilities category underneath the Applescript action. This grabs the output from the script and copies it to the clipboard ready for you to paste wherever you want.

Finally, you need to save your Service so that you can access it in other applications. Call it something sensible like “Shorten URL” or “Send to” so that it’s obvious what goes on when you run it.

Once you’ve saved it, you can test it out by selecting a URL, right- or control-clicking and choosing the name of your service (in my case, it was “Shorten URL”).

Once you’ve activated the Service, wait a couple of seconds while the script contacts the API, then paste into your browser. You’ll see that a URL has been created, and if you follow it, you’ll be taken to the original page whose URL you originally selected.

If right-clicking isn’t your thing, and you prefer using the keyboard, you can go to System Preferences > Keyboard > Keyboard Shortcuts > Services and set up a keyboard shortcut. Now when you have text selected, just use that keyboard shortcut and you’ll get your short URL.

Related content from GigaOM Pro (sub req’d):