10 Comments

Summary:

It’s become common practice to use services such as bit.ly to shorten URLs, but this is usually done using a bookmarklet script, which only works in browsers. This Automator service allows you to select an URL in any application and shorten it using the bit.ly API.

automator-feature

It’s become common practice to use services such as bit.ly to shorten URLs, but this is usually done using a bookmarklet script, which only works in web browsers. This Automator service allows you (on OS X 10.6 Snow Leopard) to select a URL in any application and shorten it using the bit.ly 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"

else
set curlCMD to "curl --stderr /dev/null \"http://bit.ly/api?url=" & 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 bit.ly” 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 bit.ly API, then paste into your browser. You’ll see that a bit.ly 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):

  1. Great idea! I did come across a small problem of convenience in Chrome, however. Since the browser no longer displays the protocol, the Applescript as you wrote it doesn’t work easily in Chrome’s location bar.

    It wasn’t too difficult to fix for that purpose–the script can just append “http://” to the string if it’s not already there. The modified script is below, if anyone wants it.

    I haven’t played with Automator too much, but if it can do things like this, I’ll have to poke around a bit! Thanks for the tip.
    ——- ————-
    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 “http:// added”
    set input to “http://” & input
    end if

    set curlCMD to “curl –stderr /dev/null \”http://bit.ly/api?url=” & input & “\””
    set tinyURL to (do shell script curlCMD)
    return tinyURL

    end ignoring
    end run

  2. Thanks! a great introduction to using Automator and doing something useful.
    /paul

  3. This is a great article and it nice to see automator getting some press.

    Yet another way that is simpler and easier for most people is to use CopyPaste the multiple clipboard archive, display and editing tool. It has a number of the url shorteners built in (just choose one in the prefs) which you can use by copying or highlighting any url and then choosing the shorten url in the CopyPaste menubar menu.

    CopyPaste is free to try out here:
    http://plumamazing.com

    The url shortening is only one of its many abilities. CopyPaste is my favorite utility and always the first I put on every Mac and one I recommend to everyone especially writers. Multiple clipboards are essential to productivity.

  4. I hate to be “that guy”, but…

    Just use (free) CloudApp. A keyboard shortcut lets you shorten any link you please, upload files from the Finder, iTunes, iPhoto, etc.

    I’m not affiliated yada yada http://getcloudapp.com/

  5. somethingtolearn Sunday, January 2, 2011

    Hi everyone, I’m trying to follow this tutorial but when i ask to copy to clipboard my link it doesnt do it. I’ve follow each step easily but its jump error. Its there any problem? i’ve tried typing my urls with http:// and without it.

    Please help!

    1. Are you connecting through a proxy server?

      Just change the curl statement if needed:

      curl -u user:passwd -x my-proxy:888 http://www.get.this/

  6. Can you get this to work from Word and Firefox too?

    I do not get the Service to show up on the menu.

  7. Peter Anthony Gales Wednesday, January 5, 2011

    Very clear instructions, and a very useful app. Thanks much.

  8. Hello:) Nice app.. I have some problem tho..

    When i run it, it just comes upp “invalid URL”…

    What do i do wrong?

Comments have been disabled for this post