There are many services you can use to make long url's a bit friendly, but all work the same, you need to open your browser point to the selected service and post your url on a form to get a shortener version.

But we can learn how to use different osx tools to access this services and parse the response. First using terminal script, the main idea is using "curl", a standard program included on every unix distro to make an http request, then copy the result to the clipboard.

With this script we are going to query tinyurl, a popular shortening url service

#!/bin/sh

curl -s http://tinyurl.com/api-create.php?url=${1} | pbcopy $1;	

Another variant of this script allows us to query a service using a basic authentication protocol, this time the example uses tr.im service, another popular one that offers the option to save your converted urls to maintain a historic.

#!/bin/sh

curl -u user:password -s http://api.tr.im/api/trim_simple?url=${1} | pbcopy $1;

A more elegant approach is to use quicksilver with some applescript magic to simplify all the process a bit more. The script is a bit complex, but all you really need is to copy next lines and paste it inside a file (ex.- tiny.scpt) on your "~/Library/Scripts/" and finally rescan quicksilver catalog.

This version sends the clipboard content to the desired service and notifies you with growl when the response is copied back to the clipboard,

set longURL to the clipboard
set cmd to "curl http://tinyurl.com/api-create.php?url=" & longURL
set shortURL to do shell script cmd
set the clipboard to shortURL as text

tell application "GrowlHelperApp"
set the allNotificationsList to {"URL copied to clipboard"}
set the enabledNotificationsList to {"URL copied to clipboard"}
register as application "TinyURL Service" all notifications allNotificationsList default notifications enabledNotificationsList icon of application "Script Editor"
notify with name "URL copied to clipboard" title "URL copied to clipboard" description shortURL application name "TinyURL Service"
end tell

That's all. Simple enough, isn't it?

Post a reply

Valid HTML tags: <strong>, <em>, <a>, <sup>, <sub>, <kbd>, <code>, <pre>