Posted By

Todd on 04/03/09


Tagged

url urls shorten bitly textexpander


Versions (?)

Who likes this?

3 people have marked this snippet as a favorite

liamr
kenton
sheedy


bit.ly URL-shortening script for TextExpander that associates URLs with your bit.ly account


 / Published in: ActionScript 3
 

TextExpander is a great Mac tool that expands special abbreviations typed anywhere in the OS into useful snippets of text. One of the AppleScript snippets included shortens a URL in the clipboard using the bit.ly URL-shortening service. You copy the URL from your browser and just type /bitly in an email, IM, or Twitter, for example, and the abbreviation is replaced by a shortened URL.

One advantage of bit.ly over other shortening services is that it provides click statistics and other useful information for each shortened URL. If you have a (free) bit.ly account, the URLs you shorten are easily accessed through their site. However, the script included with TextExpander shortens the URL without associating it with your bit.ly account. This version of the script is a very simple modification which associates URLs you shorten with your bit.ly account.

To use this script, change [your_username] and [your_API_key] in line 9 to the correct values. (Once you have created a bit.ly account, you can find your API key here: http://bit.ly/app/developers.) Create a new snippet in TextExpander and paste the script into the Content field, making sure to select AppleScript in the dropdown above the field. Set the abbreviation and you're done.

  1. set the ClipURL to (the clipboard as string)
  2.  
  3. ignoring case
  4. if ((characters 1 through 4 of ClipURL as string) is not "http") then
  5. return "Malformed URL."
  6. else
  7. set the EncodedClipURL to urlencode(ClipURL) of me
  8. set curlCMD to ¬
  9. "curl --stderr /dev/null \"http://bit.ly/api?url=" & EncodedClipURL & "&login=[your_username]&apiKey=[your_API_key]\""
  10.  
  11. -- Run the script and get the result:
  12. set tinyURL to (do shell script curlCMD)
  13.  
  14. return tinyURL
  15. end if
  16. end ignoring
  17.  
  18.  
  19. on urlencode(theText)
  20. set theTextEnc to ""
  21. repeat with eachChar in characters of theText
  22. set useChar to eachChar
  23. set eachCharNum to ASCII number of eachChar
  24. if eachCharNum = 32 then
  25. set useChar to "+"
  26. else if (eachCharNum ≠ 42) and (eachCharNum ≠ 95) and (eachCharNum < 45 or eachCharNum > 46) and (eachCharNum < 48 or eachCharNum > 57) and (eachCharNum < 65 or eachCharNum > 90) and (eachCharNum < 97 or eachCharNum > 122) then
  27. set firstDig to round (eachCharNum / 16) rounding down
  28. set secondDig to eachCharNum mod 16
  29. if firstDig > 9 then
  30. set aNum to firstDig + 55
  31. set firstDig to ASCII character aNum
  32. end if
  33. if secondDig > 9 then
  34. set aNum to secondDig + 55
  35. set secondDig to ASCII character aNum
  36. end if
  37. set numHex to ("%" & (firstDig as string) & (secondDig as string)) as string
  38. set useChar to numHex
  39. end if
  40. set theTextEnc to theTextEnc & useChar as string
  41. end repeat
  42. return theTextEnc
  43. end urlencode

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: nick108 on September 13, 2010

Just tested with TextExpaner 3.1.2, but although I set content to AppleScript it does not return the bit.ly URL, but the complete snippet code instead. I tried restarting TextExpander to no avail.

Posted By: nick108 on September 14, 2010

Running the snippet in AppleScript code editor returns

"The command exited with a non-zero status." number 3

You need to login to post a comment.