Long and shortened URLs with TypeIt4Me

This is a brilliant idea from Jeff Gamet posted at The Mac Observer. He uses TextExpander to run an AppleScript that takes a URL on the clipboard and processes it through TinyURL to get a shortened URL that fits better in Twitter, AIM, and mail messages. It does basically what my scurl script does, but because it doesn’t have to be called through Launchbar or Quicksilver or FastScripts, there’s even less context switching.

I don’t use TextExpander, I use TypeIt4Me from Ettore Software, but they are the same type of program and have roughly the same features. Their main purpose is to allow you to assign a short abbreviation to a longer piece of boilerplate text. When you type the abbreviation and some triggering keystroke, the abbreviation is replaced by the boilerplate. Of course, many text editors and word processors have this feature, but the great thing about TypeIt4Me and TextExpander is that they are system-wide utilities—one set of abbreviations works in all programs, including dialog boxes.

A lesser-used feature of TypeIt4Me and TextExpander is the ability to trigger AppleScripts. “Lesser-used” is something of an understatement; I’d never even thought about triggering an AppleScript from TypeIt4Me until I saw Jeff Gamet’s tip. But as soon as I saw it, I realized that all three of my URL-handling scripts, furl, surl, and scurl, could be adapted to work as TypeIt4Me triggers.

Let’s start with furl. The stand-alone version of this AppleScript takes the URL of the frontmost Safari tab and puts it on the clipboard. Now, I just want it to return that URL, which is a trivial script:

tell application "Safari" to get URL of front document

Here’s the TypeIt4Me preference pane, showing the abbreviations and clippings.

To get an abbreviation to trigger an AppleScript clipping, use the Insert popup menu down near the bottom of the window, and work your way through the submenus until you get to the Execute as AppleScript item.

Then insert the text of the AppleScript after the little “a/s” symbol in the clipping pane. The trick is to write the AppleScript so it returns the text you want inserted. The script should probably end with a “get” or “do shell script” command. Now I can type “furl” into any application, hit the Escape key (which is my TypeIt4Me trigger—you can configure many other triggers), and the URL of the frontmost web page replaces the “furl.”

A more useful abbreviation is “surl,” which I have set to run this AppleScript:

tell application "Safari" to set longURL to URL of front document
set cmd to "curl http://metamark.net/api/rest/simple?long_url=" & longURL
do shell script cmd

This gets Safari’s frontmost URL, passes it through the Metamark URL shortening service—which I use instead of TinyURL—and returns the shortened URL.

The abbreviation that most closely resembles Jeff Gamet’s original TextExpander abbreviation is “scurl,” which runs this AppleScript:

set longURL to the clipboard
set cmd to "curl http://metamark.net/api/rest/simple?long_url=" & longURL
do shell script cmd

It takes a URL on the clipboard, passes it through Metamark, and returns the shortened version.

If you do much writing about web sites—whether it’s for public consumption or your own research notes—these abbreviation scripts can save you a lot of time. They allow your writing to flow without switching from editor to browser and back. They’re a further improvement on my standalone AppleScripts in that they insert the URLs directly, rather than putting them on the clipboard for pasting.

This doesn’t mean my standalone scripts are obsolete. There are plenty of times when I want to collect several URLs while in the browser, then paste them into a set of notes later. For this, the standalone scripts, coupled with a multiple-clipboard utility like Quicksilver or Jumpcut (which is what I use), are still the best.

You can, of course, adapt these scripts to your situation. If you use Firefox instead of Safari, look here. If you prefer TinyURL to Metamark, you’ll want to use this line

set cmd to "http://tinyurl.com/api-create.php?url=" & longURL

And if you’re a TextExpander user, go back to Jeff Gamet’s original tip to see how to make its abbreviations run AppleScripts.