text
stringlengths
0
15.7k
source
stringlengths
6
112
return asWholeNumber(theValue)
lns3.scpt
throwInvalidParameterType(theValue, parameterName, number, "integer or non-fractional real")
lns3.scpt
end asWholeNumberParameter
lns3.scpt
to asNumberParameter(theValue, parameterName)
lns3.scpt
return asNumber(theValue)
lns3.scpt
throwInvalidParameterType(theValue, parameterName, number, "number")
lns3.scpt
end asNumberParameter
lns3.scpt
to asRealParameter(theValue, parameterName)
lns3.scpt
return asNumber(theValue) as real
lns3.scpt
throwInvalidParameterType(theValue, parameterName, real, "real")
lns3.scpt
end asRealParameter
lns3.scpt
to asTextParameter(theValue, parameterName) -- TO DO: should lists be rejected for safety? (while coercing numbers and dates to text is at least predictable within a process's lifetime, coercing list to text is dependent on whatever TIDs are set at the time so can't be guaranteed even to do that)
lns3.scpt
return asText(theValue)
lns3.scpt
throwInvalidParameterType(theValue, parameterName, text, "text")
lns3.scpt
end asTextParameter
lns3.scpt
to asDateParameter(theValue, parameterName)
lns3.scpt
return theValue as date -- note that this fails for anything except date or NSDate (while it would be possible to try `date theValue` as well, it's probably best not to as AS's text-to-date conversion is locale-sensitive, so an ambiguous date string such as "12/11/10" would produce unpredictable results rather than fail outright)
lns3.scpt
throwInvalidParameterType(theValue, parameterName, date, "date")
lns3.scpt
end asDateParameter
lns3.scpt
to asListParameter(theValue, parameterName) -- takes parameterName, even though it's unused, for consistency with other `asTYPEParameter` handlers, reducing risk of typos
lns3.scpt
return asList(theValue)
lns3.scpt
end asListParameter
lns3.scpt
to asRecordParameter(theValue, parameterName)
lns3.scpt
return theValue as record
lns3.scpt
throwInvalidParameterType(theValue, parameterName, record, "record")
lns3.scpt
end asRecordParameter
lns3.scpt
script RequiredValue -- e.g. if asOptionalRecordParameter's `defaultRecord` parameter is {foo:RequiredValue, bar:missing value}`, the `foo` property is required but the `bar` property is optional (default: `missing value`); any other properties will raise an "unrecognized properties" error
lns3.scpt
to asOptionalRecordParameter(theValue, defaultRecord, parameterName)
lns3.scpt
set theRecord to theValue as record
lns3.scpt
set defaultRecord to defaultRecord & {class:record}
lns3.scpt
set fullRecord to theRecord & defaultRecord
lns3.scpt
if fullRecord's length > defaultRecord's length then throwInvalidParameter(theValue, parameterName, record, "Record contains one or more unrecognized properties.")
lns3.scpt
if (fullRecord as list) contains {RequiredValue} then throwInvalidParameter(theValue, parameterName, record, "Record is missing one or more required properties.")
lns3.scpt
if {fullRecord's class} is not in {record, defaultRecord's class} then throwInvalidParameter(theValue, parameterName, record, "Not a record of class " & defaultRecord's class & ".") -- TO DO: record class needs to be passed as text as coercing type class symbol to text will give raw chevron syntax only
lns3.scpt
return fullRecord
lns3.scpt
end asOptionalRecordParameter
lns3.scpt
to asScriptParameter(theValue, parameterName) -- TO DO: should take scriptName parameter that describes script object's purpose (e.g. "sort comparator script object")
lns3.scpt
return theValue as script
lns3.scpt
throwInvalidParameterType(theValue, parameterName, script, "script")
lns3.scpt
end asScriptParameter
lns3.scpt
to asTypeParameter(theValue, parameterName)
lns3.scpt
return theValue as type class
lns3.scpt
throwInvalidParameterType(theValue, parameterName, type class, "type")
lns3.scpt
end asTypeParameter
lns3.scpt
to asPOSIXPathParameter(theValue, parameterName)
lns3.scpt
if (count {theValue} each text) = 1 then
lns3.scpt
return theValue
lns3.scpt
return POSIX path of (theValue as «class furl»)
lns3.scpt
throwInvalidParameterType(theValue, parameterName, text, "POSIX path")
lns3.scpt
end asPOSIXPathParameter
lns3.scpt
to asNSString(theText) -- parameter must be text
lns3.scpt
return (current application's NSString's stringWithString:theText)
lns3.scpt
end asNSString
lns3.scpt
to asNSMutableString(theText) -- parameter must be text
lns3.scpt
return (current application's NSMutableString's stringWithString:theText)
lns3.scpt
end asNSMutableString
lns3.scpt
to asNormalizedNSString(theText) -- parameter must be text
lns3.scpt
return (current application's NSString's stringWithString:theText)'s decomposedStringWithCanonicalMapping()
lns3.scpt
end asNormalizedNSString
lns3.scpt
to asNSRegularExpressionParameter(theText, flagOptions, parameterName)
lns3.scpt
set asocPattern to current application's NSRegularExpression's regularExpressionWithPattern:asNormalizedNSString(theText as text) options:flagOptions |error|:(missing value) -- NSRegularExpression doesn't return detailed error descriptions, so just use generic error description below
lns3.scpt
throwInvalidParameterType(theText, parameterName, text, "text")
lns3.scpt
if asocPattern is missing value then throwInvalidParameter(theText, parameterName, text, "Not a valid pattern.")
lns3.scpt
return asocPattern
lns3.scpt
end asNSRegularExpressionParameter
lns3.scpt
to asNSLocaleParameter(localeCode, parameterName)
lns3.scpt
if localeCode is "none" then
lns3.scpt
return current application's NSLocale's systemLocale()
lns3.scpt
else if localeCode is "current" then
lns3.scpt
return current application's NSLocale's currentLocale()
lns3.scpt
set localeCode to localeCode as text
lns3.scpt
throwInvalidParameterType(localeCode, parameterName, "‘no locale’, {constant, text}, ‘current locale’, or text")
lns3.scpt
if localeCode is not in (current application's NSLocale's availableLocaleIdentifiers() as list) then throwInvalidParameter(localeCode, parameterName, {constant, text}, "Unknown locale name: “" & localeCode & "”.")
lns3.scpt
return current application's NSLocale's localeWithLocaleIdentifier:localeCode -- (locale codes are case-insensitive)
lns3.scpt
end asNSLocaleParameter
lns3.scpt
to asNSURLParameter(urlText, parameterName)
lns3.scpt
set asocURL to (current application's NSURL's URLWithString:asTextParameter(urlText, parameterName))
lns3.scpt
if asocURL is missing value then throwInvalidParameter(urlText, parameterName, text, "Not a valid URL.") -- NSURL requires RFC 1808
lns3.scpt
return asocURL
lns3.scpt
end asNSURLParameter
lns3.scpt
to nativeTypeOfValue(theValue)
lns3.scpt
if (count {theValue} each reference) ≠ 0 then -- avoid implicit dereferencing
lns3.scpt
return reference
lns3.scpt
else if (count {theValue} each application) ≠ 0 then -- avoid implicit dereferencing (we don't want `application NAME` value being treated as an object specifier root, i.e. null)
lns3.scpt
return application
lns3.scpt
else if (count {theValue} each record) ≠ 0 then -- ignore custom `class` property
lns3.scpt
return record
lns3.scpt
else if (count {theValue} each script) ≠ 0 then -- ignore custom `class` property
lns3.scpt
return script
lns3.scpt
return class of theValue
lns3.scpt
end nativeTypeOfValue
lns3.scpt
to isValueOfType(theValue, typeClasses) -- performs a 'VALUE is-a CLASS' test, which is trickier than it sounds since AppleScript has neither classes nor `is-a` operator
lns3.scpt
if (count {typeClasses} each list) = 0 then set typeClasses to {typeClasses}
lns3.scpt
repeat with aRef in typeClasses
lns3.scpt
set typeClass to contents of aRef as class
lns3.scpt
on error number -1700 -- TO DO: move to asTypeClassParameter
lns3.scpt
error throwInvalidParameterType(typeClass, "is type", class, "type class or list of type class")
lns3.scpt
if typeClass is class or typeClass is type class then -- TO DO: this could be problematic (AS is murky on distinction between `class` and `type class`, treating them as synonymous when used in `as` operation but not when comparing them; be aware that library's SDEF causes `type class` keyword to reformat as `type` keyword (for added confusion)
lns3.scpt
if (count {theValue} each reference) = 0 and ¬
lns3.scpt
theValue as class is theValue then return true
lns3.scpt