text
stringlengths
0
15.7k
source
stringlengths
6
112
to HTTP status name statusCode
lns3.scpt
set statusCode to _support's asIntegerParameter(statusCode, "")
lns3.scpt
return (current application's NSHTTPURLResponse's localizedStringForStatusCode:statusCode) as text
lns3.scpt
_error("HTTP status name", eText, eNumber, eFrom, eTo)
lns3.scpt
end HTTP status name
lns3.scpt
property _support : a reference to script "TypeSupport"
lns3.scpt
_support's throwCommandError("LIBRARY_NAME", handlerName, eText, eNumber, eFrom, eTo)
lns3.scpt
to SOME HANDLER directParam USING usingParam FLAG flagParam : (false)
lns3.scpt
set directParam to _support's asTextParameter(directParam, "")
lns3.scpt
set usingParam to _support's asWholeNumberParameter(usingParam, "USING")
lns3.scpt
set flagParam to _support's asBooleanParameter(flagParam, "FLAG")
lns3.scpt
_error("SOME HANDLER", eText, eNumber, eFrom, eTo)
lns3.scpt
end SOME HANDLER
lns3.scpt
The 'support' section contains standard boilerplate code for rethrowing handler errors with the name of the library included; only the `_error` handler's "LIBRARY_NAME" text needs changed to contain the library's actual name.
lns3.scpt
Each public handler should also contain a `try` block that catches all errors occurring within that handler, and rethrow them via the supporting `_error` handler; again, "SOME HANDLER" should be changed to match the handler's actual name.
lns3.scpt
These boilerplate idioms help to compensate for AppleScript's own weak error reporting, which only indicates that an error has occurred but does not tell the user where.
lns3.scpt
to hasHandler(handlerRef) -- handlerRef must be `a reference to HANDLER of SCRIPT`
lns3.scpt
try -- horrible hack to check if a script object has a specific handler
lns3.scpt
handlerRef as handler -- dereference and type check; this raises -1700 on failure
lns3.scpt
on error number -1700 -- the referred-to script object slot doesn't exist, or isn't a handler
lns3.scpt
end hasHandler
lns3.scpt
to _throwInvalidParameter(theValue, parameterName, expectedType, errorDescription, errorNumber) -- private
lns3.scpt
if parameterName's length = 0 then
lns3.scpt
set parameterName to "direct"
lns3.scpt
set parameterName to "‘" & parameterName & "’"
lns3.scpt
error "Invalid " & parameterName & " parameter: " & errorDescription number errorNumber from theValue to expectedType
lns3.scpt
end _throwInvalidParameter
lns3.scpt
to throwInvalidParameter(theValue, parameterName, expectedType, errorDescription)
lns3.scpt
_throwInvalidParameter(theValue, parameterName, expectedType, errorDescription, -1703)
lns3.scpt
end throwInvalidParameter
lns3.scpt
to throwInvalidParameterIndex(theValue, parameterName) -- can't get list item
lns3.scpt
_throwInvalidParameter(theValue, parameterName, integer, "Index is out of range.", -1728)
lns3.scpt
end throwInvalidParameterIndex
lns3.scpt
to throwInvalidParameterType(theValue, parameterName, expectedType, expectedTypeName)
lns3.scpt
if (count {theValue} each reference) ≠ 0 then
lns3.scpt
set actualTypeName to " but received specifier"
lns3.scpt
set actualTypeName to " but received " & (theValue's class) -- include the value's type name in error message; note: this will display as raw four-char code when terminology isn't available, or may be a custom value in the case of records and scripts, but this can't be helped as it's a limitation of AppleScript itself
lns3.scpt
set actualTypeName to ""
lns3.scpt
throwInvalidParameter(theValue, parameterName, expectedType, "Expected " & expectedTypeName & actualTypeName & ".")
lns3.scpt
end throwInvalidParameterType
lns3.scpt
to throwInvalidConstantParameter(theValue, parameterName)
lns3.scpt
throwInvalidParameter(theValue, parameterName, constant, "Not an allowed constant.")
lns3.scpt
end throwInvalidConstantParameter
lns3.scpt
to rethrowError(libraryName, handlerName, eText, eNumber, eFrom, eTo, targetObjectName, partialResult) -- targetObjectName and partialResult should be `missing value` if unused; if eTo is unused, AS seems to be to pass `item`
lns3.scpt
if targetObjectName is missing value then
lns3.scpt
set eText to libraryName & " library can’t " & handlerName & ": " & eText
lns3.scpt
set eText to libraryName & " library’s " & targetObjectName & " can’t " & handlerName & ": " & eText
lns3.scpt
if partialResult is missing value then
lns3.scpt
error eText number eNumber from eFrom to eTo
lns3.scpt
error eText number eNumber from eFrom to eTo partial result partialResult
lns3.scpt
end rethrowError
lns3.scpt
to throwCommandError(libraryName, handlerName, eText, eNumber, eFrom, eTo)
lns3.scpt
error rethrowError(libraryName, handlerName, eText, eNumber, eFrom, eTo, missing value, missing value)
lns3.scpt
end throwCommandError
lns3.scpt
to throwMethodError(libraryName, targetObjectName, handlerName, eText, eNumber, eFrom, eTo)
lns3.scpt
error rethrowError(libraryName, handlerName, eText, eNumber, eFrom, eTo, targetObjectName, missing value)
lns3.scpt
end throwMethodError
lns3.scpt
to asNumber(theValue)
lns3.scpt
set theValue to theValue as anything -- make sure ASOC objects are coerced to AS equivalents
lns3.scpt
if (count {theValue} each list) = 1 and theValue's length = 1 then
lns3.scpt
set theValue to theValue's item 1 -- unwrap single-item list
lns3.scpt
if ((count {theValue} each number) = 1 or (count {theValue} each text) = 1 and theValue's length > 0) then
lns3.scpt
return theValue as number
lns3.scpt
on error number -1700 -- `theValue as anything` will fail if value is a record (presumably an AS bug)
lns3.scpt
error "Can’t coerce to number (not a valid number)." number -1700 from theValue to (number)
lns3.scpt
end asNumber
lns3.scpt
to asWholeNumber(theValue)
lns3.scpt
set theNumber to asNumber(theValue)
lns3.scpt
if theNumber mod 1 ≠ 0 then error "Can’t coerce to whole number (not an integer or non-fractional real)." number -1700 from theValue to (number)
lns3.scpt
if (theNumber < 0 and theNumber - 1 = theNumber) or (theNumber + 1 = theNumber) then error "Can’t coerce to whole number (can't be represented as real number without losing precision)." number -1700 from theValue to (number)
lns3.scpt
return theNumber
lns3.scpt
end asWholeNumber
lns3.scpt
to asInteger(theValue)
lns3.scpt
if theNumber mod 1 ≠ 0 then error "Can’t coerce to integer." number -1700 from theValue to (integer)
lns3.scpt
return theNumber as integer
lns3.scpt
end asInteger
lns3.scpt
to asText(theValue)
lns3.scpt
if (count {theValue} each list) = 1 then
lns3.scpt
if theValue's length = 1 then
lns3.scpt
set theValue to theValue's item 1 -- unwrap single-item list (note that nested lists are also disallowed)
lns3.scpt
if (count {theValue} each list) = 0 then return theValue as text
lns3.scpt
else if theValue's length = 0 then
lns3.scpt
error "Can’t coerce to text." number -1700 from theValue to (text)
lns3.scpt
return theValue as text
lns3.scpt
end asText
lns3.scpt
to asList(theValue)
lns3.scpt
if isNSObject(theValue) and (theValue's isKindOfClass:(current application's NSArray's |class|())) then -- if value is NSArray, coerce it to AS list
lns3.scpt
set theValue to theValue as list -- note: this also coerces all of the items in the array, which may not be ideal; however, if a handler is coercing to AS list then it probably wants to work with its contents as well, and will most likely want those to be AS values too
lns3.scpt
if (count {theValue} each list) = 1 then return theValue
lns3.scpt
return {theValue}
lns3.scpt
end asList
lns3.scpt
to asBooleanParameter(theValue, parameterName)
lns3.scpt
return theValue as boolean
lns3.scpt
throwInvalidParameterType(theValue, parameterName, boolean, "boolean")
lns3.scpt
end asBooleanParameter
lns3.scpt
to asIntegerParameter(theValue, parameterName)
lns3.scpt
return asWholeNumber(theValue) as integer
lns3.scpt
throwInvalidParameterType(theValue, parameterName, integer, "integer")
lns3.scpt
end asIntegerParameter
lns3.scpt
to asWholeNumberParameter(theValue, parameterName) -- this should be used instead of asIntegerParameter when non-fractional numbers are required but do not have to be specifically of integer type (which is limited in range to ±2^30)
lns3.scpt