URL-encoding URLs in AppleScript
The AppleScript Safari API is apparently quite finicky and rejects Russian Cyrillic characters when loading URLs.
For example, the following URL https://en.wiktionary.org/wiki/стоять#Russian throws an error in AppleScript. Instead, Safari requires URL’s of the form https://en.wiktionary.org/wiki/%D1%81%D1%82%D0%BE%D1%8F%D1%82%D1%8C#Russian whereas Chrome happily consumes whatever comes along. So, we just need to encode the URL thusly:
use framework "Foundation"
-- encode Cyrillic test as "%D0" type strings
on urlEncode(input)
tell current application's NSString to set rawUrl to stringWithString_(input)
-- 4 is NSUTF8StringEncoding
set theEncodedURL to rawUrl's stringByAddingPercentEscapesUsingEncoding:4
return theEncodedURL as Unicode text
end urlEncodeWhen researching Russian words for vocabulary study, I use the URL encoding handler to load the appropriate words into several reference sites in sequential Safari tabs.
