10.6: A New Service to Shorten URLs

Dec 30, '10 07:30:00AM

Contributed by: renaultssoftware

Snow Leopard only hintI submitted a hint a while ago, regarding a way to create short URLs using Perl, AppleScript and Automator, which also required that one download a package of software.

Since that hint was published, the URL service (qgf.in) went down because the owner left it to the wolves. So here's a new Service that completely eliminates the need for Perl and the download. This service uses bit.ly, but you can choose another website (make sure you get the API format [usually http://short.url/api?url=http://website.com/]) as well.

First, create a new Service in Automator. Set the top bar to read 'Service receives selected URLs in any application', and check 'Replaces selected text.'

Then drag in a Run AppleScript action from Utilities. Copy/paste the following script into the script field:

on run {input, parameters}
  set dlstring to ((path to temporary items folder as string) & "shortURL.html")
  tell application "URL Access Scripting"
    download ("http://bit.ly/api?url=" & (item 1 of the input)) to dlstring replacing yes
  end tell
  set x to open for access dlstring
  set aurl to read x
  close access x
  return aurl
end run
Save the service as Shorten URL. Now you can right-click on any URL and shorten it, which is useful for sites like Twitter.

The main difference between this service and the previous shortener is that the new one uses temporary files, and no Perl/UNIX, while the previous used a separate Perl/UNIX script to do its job.

[crarko adds: I tested this, and it works as described.]

Comments (18)


Mac OS X Hints
http://hints.macworld.com/article.php?story=20101223123552534