text
stringlengths 0
15.7k
| source
stringlengths 6
112
|
---|---|
return theList as text
|
lns3.scpt
|
end _replaceText
|
lns3.scpt
|
to encode URL characters theText preserving allowedCharacters : ("")
|
lns3.scpt
|
set theText to _support's asTextParameter(theText, "")
|
lns3.scpt
|
set allowedCharacters to _safeCharacters & _support's asTextParameter(allowedCharacters, "preserving")
|
lns3.scpt
|
set asocAllowedChars to current application's NSCharacterSet's characterSetWithCharactersInString:allowedCharacters
|
lns3.scpt
|
set asocResult to _support's asNSString(theText)'s stringByAddingPercentEncodingWithAllowedCharacters:asocAllowedChars -- Returns a new string made from the receiver by replacing all characters not in the allowedCharacters set with percent encoded characters. UTF-8 encoding is used to determine the correct percent encoded characters. Entire URL strings cannot be percent-encoded. This method is intended to percent-encode an URL component or subcomponent string, NOT the entire URL string. Any characters in allowedCharacters outside of the 7-bit ASCII range are ignored.
|
lns3.scpt
|
if asocResult is missing value then error "Couldn't convert characters to percent escapes." number -1703 from theText -- NSString docs are hopeless on details
|
lns3.scpt
|
return asocResult as text
|
lns3.scpt
|
_error("encode URL characters", eText, eNumber, eFrom, eTo)
|
lns3.scpt
|
end encode URL characters
|
lns3.scpt
|
to decode URL characters theText
|
lns3.scpt
|
set asocResult to _support's asNSString(theText)'s stringByRemovingPercentEncoding() -- Returns a new string made from the receiver by replacing all percent encoded sequences with the matching UTF-8 characters. (NSURLUtilites, 10.9+)
|
lns3.scpt
|
if asocResult is missing value then error "Couldn't convert percent escapes to characters (e.g. not valid UTF8)." number -1703 from theText
|
lns3.scpt
|
_error("decode URL characters", eText, eNumber, eFrom, eTo)
|
lns3.scpt
|
end decode URL characters
|
lns3.scpt
|
to split URL query string queryText
|
lns3.scpt
|
set oldTIDs to AppleScript's text item delimiters
|
lns3.scpt
|
set queryText to _replaceText(_support's asTextParameter(queryText, ""), "+", space)
|
lns3.scpt
|
set AppleScript's text item delimiters to "&"
|
lns3.scpt
|
set queryList to queryText's text items
|
lns3.scpt
|
set AppleScript's text item delimiters to "="
|
lns3.scpt
|
repeat with aRef in queryList
|
lns3.scpt
|
set queryParts to text items of (get aRef's contents)
|
lns3.scpt
|
if length of queryParts ≠ 2 then error "Invalid query string." number -1703 from queryText -- TO DO: implement 'without strict parsing' option, in which case missing `=` wouldn't throw error?
|
lns3.scpt
|
set aRef's contents to {decode URL characters (item 1 of queryParts), decode URL characters (item 2 of queryParts)}
|
lns3.scpt
|
set AppleScript's text item delimiters to oldTIDs
|
lns3.scpt
|
return queryList
|
lns3.scpt
|
_error("split URL query string", eText, eNumber, eFrom, eTo)
|
lns3.scpt
|
end split URL query string
|
lns3.scpt
|
to join URL query string queryList
|
lns3.scpt
|
set queryList to _support's asList(queryList, "")'s items
|
lns3.scpt
|
set kvPair to aRef's contents
|
lns3.scpt
|
if not (count {kvPair} each list) = 1 and kvPair's length = 2 then error "Invalid query list (not a list of key-value sublists)." number -1703 from aRef
|
lns3.scpt
|
if not (count kvPair each text) = 2 then
|
lns3.scpt
|
error "Invalid query list (keys and values must be text)." number -1703 from aRef
|
lns3.scpt
|
set aRef's contents to _replaceText((encode URL characters (kvPair's item 1) preserving space) & "=" & (encode URL characters (kvPair's item 2) preserving space), space, "+")
|
lns3.scpt
|
set queryText to queryList as text
|
lns3.scpt
|
return queryText
|
lns3.scpt
|
_error("join URL query string", eText, eNumber, eFrom, eTo)
|
lns3.scpt
|
end join URL query string
|
lns3.scpt
|
to encode JSON jsonObject extra white space isPrettyPrinted : (false)
|
lns3.scpt
|
if _support's asBooleanParameter(isPrettyPrinted, "extra white space") then
|
lns3.scpt
|
set writeOptions to current application's NSJSONWritingPrettyPrinted
|
lns3.scpt
|
set writeOptions to 0
|
lns3.scpt
|
if not (current application's NSJSONSerialization's isValidJSONObject:jsonObject) then
|
lns3.scpt
|
error "Can’t convert object to JSON (found unsupported object type)." number -1703 from jsonObject
|
lns3.scpt
|
set {theData, theError} to current application's NSJSONSerialization's dataWithJSONObject:jsonObject options:writeOptions |error|:(specifier)
|
lns3.scpt
|
if theData is missing value then error "Can’t convert object to JSON(" & theError's localizedDescription() & ")." number -1703 from jsonObject
|
lns3.scpt
|
return (current application's NSString's alloc()'s initWithData:theData encoding:(current application's NSUTF8StringEncoding)) as text
|
lns3.scpt
|
_error("encode JSON", eText, eNumber, eFrom, eTo)
|
lns3.scpt
|
end encode JSON
|
lns3.scpt
|
to decode JSON jsonText fragments allowed areFragmentsAllowed : (false)
|
lns3.scpt
|
set jsonText to _support's asTextParameter(jsonText, "")
|
lns3.scpt
|
if _support's asBooleanParameter(areFragmentsAllowed, "allowing fragments") then
|
lns3.scpt
|
set readOptions to current application's NSJSONReadingAllowFragments
|
lns3.scpt
|
set readOptions to 0
|
lns3.scpt
|
set theData to _support's asNSString(jsonText)'s dataUsingEncoding:(current application's NSUTF8StringEncoding)
|
lns3.scpt
|
set {jsonObject, theError} to current application's NSJSONSerialization's JSONObjectWithData:theData options:readOptions |error|:(specifier)
|
lns3.scpt
|
if jsonObject is missing value then error "Not valid JSON (" & theError's localizedDescription() & ")." number -1703 from jsonText
|
lns3.scpt
|
return jsonObject as any
|
lns3.scpt
|
_error("decode JSON", eText, eNumber, eFrom, eTo)
|
lns3.scpt
|
end decode JSON
|
lns3.scpt
|
to encode Base64 theText
|
lns3.scpt
|
set asocString to _support's asNSString(_support's asTextParameter(theText, ""))
|
lns3.scpt
|
return asocString's dataUsingEncoding:((current application's NSUTF8StringEncoding)'s base64EncodedStringWithOptions:0)
|
lns3.scpt
|
_error("encode Base64", eText, eNumber, eFrom, eTo)
|
lns3.scpt
|
end encode Base64
|
lns3.scpt
|
to decode Base64 theText
|
lns3.scpt
|
set asocData to current application's NSData's alloc()'s initWithBase64EncodedString:theText options:(current application's NSDataBase64DecodingIgnoreUnknownCharacters) -- ignores line breaks and other non-Base64 chars; TO DO: would it be better to strip white space chars separately, then fail if text contains any other non-Base64 chars
|
lns3.scpt
|
set asocString to (current application's NSString's alloc()'s initWithData:asocData encoding:(current application's NSUTF8StringEncoding))
|
lns3.scpt
|
if asocString is missing value then _support's throwInvalidParameter(theText, "", text, "Base64-encoded data doesn't contain UTF8-encoded text.")
|
lns3.scpt
|
_error("decode Base64", eText, eNumber, eFrom, eTo)
|
lns3.scpt
|
end decode Base64
|
lns3.scpt
|
property _excludeHeaders : {"Authorization", "Connection", "Host", "Proxy-Authenticate", "Proxy-Authorization", "WWW-Authenticate", "Content-Length"} -- note: unlike authorization headers, "Content-Length" isn't reserved by NSSession but is already set automatically so no point allowing users to override with potentially wrong value
|
lns3.scpt
|
script _NSStringEncodings -- used by `send HTTP request` to automatically encode/decode text-based request/response body data
|
lns3.scpt
|
property _list : missing value
|
lns3.scpt
|
to _init()
|
lns3.scpt
|
set _list to {¬
{{"utf-8", "utf8"}, current application's NSUTF8StringEncoding}, ¬
|
lns3.scpt
|
{{"utf-16", "utf16"}, current application's NSUTF16StringEncoding}, ¬
|
lns3.scpt
|
{{"utf-16be", "utf16be"}, current application's NSUTF16BigEndianStringEncoding}, ¬
|
lns3.scpt
|
{{"utf-16le", "utf16le"}, current application's NSUTF16LittleEndianStringEncoding}, ¬
|
lns3.scpt
|
{{"utf-32", "utf32"}, current application's NSUTF32StringEncoding}, ¬
|
lns3.scpt
|
{{"utf-32be", "utf32be"}, current application's NSUTF32BigEndianStringEncoding}, ¬
|
lns3.scpt
|
{{"utf-32le", "utf32le"}, current application's NSUTF32LittleEndianStringEncoding}, ¬
|
lns3.scpt
|
{{"ascii"}, current application's NSASCIIStringEncoding}, ¬
|
lns3.scpt
|
{{"iso-2022-jp", "iso2022jp", "csiso2022jp"}, current application's NSISO2022JPStringEncoding}, ¬
|
lns3.scpt
|
{{"iso-8859-1", "latin1", "iso8859-1"}, current application's NSISOLatin1StringEncoding}, ¬
|
lns3.scpt
|
{{"iso-8859-2", "latin2", "iso8859-2"}, current application's NSISOLatin2StringEncoding}, ¬
|
lns3.scpt
|
{{"euc-jp", "u-jis", "eucjp", "ujis"}, current application's NSJapaneseEUCStringEncoding}, ¬
|
lns3.scpt
|
{{"macroman"}, current application's NSMacOSRomanStringEncoding}, ¬
|
lns3.scpt
|
{{"shift-jis", "s-jis", "sjis"}, current application's NSShiftJISStringEncoding}, ¬
|
lns3.scpt
|
{{"windows-1250", "windows1250", "cp1250"}, current application's NSWindowsCP1250StringEncoding}, ¬
|
lns3.scpt
|
{{"windows-1251", "windows1251", "cp1251"}, current application's NSWindowsCP1251StringEncoding}, ¬
|
lns3.scpt
|
{{"windows-1252", "windows1252", "cp1252"}, current application's NSWindowsCP1252StringEncoding}, ¬
|
lns3.scpt
|
{{"windows-1253", "windows1253", "cp1253"}, current application's NSWindowsCP1253StringEncoding}, ¬
|
lns3.scpt
|
{{"windows-1254", "windows1254", "cp1254"}, current application's NSWindowsCP1254StringEncoding}}
|
lns3.scpt
|
end _init
|
lns3.scpt
|
to getEncoding(textEncoding)
|
lns3.scpt
|
if _list is missing value then _init()
|
lns3.scpt
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.