I’m getting started with a small project that requires OSX Native AppleScript CGI. All is going well so far, except for the issue of decoding URLs.
All I’ve seen so far out there in the world is Chuck Shotton’s old “Decode URL” OSAX, which doesn’t appear to have been updated for native OS X compatibility.
Can anyone suggest another method for decoding URLs that would be OS X native?
URL_Decode("Die%2Fa")
to URL_Decode(t)
set AppleScript's text item delimiters to "%"
set t to t's text items
set AppleScript's text item delimiters to {""}
set final_string to t's item 1
repeat with i in (items 2 thru -1 of t)
set to_decode to text from character 1 of i to character 2 of i
try
set the_rest to text from character 3 of i to character -1 of i
on error
set the_rest to ""
end try
hextoString(text from character 1 of i to character 2 of i)
set final_string to final_string & result & the_rest
end repeat
final_string
end URL_Decode
on hextoString(t)
set z to ""
repeat with i from 1 to t's length by 252
try
set z to z & (run script "«data cstr" & ¬
text from character i of t to character (i + 251) of t & "00» as string")
on error
set z to z & (run script "«data cstr" & ¬
text from character i of t to character -1 of t & "00» as string")
end try
end repeat
z
end hextoString
For long strings to decode, I’d suggest the old method of “offseting” both ASCII and HEX tables. The main objective: do not call standard additions for “run script” nor for “ascii char”, not for… nothing.
For a cool sample, take a look to Has’ “conversionLib” (routines related to hex-to-ascii conversion) at http://www.barple.pwp.blueyonder.co.uk/roughstuff/index.html#conversionLib