How-to: Automatic URL Shortening with Bit.ly and TextExpander

July 23rd, 2008 by jonathan berger

Hi, my name is jonathanpberger and I’ll be covering all sorts of nerdy topics here on LiveDigitally. Got comments? Questions? Rants? You can drop me a line at jpb@livedigitally.com or see what else I’m up to on my Twitter feed, my homepage, or my tumbleblog.

One of my can’t-live-without-it time-and-keystroke-saving apps (TextExpander) got bumped to version 2.3 today. It’s a minor update: a few bug-fixes and a new snippet group for Accented Words (like “rosé”, “olé”, “sauté”, or “jalapeño”). But I got to playing around, and saw a cool video demonstrating how to use TextExpander for another time-and-keystroke-saver: URL-Shortening.

URL-Shortening may not seem like a big deal, but it comes in handy when you’re dealing with space restrictions (like Twitter’s 140 character-limit), you want to tidy up some messy links, or you’re trying to RickRoll someone. It’s one of those possibly-extraneous activities that makes life a tiny little bit easier, but only when it’s effortless to employ. Enter TextExpander.

Jack-Daniyel Strong has a great tip for shortening URLs using TinyURL, TextExpander, and Applescript. After setting it up, you can shorten URLs by

1) copying the URL to the clipboard, and 2) typing a preset abbreviation. It’s that easy.

I’ve adapted his technique for the new kid on the URL-shortening block, Bit.ly (which everyone is gushing over), and which provides a host of neat features like History, Click/Referrer tracking, thumbnails, mirroring, and best of all, a robust API.

I’ve written briefly about TextExpander in the past, but allow me to go on at greater length: TextExpander is frickin’ AWESOME. It provides system-wide text abbreviations which I use literally tens or even hundreds of times each day. Common typos? Fixed. “Teh” turns into “the” automatically, every time, in any application. Common abbreviations or boilerplate text? Use a few letters to spit out long, commonly-used strings of text. I use “jonathanpberger” as my account name on pretty much every public web service around: Twitter, Flickr, Del.icio.us, Google account, etc. I must type those 15 characters a hundred times a day—except I don’t type them. I type “jjpb”, and they’re filled in automatically.

How about HTML or CSS? Not only can I type “,a” (that’s “comma-A”) and get

<a href=""></a>

or type “,img” (that’s “comma-i-m-g”) for

<img src="" alt="" width="" height="" border="0" />

but the cursor will return to the spot where I need to start typing: between the quotes after the “href=""” or “src=""”.

What’s more, SmileOnMyMac (the company responsible for TextExpander) provides libraries of useful snippets like HTML, CSS, common misspellings, and more, all of which you can install with a click.

Ok, enough gushing. This isn’t an infomercial. What we’re here for today is TextExpander’s AppleScript support. Here we go:

  1. Create a new snippet (command-N) and paste the following AppleScript in the “Content” box:
  2. 	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://bit.ly/api?url=" & ClipURL & "\""
    
    			-- Run the script and get the result:
    			set tinyURL to (do shell script curlCMD)
    
    			return tinyURL
    		end if
    	end ignoring
  3. Above the box, you’ll want to change “Content: Plain Text” to “Content: AppleScript” so that we execute the script instead of replacing our text with the raw code.
  4. Set the Abbreviation. I like the standard TextExpander convention of doubling the first letter (I use the default “ttime” for a timestamp), so I use “bbit”.

Here’s what it should look like:

Setting up the Bit.ly AppleScript in TextExpander

All done. Now let’s say (hypothetically) you have a cousin who’s researching family genealogy on her summer break. Maybe (if you’re a cousin of mine) she’ll have sent you a link to an image of the immigration record on Ellis Island that brought your family to this country back in 1908. It might have a URL that looks like this:

http://www.ellisisland.org/EIFile/popup_weif_5a.asp?src=%2Fcgi%2Dbin%2Ftif2gif%2Eexe%3FT%3D%5C%5C%5C%5C192%2E168%2E4%2E227%5C%5Cimages%5C%5CT715%2D1081%5C%5CT715%2D10810827%2ETIF%26S%3D%2E5&pID=101759060124&name=Beile%26nbsp%3BMinowitz&doa=Mar+07%2C+1908&port=Liverpool&line=0019

Now that we’ve got the Bit.ly AppleScript set up, all we have to do is select that painfully long URL, copy it to the clipboard (Edit>Copy or command-C), and type “bbit”. Voilà:

http://bit.ly/4swZ2G

That’s a way easier URL to post on Twitter!

Like the post? Share it here:

10 Responses to “How-to: Automatic URL Shortening with Bit.ly and TextExpander”

  1. My First Article Is Up at LiveDigitally.com Says:

    [...] I’ve started writing for a tech blog called LiveDigitally.com and my first post is up! It’s incredibly nerdy. Go check it out! [...]

  2. SmileOnMyMac Blog » Blog Archive » TextExpander: Nice AppleScript Shortcut for bit.ly Says:

    [...] very interesting features, like history, thumbnails and click-through stats. Jonathan Berger posted an article at LIVEdigitally on creating a TextExpander AppleScript snippet to quickly generate bit.ly [...]

  3. someone Says:

    If you’re truncating the URL of your front Safari window, you can cut out the selecting/copying steps like this:

    tell application "Safari"

    set PageURL to URL of front document

    end tell

    set curlCMD to ¬
    "curl --stderr /dev/null \"http://bit.ly/api?url=" & PageURL & "\""

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

    return bitlyURL

  4. Rawkes Weekly – 27 July 2008 » Captain's Blog » Rawkes Says:

    [...] Automatic URL shortening with TextExpander – Applescript to let TextExpander automatically create bit.ly URLs from a url in the clipboard [...]

  5. Cristine Says:

    How about http://sn.vc? They allow to preview the original links before redirected. For me, it’s useful as to prevent mislead.

  6. Downshift: The Second Gear Blog » Blog Archive » Our Toolbox: TextExpander Says:

    [...] timesaver. When pasting long URLs around the Web, I like to shrink them using bit.ly. There is a script that will automatically take the link on your clipboard and convert it to a bit.ly-fied [...]

  7. Quickly Extract a Shortened URL for Anything on the iTunes App Store | MacMembrane Says:

    [...] 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. [...]

  8. Add Bit.ly URL Shortening To Quicksilver | Lifehacker Australia Says:

    [...] find one that worked or was quick enough for me. So, with the help of Jonathan Berger at LIVEdigitally and Andrew Hedges over on Stack Overflow, I was able to cobble together some Applescripts for use [...]

  9. Add Bit.ly URL Shortening to Quicksilver [How To] · TechBlogger Says:

    [...] find one that worked or was quick enough for me. So, with the help of Jonathan Berger at LIVEdigitally and Andrew Hedges over on Stack Overflow, I was able to cobble together some Applescripts for use [...]

  10. skillet Says:

    This doesn’t seem to work anymore. Has anyone tried this code since Bitly.com changed things around with their service and tracking links?

Leave a Reply