Sunday, March 2, 2008

ShortenURL AppleScript Plugin for QuickSilver

Here is a small AppleScript plugin for QuickSilver. It takes a text URL input, and converts it to a short URL using the MetaMark.net REST API. The short URL is saved to the clipboard for pasting to a browser, or to an Email, IM, whatever. I've also built in Growl support so that you get notification of what's going on.

To use the script, copy the text below to a file named "Shorten URL.scpt". Put this file in your QuickSilver Actions folder, which is ~/Library/Application Support/Quicksilver/Actions/". Restart Quicksilver (press Command, Control, Q with a Quicksilver window open to restart it). Quicksilver will then automatically recognise the script and you're ready to go.

To use, open Quicksilver, press '.' to get the text input, and enter a URL. There is some type checking in the script, so you can enter "www.whatever.com", as well as "http://www.whatever.com". The script will automatically add the "http://" if you don't want to type it. If you don't enter any text, or if you just run shortenURL straight from within Quicksilver, it will take the URL of the front page in Safari and convert that.

The script will then convert the URL, notify you using Growl (if it's running), and put the resulting short URL in the clipboard. You can then paste this (Command-V) to wherever you'd like it.


using terms from application "Quicksilver"
on process text longURL
-- Init
my growlRegister()

-- If we didn't get a text string then grab the URL from Safari
if longURL is "" then
tell application "Safari"
if document 1 exists then
copy the URL of the front document to longURL
end if
end tell
end if

-- Format the URL accordingly in case it was types sans http://
if (longURL does not start with "http://") then
set longURL to "http://" & longURL
end if

-- Convert the longURL
set shellScript to ("curl --url \"http://metamark.net/api/rest/simple?long_url=" & longURL & "\" ")

set shortURL to (do shell script shellScript)

-- Display success message
growlNotify("Short URL now in clipboard:", shortURL)

-- Open the shortURL in Safari to test it
--tell application "Safari"
--set URL of front document to shortURL
--end tell

set the clipboard to shortURL

end process text
end using terms from

using terms from application "GrowlHelperApp"
-- Register Growl
on growlRegister()
tell application "GrowlHelperApp"
register as application "Shorten URL" all notifications {"Alert"} default notifications {"Alert"} icon of application "Script Editor.app"
end tell
end growlRegister

-- Notify using Growl
on growlNotify(grrTitle, grrDescription)
tell application "GrowlHelperApp"
notify with name "Alert" title grrTitle description grrDescription application name "Shorten URL"
end tell
end growlNotify
end using terms from