text
stringlengths
0
15.7k
source
stringlengths
6
112
_error("normalize text", eText, eNumber, eFrom, eTo)
lns3.scpt
end transform text
lns3.scpt
to pad text theText to places textWidth using padText : (" ") adding whichEnd : (leading characters only)
lns3.scpt
set textWidth to _support's asIntegerParameter(textWidth, "to places")
lns3.scpt
set widthToAdd to textWidth - (theText's length)
lns3.scpt
if widthToAdd ≤ 0 then return theText
lns3.scpt
set padText to _support's asTextParameter(padText, "using")
lns3.scpt
set padSize to padText's length
lns3.scpt
if padText's length = 0 then _support's throwInvalidParameter(padText, "using", text, "Can’t be empty text.")
lns3.scpt
repeat while padText's length < (widthToAdd + padSize)
lns3.scpt
set padText to padText & padText
lns3.scpt
if whichEnd is leading characters only then
lns3.scpt
return (padText's text 1 thru widthToAdd) & theText
lns3.scpt
else if whichEnd is trailing characters only then
lns3.scpt
set padOffset to (theText's length) mod padSize
lns3.scpt
return theText & (padText's text (1 + padOffset) thru (padOffset + widthToAdd))
lns3.scpt
else if whichEnd is leading and trailing characters then
lns3.scpt
if widthToAdd > 1 then set theText to padText's text 1 thru (widthToAdd div 2) & theText
lns3.scpt
return theText & (padText's text (1 + padOffset) thru (padOffset + (widthToAdd + 1) div 2))
lns3.scpt
_support's throwInvalidConstantParameter(whichEnd, "adding")
lns3.scpt
_error("pad text", eText, eNumber, eFrom, eTo)
lns3.scpt
end pad text
lns3.scpt
to slice text theText from startIndex : (missing value) to endIndex : (missing value)
lns3.scpt
set theLength to theText's length
lns3.scpt
if theLength = 0 then
lns3.scpt
if startIndex = 0 then _support's throwInvalidParameter(startIndex, "from", integer, "Index can’t be 0.")
lns3.scpt
if endIndex = 0 then _support's throwInvalidParameter(endIndex, "to", integer, "Index can’t be 0.")
lns3.scpt
if startIndex is not missing value then
lns3.scpt
set startIndex to _support's asIntegerParameter(startIndex, "from")
lns3.scpt
if endIndex is missing value then
lns3.scpt
if startIndex < -theLength then
lns3.scpt
else if startIndex > theLength then
lns3.scpt
return theText's text startIndex thru -1
lns3.scpt
else if endIndex is missing value then
lns3.scpt
error "Expected ‘from’ and/or ‘to’ parameter but received neither." number -1701
lns3.scpt
if endIndex is not missing value then
lns3.scpt
set endIndex to _support's asIntegerParameter(endIndex, "to")
lns3.scpt
if startIndex is missing value then
lns3.scpt
if endIndex < -theLength then
lns3.scpt
else if endIndex > theLength then
lns3.scpt
return theText's text 1 thru endIndex
lns3.scpt
if startIndex < 0 then set startIndex to theLength + 1 + startIndex
lns3.scpt
if endIndex < 0 then set endIndex to theLength + 1 + endIndex
lns3.scpt
if startIndex > endIndex or startIndex < 1 and endIndex < 1 ¬
lns3.scpt
or startIndex > theLength and endIndex > theLength then return ""
lns3.scpt
if startIndex < 1 then
lns3.scpt
set startIndex to theLength
lns3.scpt
if endIndex < 1 then
lns3.scpt
set endIndex to 1
lns3.scpt
set endIndex to theLength
lns3.scpt
return text startIndex thru endIndex of theText
lns3.scpt
_error("slice text", eText, eNumber, eFrom, eTo)
lns3.scpt
end slice text
lns3.scpt
to trim text theText removing whichEnd : (leading and trailing characters)
lns3.scpt
if {whichEnd} is not in {leading characters only, trailing characters only, leading and trailing characters} then
lns3.scpt
_support's throwInvalidConstantParameter(whichEnd, "removing")
lns3.scpt
considering case, diacriticals, hyphens and punctuation but ignoring numeric strings and white space
lns3.scpt
if theText is "" then return "" -- check if theText is empty or contains white space characters only
lns3.scpt
set {startIndex, endIndex} to {1, -1}
lns3.scpt
if {whichEnd} is in {leading characters only, leading and trailing characters} then
lns3.scpt
repeat while character startIndex of theText is ""
lns3.scpt
set startIndex to startIndex + 1
lns3.scpt
if {whichEnd} is in {trailing characters only, leading and trailing characters} then
lns3.scpt
repeat while character endIndex of theText is ""
lns3.scpt
set endIndex to endIndex - 1
lns3.scpt
_error("trim text", eText, eNumber, eFrom, eTo)
lns3.scpt
end trim text
lns3.scpt
to _asLineBreakParameter(lineBreakType, parameterName) -- used by `join paragraphs` and `normalize line breaks`
lns3.scpt
if lineBreakType is OS X line breaks then
lns3.scpt
return linefeed
lns3.scpt
else if lineBreakType is classic Mac line breaks then
lns3.scpt
return return
lns3.scpt
else if lineBreakType is Windows line breaks then
lns3.scpt
return return & linefeed
lns3.scpt
_support's throwInvalidConstantParameter(lineBreakType, parameterName)
lns3.scpt
end _asLineBreakParameter
lns3.scpt
to _splitText(theText, theSeparator) -- used by `split text` to split text using one or more text item delimiters and current or predefined considering/ignoring settings
lns3.scpt
set delimiterList to _support's asList(theSeparator)
lns3.scpt
repeat with aRef in delimiterList
lns3.scpt
set contents of aRef to contents of aRef as text -- caution: AS silently ignores invalid TID values, so separator items must be explicitly validated to catch any user errors; for now, just coerce to text and catch errors, but might want to make it more rigorous in future (e.g. if a list of lists is given, should sublist be treated as an error instead of just coercing it to text, which is itself TIDs sensitive); see also existing TODO on TypeSupport's asTextParameter handler
lns3.scpt
_support's throwInvalidParameterType(aRef, "using", text, "list of text")
lns3.scpt
set AppleScript's text item delimiters to delimiterList
lns3.scpt
set resultList to text items of theText
lns3.scpt
end _splitText
lns3.scpt
to _splitPattern(theText, patternText) -- used by `split text` to split text using a regular expression as separator
lns3.scpt
if (count {patternText} each list) = 1 then set patternText to join text patternText using "|"
lns3.scpt
set asocPattern to _asNSRegularExpressionParameter(patternText, "at")
lns3.scpt
set asocMatchRange to ((asocMatchArray's objectAtIndex:i)'s rangeAtIndex:0)
lns3.scpt
set end of resultList to (asocString's substringWithRange:{location:asocNonMatchStart, |length|:asocMatchStart - asocNonMatchStart}) as text
lns3.scpt
set end of resultList to (asocString's substringFromIndex:asocNonMatchStart) as text
lns3.scpt
if resultList's length = 1 and resultList's item 1's length = 0 then return {} -- for consistency with _splitText(), where `text items of ""` returns empty list
lns3.scpt
end _splitPattern
lns3.scpt
to _joinText(theList, separatorText)
lns3.scpt
set AppleScript's text item delimiters to separatorText
lns3.scpt
set resultText to theList as text
lns3.scpt
_support's throwInvalidParameter(theList, "", list, "Expected list of text.")
lns3.scpt
end _joinText
lns3.scpt
to split text theText at theSeparator : (missing value) using matchFormat : (case insensitivity)
lns3.scpt
if theText's length = 0 then return {}
lns3.scpt
if theSeparator is missing value then -- if `at` parameter is omitted, trim ends then then split on whitespace runs, same as Python's str.split() default behavior (any `using` options are ignored)
lns3.scpt