So to “fix” theLink portion in my code above, I have come up with:
set theLink to reference URL of theRecord & ¬
"?page=" & current page of fWindow & ¬
"&search=" & my urlencode(theText)
and pulled some code from stack overflow to url encode some text as follows:
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 "%20"
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
Hey, Rick… let me save you a little suffering and learn you something
You can run ad-hoc JavaScript in an AppleScript via the do Javascript command. The only requirement is it has to run in a think window. However, you don’t have to specify a window nor does the window need to be even remotely related to the command. It’s literally do javascript "<insert script here>" in think window 1
set theText to "Hey, Rick! Here's an easier way to URL encode some text via AppleScript…"
tell application id "DNtp"
do JavaScript "encodeURIComponent(\"" & theText & "\");" in think window 1 -- The argument needs to be in escaped double quotes
end tell
---> Hey%2C%20Rick!%20Here's%20an%20easier%20way%20to%20URL%20encode%20some%20text%20via%20AppleScript%E2%80%A6
And yes @chrillek, I sometimes use this to inject a little JS into AS as the situation needs it.
Making this open where it can be used at any time will make this a killer feature across all scripting. Seriously.
@chrillek and I were looking at an issue recently and he had a perfect JavaScript solution. The problem was I already had a giant applescript so rewriting in JXA wouldn’t have worked for me.