Shorten your URLs with TextExpander and Clicky.me

TextExpander comes with an Internet Productivity suite that contains URL shortening snippets for a variety of services including bit.ly and is.gd. One of the newer services that I just discovered is called Clicky. Clicky is a Web analytics package that makes tracking how people are finding and using your Web sites a breeze. As part of the package they also offer a URL shortening service called clicky.me. I’ve started using clicky.me for all my shortened URLs, and of course I need a TextExpander snippet to do that. The snippet takes any URL that is on your Mac’s clipboard and runs it through Clicky’s API to generate a shortened URL that you can paste into Twitter, Facebook or anywhere you so desire. Note: You do need a Clicky user account, which is free for low volume users. Read on for instructions on how to use this snippet.

  1. Copy the text below into a new AppleScript snippet (also available on Gist)
  2. Update your yourUsername and yourPassword variables on line 2 and 3 to match your clicky.me authentication credentials.
  3. Set an expansion trigger. I’ve set mine to be /clicky, but it can be anything you desire.

-- CONFIGURATION OPTIONS BEGIN
set the yourUsername to "username"
set the yourPassword to "password"
-- CONFIGURATION OPTIONS END

set the ClipURL to (the clipboard as string)

ignoring case
	if ((characters 1 through 4 of ClipURL as string) is not "http") then
		return "Malformed URL."
	else
		set the EncodedClipURL to urlencode(ClipURL) of me
		set curlCMD to ¬
			"curl --stderr /dev/null \"http://clicky.me/app/api?username=" & yourUsername & "&password=" & yourPassword & "&url=" & EncodedClipURL & "\""
		
		-- Run the script and get the result:
		set tinyURL to (do shell script curlCMD)
		
		return tinyURL
	end if
end ignoring


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
Clicky Snippet In TextExpander

It should look like so when you are done: Where Clicky’s URL shortening gets to be really useful is when you use it to track shortened URLs to sites you are tracking on their service. To take advantage of that, you need to provide the URL shortener with your site ID. You can use this script instead (also available on Gist)


-- CONFIGURATION OPTIONS BEGIN
set the yourUsername to "username"
set the yourPassword to "password"
set the yourSiteId to "site_id"
-- CONFIGURATION OPTIONS END

set the ClipURL to (the clipboard as string)

ignoring case
	if ((characters 1 through 4 of ClipURL as string) is not "http") then
		return "Malformed URL."
	else
		set the EncodedClipURL to urlencode(ClipURL) of me
		set curlCMD to ¬
			"curl --stderr /dev/null \"http://clicky.me/app/api?username=" & yourUsername & "&password=" & yourPassword & "&site_id=" & yourSiteId & "&url=" & EncodedClipURL & "\""
		
		-- Run the script and get the result:
		set tinyURL to (do shell script curlCMD)
		
		return tinyURL
	end if
end ignoring


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

You will need to know your site ID in addition to your username and password. The site ID can be parsed from the URL of your site’s stats page by looking at the site_id parameter. For example, take the following URL: http://getclicky.com/stats/home?site_id=123456 It’s site ID is the 123456.