text
stringlengths 0
15.7k
| source
stringlengths 6
112
|
---|---|
set asocPath to (current application's NSString's pathWithComponents:subPaths)
|
lns3.scpt
|
set fileExtension to _support's asTextParameter(fileExtension, "using file extension")
|
lns3.scpt
|
if fileExtension's length ≠ 0 then
|
lns3.scpt
|
set asocPath to asocPath's stringByAppendingPathExtension:fileExtension
|
lns3.scpt
|
if asocPath is missing value then _support's throwInvalidParameter(fileExtension, "using file extension", text, "Invalid file extension.")
|
lns3.scpt
|
return asocPath as text
|
lns3.scpt
|
_error("join path", eText, eNumber, eFrom, eTo)
|
lns3.scpt
|
end join path
|
lns3.scpt
|
to split path filePath at splitPosition : (last component)
|
lns3.scpt
|
set asocPath to current application's NSString's stringWithString:(_support's asPOSIXPathParameter(filePath, ""))
|
lns3.scpt
|
if splitPosition is last component then
|
lns3.scpt
|
return {(asocPath's stringByDeletingLastPathComponent()) as text, (asocPath's lastPathComponent()) as text}
|
lns3.scpt
|
else if splitPosition is file extension then
|
lns3.scpt
|
return {(asocPath's stringByDeletingPathExtension()) as text, (asocPath's pathExtension()) as text}
|
lns3.scpt
|
else if splitPosition is all components then
|
lns3.scpt
|
return (asocPath's pathComponents()) as list
|
lns3.scpt
|
_support's throwInvalidConstantParameter(matchFormat, "at")
|
lns3.scpt
|
_error("split path", eText, eNumber, eFrom, eTo)
|
lns3.scpt
|
end split path
|
lns3.scpt
|
property _ArgvUserError : 10000 -- error code used to indicate the shell script's user supplied invalid command line options (errors due to bugs in invalid option/argument definitions supplied by shell script author use standard AS error codes); need to decide what's a sensible code to use and document it in SDEF (unfortunately, `on error number ...` blocks only accept literal integer (for pattern matching) or identifier (for assignment) and don't allow a command as parameter, so there's no way to supply library-defined error numbers as 'named constants' via library-defined commands, e.g. `on error number (command line user error)`, that return the appropriate number)
|
lns3.scpt
|
script NoValue -- unique constant used to indicate no defaultValue property was given
|
lns3.scpt
|
property LF2 : linefeed & linefeed
|
lns3.scpt
|
property Indent1 : " "
|
lns3.scpt
|
property Indent2 : " "
|
lns3.scpt
|
to VT100(formatCode)
|
lns3.scpt
|
return (character id 27) & "[" & formatCode & "m"
|
lns3.scpt
|
end VT100
|
lns3.scpt
|
set theText to theList as text
|
lns3.scpt
|
to _asValueType(valueType)
|
lns3.scpt
|
if (count {valueType} each list) = 1 then -- list of text = enumeration of allowed values
|
lns3.scpt
|
if valueType's length = 0 or valueType's length ≠ (count valueType each text) then
|
lns3.scpt
|
error "Not a list of text." number -1700 from valueType to list
|
lns3.scpt
|
else -- must be type name
|
lns3.scpt
|
set valueType to valueType as class
|
lns3.scpt
|
return valueType
|
lns3.scpt
|
end _asValueType
|
lns3.scpt
|
to _unpackValue(theValue, definitionRecord)
|
lns3.scpt
|
set valueType to _asValueType(definitionRecord's valueType)
|
lns3.scpt
|
if valueType's class is list then
|
lns3.scpt
|
if {theValue} is not in valueType then
|
lns3.scpt
|
error "Not a valid option (" & _joinText(valueType, "/") & "): " & theValue number _ArgvUserError
|
lns3.scpt
|
set theResult to theValue -- note: the given value is returned as-is, so its case is not guaranteed to be same as in enumeration; client code should normalize value itself as necessary
|
lns3.scpt
|
else -- valueType is a type name (text, integer, boolean, etc)
|
lns3.scpt
|
if valueType is text then
|
lns3.scpt
|
set theResult to theValue
|
lns3.scpt
|
else if {valueType} is in {integer, real, number} then -- note: decimal numbers must be in canonical form
|
lns3.scpt
|
set asocFormatter to current application's NSNumberFormatter's alloc()'s init()
|
lns3.scpt
|
asocFormatter's setLocale:(current application's NSLocale's systemLocale())
|
lns3.scpt
|
set asocResult to asocFormatter's numberFromString:theValue
|
lns3.scpt
|
if asocResult is missing value then error "Not a valid number: " & theValue number _ArgvUserError
|
lns3.scpt
|
set theResult to asocResult as any
|
lns3.scpt
|
if valueType is integer then
|
lns3.scpt
|
if theResult mod 1 ≠ 0 then error "Not a valid integer: " & theValue number _ArgvUserError
|
lns3.scpt
|
set theResult to theResult as integer
|
lns3.scpt
|
else if {valueType} is in {«class furl», alias, file, POSIX file} then -- note: `file` is treated as synonym for `POSIX file` here, as actual 'file' object specifiers are both mostly pointless and much more problematic due to using HFS paths
|
lns3.scpt
|
if theValue does not start with "/" and theValue does not start with "~" then
|
lns3.scpt
|
set basePath to current application's NSFileManager's defaultManager()'s currentDirectoryPath()
|
lns3.scpt
|
if basePath is missing value then error "Can't expand relative file path (current working directory is unknown): " & theValue number _ArgvUserError
|
lns3.scpt
|
set theValue to (current application's NSString's pathWithComponents:{basePath, theValue})
|
lns3.scpt
|
set theResult to ((current application's NSString's stringWithString:theValue)'s stringByStandardizingPath()) as text as POSIX file
|
lns3.scpt
|
if valueType is alias then set theResult to theResult as alias
|
lns3.scpt
|
on error number -43 -- file not found
|
lns3.scpt
|
error "File path doesn’t exist: " & theValue number _ArgvUserError
|
lns3.scpt
|
else if valueType is boolean then -- may be used by boolean argument definitions (boolean options don't take a value)
|
lns3.scpt
|
if {theValue} is in {"true", "yes", "t", "y", "1"} then
|
lns3.scpt
|
set theResult to true
|
lns3.scpt
|
else if {theValue} is in {"false", "no", "f", "n", "0"} then
|
lns3.scpt
|
set theResult to false
|
lns3.scpt
|
error "Not ‘yes’ or ‘no’ (Y|N): " & theValue number _ArgvUserError
|
lns3.scpt
|
error "Invalid option/argument definition (not an allowed type)." number -1703 from (a reference to valueType of definitionRecord) to type
|
lns3.scpt
|
end _unpackValue
|
lns3.scpt
|
to _defaultValuePlaceholder(definitionRecord) -- given an option/argument definition record, returns the appropriate default placeholderValue according to valueType
|
lns3.scpt
|
if valueType's class is list then return "OPTION" -- (valueType is list of allowed text values, i.e. an enumeration)
|
lns3.scpt
|
return "TEXT"
|
lns3.scpt
|
else if {valueType} is in {integer, real, number} then
|
lns3.scpt
|
return "INTEGER"
|
lns3.scpt
|
return "NUMBER"
|
lns3.scpt
|
else if {valueType} is in {«class furl», alias, file, POSIX file} then
|
lns3.scpt
|
return "FILE"
|
lns3.scpt
|
else if valueType is boolean then
|
lns3.scpt
|
return "Y|N"
|
lns3.scpt
|
end _defaultValuePlaceholder
|
lns3.scpt
|
to _formatDefaultValue(definitionRecord) -- formats default value for inclusion in OPTIONS/ARGUMENTS section
|
lns3.scpt
|
set defaultValue to defaultValue of definitionRecord
|
lns3.scpt
|
if (count {defaultValue} each list) ≠ 0 and length of defaultValue = 1 then set defaultValue to item 1 of defaultValue
|
lns3.scpt
|
if (count {defaultValue} each text) ≠ 0 then
|
lns3.scpt
|
set defaultText to defaultValue
|
lns3.scpt
|
else if (count {defaultValue} each integer) ≠ 0 or (count {defaultValue} each real) ≠ 0 then
|
lns3.scpt
|
set defaultText to (asocFormatter's stringFromNumber:defaultValue) as any
|
lns3.scpt
|
else if (count {defaultValue} each «class furl») ≠ 0 or (count {defaultValue} each alias) ≠ 0 then
|
lns3.scpt
|
set defaultText to POSIX path of defaultValue
|
lns3.scpt
|
else if defaultValue is true then
|
lns3.scpt
|
set defaultText to "Y"
|
lns3.scpt
|
else if defaultValue is false then
|
lns3.scpt
|
set defaultText to "N"
|
lns3.scpt
|
return "Default: “" & defaultText & "”. "
|
lns3.scpt
|
end _formatDefaultValue
|
lns3.scpt
|
to _formatEnumeration(valueType)
|
lns3.scpt
|
copy valueType to enumList
|
lns3.scpt
|
repeat with textRef in enumList
|
lns3.scpt
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.