Skip to content

Instantly share code, notes, and snippets.

@braidn
Created July 31, 2010 01:55
Show Gist options
  • Save braidn/501647 to your computer and use it in GitHub Desktop.
Save braidn/501647 to your computer and use it in GitHub Desktop.
set the PageURL to (the clipboard as string)
set rlogin to "yourLogin"
set mapikey to "yourApiKey"
set the EncodedURL to urlencode(PageURL) of me
set curlCMD to ¬
¬
"curl --stderr /dev/null \"http://api.bit.ly/v3/shorten?longUrl=" & EncodedURL & "&history=1&format=txt&login=" & rlogin ¬
& "&apiKey=" & mapikey ¬
& "\""
-- Run the script and get the result:
set bitlyURL to (do shell script curlCMD)
return bitlyURL
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 < 45 or eachCharNum > 46) ¬
and (eachCharNum < 48 or eachCharNum > 57) ¬
and (eachCharNum < 65 or eachCharNum > 90) ¬
and (eachCharNum < 97 or 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
@braidn
Copy link
Author

braidn commented Aug 4, 2010

I was turned on to this script by a friend; however, it used an older version of the bit.ly api (v 2.0). After a couple of swings and an email to the awesome peeps at bit.ly, this is a much better running and performing script. Currently I run it through Textexpander to hand me the shortUrl. Fork, do whatever, just make sure it involves a shit load of fun

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment