Like many Mac users, I prefer NetNewsWire for managing my RSS feeds. It works just fine, until I come across an article I’d like to share via my Twitter account. Because Twitter has such tight restrictions on what you can squeeze into a single tweet (140 characters), it’s imperative to shorten a URL before you tweet it.

There are multiple URL-shortening services available. TinyURL is the best known, but personally I prefer because you just can’t beat a five-character root domain.

NetNewsWire does not currently offer any URL shortening features “out of the box.” So when I want to tweet about an article in one of my feeds, I have to

– open the article in Firefox
– hit the button for the Creator Firefox extension
– paste the now-shortened URL into my Twitter client.

While these are not a lot of steps, something as computer-friendly as URL shortening shouldn’t require even this much interaction. Fortunately, NetNewsWire is highly scriptable thanks to a fleshed-out AppleScript dictionary and the ability of AppleScript to execute command-line scripts.

After glancing at NetNewsWire’s sample AppleScripts and reading up on command-line clipboard support I was able to come up with a script that does just what I need.

Update: The script now knows when you’re looking at a web page rather than at a list of headlines. It will “just work” the way you’d expect.

Second update: The downloadable version now features Growl support, but for simplicity I’m not including those portions below.

Here’s what it looks like:

tell application "NetNewsWire"
if index of selected tab = 0 then
-- We're looking at headlines, so just get the headline URL
set s to URL of selectedHeadline
-- We're looking at a web view tab, so we need to know which tab
set i to index of selected tab
set i to i + 1
-- Get the tab's URL
set URL_list to URLs of tabs
set s to item i of URL_list
end if
-- Build the GET request for the API
set s to "" & s
-- Submit the GET request and copy the results to clipboard
do shell script "curl " & s & " | pbcopy"
end tell

One nice feature of the script is that it uses the actual API. Of course, if you wanted to use a different shortening service you could just change the URL and parameter syntax as appropriate.

The script stops short of switching to my Twitter client and pasting the shortened URL there. That’s because I almost always paste the URL after I’ve written up some summary info about it. Easier for me to type that info and hit command-V rather than fiddle with changing the insertion point after the URL has been auto-pasted.

If you’d like to use this script yourself, simply download it as a .zip archive, decompress it, and place it in ~/Library/Application Support/NetNewsWire/Scripts. It’ll automatically show up in NetNewsWire’s scripts menu. Use it with any headline selected or on a web view tab, and you’ll have your shortened URL right on the clipboard.

January 31, 2009