Using bit.ly and Quicksilver to Shorten URLs

If you have a mac, and don’t use Quicksilver, well, you should!

Here’s how to use Quicksilver to shorten URLs using bit.ly. This is useful for many things, but especially for posting links to twitter.

Obviously you need Quicksilver installed, and you also need to create an account with bit.ly, and get an API key. (API stands for application programming interface, which is basically a set of standard ways to interact with bit.ly using computer code.) You find the key listed under your account once you register. You’ll also need Growl, which is a program that delivers alerts from different apps to your mac.

So, to start, you open Applescript, and paste the following into a new script:

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://api.bit.ly/shorten?version=2.0.1&longUrl=" & longURL & "&login=YOUR_BITLY_ID" & "&apiKey=YOUR_BITLY_API_KEY " & "\" | grep shortUrl | grep -o http.*[/a-zA-Z0-9] ")
		
		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

Now, find in the script where I wrote in all caps YOUR_BITLY_ID and YOUR_BITLY_API_KEY — that’s where you plug in your info from bit.ly.

Next, save this file (I use the file name “Shorten URL”) in your Actions folder, which is located on your mac in:

Home -> Library -> Application Support -> Quicksilver -> Actions

One note — make sure your Actions folder appears in the Catalog in quicksilver. If it does not, open the catalog (apple-comma), go to Custom, and use the + sign on the bottom to find your Actions folder and add it in.

Rescan the catalog, and you’re done.

To use it, first copy a long URL to the clipboard. Then, invoke Quicksilver, paste the URL in, and tab to the second box. Type “Shorten URL” (or whatever you named the script), and hit enter.

You should see a growl alert with the new URL, and it will be in your clipboard. Paste away!

Credit where credit is due:

I didn’t write this script on my own, but found these two posts, which I used to create the script:

http://tanniespace.com/bitly-textexpander-applescript-win/

http://trumcgowan.tumblr.com/post/84901795/shorten-urls-using-quicksilver

Programming is all about remix, really… :-)

Advertisements

6 responses

  1. thanks so much for this! it works great

    just one tip – if you dont see the action in the second panel right away, it may help to quit Quicksilver and reopen it again. i did a rescan catalog but that didn’t find the script, but then i did the quit and relaunch and it was there.

  2. Hey, thanks!

    Good point about restarting QS…definitely the catalog rescan doesn’t always work.

    Glad it works for you!

  3. Thanks for post! Unfortunately, I followed the instructions without success. Is the script still working or did I do something wrong?

    1. Hi, first, sorry it took me so long to reply…

      Yes, it’s broken. I still have not been able to figure out how to use the new bit.ly APIs to get this working again.

      Any help would be appreciated!!!

  4. Any update on this? Would so love it to work!

    1. hi,

      yes, i have been so lazy about blogging i’ve forgotten to post that i have this working again!

      it seems to work now if you paste in the url you want shorten first, and then tab over and invoke your script, or, you can invoke your script, and quicksilver has a “process text” action, in the third pane you paste in the URL.

      here’s the script:

      using terms from application “Quicksilver”
      on process text _text
      try

      set login to “LOGIN_NAME” — Put your login name here
      set api_key to “API_KEY” — Put your API key name here
      set the _encodedUrl to _urlEncode(_text) of me
      set curlCMD to ¬
      ¬
      “curl –stderr /dev/null \”http://api.bit.ly/shorten?longUrl=” & _encodedUrl & “&history=1&version=2.0.1&login=” & login ¬
      & “&apiKey=” & api_key ¬
      & “\”| grep shortUrl | grep -o http.*[/a-zA-Z0-9]”

      tell me to set _bitlyUrl to (do shell script curlCMD)
      set AppleScript’s text item delimiters to “http://”
      set _bitlyUrl to text item 3 of _bitlyUrl
      set AppleScript’s text item delimiters to “\””
      set _bitlyUrl to “http://” & text item 1 of _bitlyUrl
      set AppleScript’s text item delimiters to “”

      set the clipboard to _bitlyUrl
      tell application “Quicksilver” to set selection to _bitlyUrl
      on error a number b
      say a
      return
      end try
      end process text
      end using terms from

      on _urlEncode(theText)

      set theTextEnc to “”
      repeat with eachChar in characters of theText
      set useChar to eachChar
      set eachCharNum to ASCII number of eachChar
      if eachCharNum = 32 then
      set useChar to “+”
      else if (eachCharNum ≠ 42) and (eachCharNum ≠ 95) ¬
      and (eachCharNum 46) ¬
      and (eachCharNum 57) ¬
      and (eachCharNum 90) ¬
      and (eachCharNum 122) then
      set firstDig to round (eachCharNum / 16) rounding down
      set secondDig to eachCharNum mod 16
      if firstDig > 9 then
      set aNum to firstDig + 55
      set firstDig to ASCII character aNum
      end if
      if secondDig > 9 then
      set aNum to secondDig + 55
      set secondDig to ASCII character aNum
      end if
      set numHex to (“%” & (firstDig as string) ¬
      & (secondDig as string)) as string
      set useChar to numHex
      end if
      set theTextEnc to theTextEnc & useChar as string
      end repeat
      return theTextEnc
      end _urlEncode

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: