text
stringlengths
0
15.7k
source
stringlengths
6
112
else if typeClass is constant then
lns3.scpt
theValue as constant is theValue then return true
lns3.scpt
else if typeClasses is «class ocid» then -- is it an AppleScriptObjC specifier?
lns3.scpt
return isNSObject(theValue)
lns3.scpt
else if (count {theValue} each application) ≠ 0 then
lns3.scpt
return typeClass is application
lns3.scpt
else if (count {theValue} each typeClass) ≠ 0 then -- other AS types can be reliably filtered using a `count` command -- TO DO: need to confirm this works for all types
lns3.scpt
_error("isValueOfType", eText, eNumber, eFrom, eTo)
lns3.scpt
end isValueOfType
lns3.scpt
to isNSObject(theValue)
lns3.scpt
if (count {theValue} each reference) = 0 then return false
lns3.scpt
return theValue's isKindOfClass:(current application's NSObject's |class|())
lns3.scpt
on error -- the above statement may fail in numerous ways, so all we can do is catch all errors and hope they were due to theValue not being an ASOC object rather than caused by something else
lns3.scpt
end isNSObject
lns3.scpt
to asNSObject(theValue)
lns3.scpt
return (current application's NSArray's arrayWithObject:theValue)'s objectAtIndex:0
lns3.scpt
end asNSObject
lns3.scpt
to asASObject(theValue)
lns3.scpt
if isNSObject(theValue) then
lns3.scpt
return theValue as anything -- coerce ASOC specifier to AS value, if possible
lns3.scpt
end asASObject
lns3.scpt
property _UnmatchedTextType : unmatched text
lns3.scpt
property _MatchedTextType : matched text
lns3.scpt
property _MatchedGroupType : matched group
lns3.scpt
_support's throwCommandError("Text", handlerName, eText, eNumber, eFrom, eTo)
lns3.scpt
to _asNSRegularExpressionParameter(patternText, parameterName) -- returns a regexp object with `(?imsw)` options enabled by default
lns3.scpt
return _support's asNSRegularExpressionParameter(patternText, ¬
lns3.scpt
((get current application's NSRegularExpressionCaseInsensitive) + ¬
lns3.scpt
(get current application's NSRegularExpressionAnchorsMatchLines) + ¬
lns3.scpt
(get current application's NSRegularExpressionDotMatchesLineSeparators) + ¬
lns3.scpt
(get current application's NSRegularExpressionUseUnicodeWordBoundaries)), parameterName)
lns3.scpt
end _asNSRegularExpressionParameter
lns3.scpt
property _tokens : "
lns3.scpt
\\\\ # match a backslash escape, followed by one of:
lns3.scpt
(?:(n|r|t|\\\\) # newline, return, tab or backslash character
lns3.scpt
| ( [0-9] ) # backslash + single digit (insertion index)
lns3.scpt
| \\{ ([^}{\\\\]*) \\} # backslash + any text in braces (insertion index or name)
lns3.scpt
)" -- note: \N{...}, \u1234, and \U00123456 escape sequences are processed separately (via ICU transforms); any other backslash sequences are unrecognized and left unchanged
lns3.scpt
to _parseTemplateText(templateText) -- convert template text containing backslashed special characters (\t, \u1234, \3, etc.) to a list of text items of form {text, index or name, ..., text}, where every 2nd item is an unprocessed back reference; used by the _parseSearchTemplate and _parseFormatTemplate handlers
lns3.scpt
set asocString to current application's NSString's stringWithString:templateText
lns3.scpt
set asocPattern to current application's NSRegularExpression's regularExpressionWithPattern:_tokens ¬
lns3.scpt
options:(get current application's NSRegularExpressionAllowCommentsAndWhitespace) |error|:(missing value)
lns3.scpt
set asocNonMatchStart to 0
lns3.scpt
set resultList to {}
lns3.scpt
set asocMatchArray to asocPattern's matchesInString:asocString options:0 range:{0, asocString's |length|()}
lns3.scpt
set concatNext to false
lns3.scpt
repeat with i from 0 to (asocMatchArray's |count|()) - 1
lns3.scpt
set asocMatch to (asocMatchArray's objectAtIndex:i)
lns3.scpt
set asocMatchRange to (asocMatch's rangeAtIndex:0)
lns3.scpt
set asocMatchStart to asocMatchRange's location()
lns3.scpt
set nonMatchText to ((asocString's substringWithRange:{asocNonMatchStart, (asocMatchStart - asocNonMatchStart)})'s stringByApplyingTransform:("Hex-Any/C;Name-Any") |reverse|:false) as text -- convert \u1234, \U00123456, \N{CHARNAME} escapes to specified characters (10.11+)
lns3.scpt
if concatNext then -- only item indexes/names should be at even indexes
lns3.scpt
set last item of resultList to last item of resultList & nonMatchText
lns3.scpt
set end of resultList to nonMatchText
lns3.scpt
set asocNonMatchStart to asocMatchStart + (asocMatchRange's |length|())
lns3.scpt
repeat with j from 1 to 3 -- _tokens defines 3 groups (index 0 is full match, so ignore that)
lns3.scpt
set asocGroupRange to (asocMatch's rangeAtIndex:j)
lns3.scpt
if asocGroupRange's |length|() > 0 then
lns3.scpt
set asocMatchString to (asocString's substringWithRange:asocGroupRange)
lns3.scpt
set itemText to asocMatchString as text
lns3.scpt
if j = 1 then
lns3.scpt
set last item of resultList to last item of resultList & (item (offset of itemText in "nrt\\") of {linefeed, return, tab, "\\"})
lns3.scpt
set concatNext to true
lns3.scpt
set end of resultList to itemText
lns3.scpt
set itemText to ((asocString's substringFromIndex:asocNonMatchStart)'s stringByApplyingTransform:("Hex-Any/C;Name-Any") |reverse|:false) as text -- trailing text; convert and append
lns3.scpt
if concatNext then
lns3.scpt
set last item of resultList to last item of resultList & itemText
lns3.scpt
return resultList
lns3.scpt
end _parseTemplateText
lns3.scpt
property _previousSearchTemplateText : ""
lns3.scpt
property _previousSearchTemplateParsedText : ""
lns3.scpt
to _parseSearchTemplate(templateText)
lns3.scpt
if templateText is not _previousSearchTemplateText then
lns3.scpt
set templateItems to _parseTemplateText(templateText)
lns3.scpt
repeat with i from 1 to templateItems's length
lns3.scpt
set itemText to item i of templateItems
lns3.scpt
if i mod 2 = 0 then -- substitute integer N with $N
lns3.scpt
set itemText to "$" & (itemText as integer)
lns3.scpt
set nextItem to item (i + 1) of templateItems
lns3.scpt
if nextItem's length > 0 and nextItem's character 1 is in "1234567890" then set itemText to itemText & "\\" -- make sure ICT template doesn't consume next digit
lns3.scpt
set item i of templateItems to itemText
lns3.scpt
else -- re-escape '/' and '$'
lns3.scpt
set asocString to _support's asNSString(itemText)
lns3.scpt
set item i of templateItems to (asocString's stringByReplacingOccurrencesOfString:"([\\$])" withString:"\\\\$1" options:(current application's NSRegularExpressionSearch) range:{0, asocString's |length|()}) as text
lns3.scpt
set _previousSearchTemplateParsedText to _joinText(templateItems, "")
lns3.scpt
set _previousSearchTemplateText to templateText
lns3.scpt
return _previousSearchTemplateParsedText
lns3.scpt
end _parseSearchTemplate
lns3.scpt
property _previousFormatTemplateText : ""
lns3.scpt
property _previousFormatTemplateParsedItems : "" -- list of form {text, index or name, ..., text}
lns3.scpt
to _parseFormatTemplate(templateText)
lns3.scpt
if templateText is not _previousFormatTemplateText then
lns3.scpt
set _previousFormatTemplateParsedItems to _parseTemplateText(templateText)
lns3.scpt
set _previousFormatTemplateText to templateText
lns3.scpt
return _previousFormatTemplateParsedItems's items
lns3.scpt
end _parseFormatTemplate
lns3.scpt
to _matchInfoRecord(asocString, asocMatchRange, textOffset, recordType)
lns3.scpt
set foundText to (asocString's substringWithRange:asocMatchRange) as text
lns3.scpt
set nextTextOffset to textOffset + (length of foundText) -- calculate the start index of the next AS text range
lns3.scpt
return {{class:recordType, startIndex:textOffset, endIndex:nextTextOffset - 1, foundText:foundText}, nextTextOffset}
lns3.scpt