Skip to content

Instantly share code, notes, and snippets.

@fannheyward
Created May 10, 2011 03:15
Show Gist options
  • Save fannheyward/963844 to your computer and use it in GitHub Desktop.
Save fannheyward/963844 to your computer and use it in GitHub Desktop.
longURl action for Quicksilver, powered by http://longurl.org
using terms from application "Quicksilver"
on process text _text
try
set the _encodedUrl to _urlEncode(_text) of me
set curlCMD to "curl --stderr /dev/null \"http://api.longurl.org/v2/expand?url=" & _encodedUrl & "\""
tell me to set _longURL to (do shell script curlCMD)
set AppleScript's text item delimiters to "CDATA["
set _longURL to text item 2 of _longURL
set AppleScript's text item delimiters to "]]"
set _longURL to text item 1 of _longURL
set AppleScript's text item delimiters to ""
set the clipboard to _longURL
tell application "Quicksilver" to set selection to _longURL
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 < 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment