TypeIt4Me URL shortener using Bitly

Posted by & filed under Uncategorized.

Here's a quick thingy for bit.ly-ing urls. Copy a url, type your snippet and TypeIt4Me makes it a bit.ly url. To make applescript work right in TypeIt4Me, you'll need to add the a/s character at the beginning of your snippet content. At the bottom of the window, go to Insert> Special> a/s (Execute As Applescript)
set the ClipURL to (the clipboard as string)
# to get your api key, go to http://bitly.com/a/your_api_key
set u to "" # your username goes in the quotes
set k to "" # your api key goes in the quotes


ignoring case
	if ((characters 1 through 4 of ClipURL as string) is not "http") then
		return "Malformed URL."
	else
		set ClipURL to deHash(ClipURL)
		set curlCMD to ¬
			"curl --stderr /dev/null \"http://api.bitly.com/v3/shorten?login=" & u & "&apiKey=" & k & "&longUrl=" & ClipURL & "&format=txt\""
		
		-- Run the script and get the result:
		set tinyURL to (do shell script curlCMD)
		
		return tinyURL
	end if
end ignoring

on deHash(uri)
	set hash to "#"
	set rehash to "%23"
	set ditd to text item delimiters of AppleScript
	set text item delimiters of AppleScript to hash
	set uri to text items of uri
	set text item delimiters of AppleScript to rehash
	set uri to "" & uri
	set text item delimiters of AppleScript to ditd
	return uri
end deHash

Leave a Reply