AppleScript provides the text item delimiters property for use in processing text. This property consists of a list of strings used as delimiters by AppleScript when it coerces a list to text or gets text items from text strings. When getting text items of text, all of the strings are used as separators. When coercing a list to text, the first item is used as a separator.
In essence, you can set a delimiter (or a list of them) and then split or separate a string on it, creating a list of the resulting text items.
In this scenario, the scripts are splitting the text on the chevron character and creating a list of the resulting strings.
You can also rejoin items in a list around the delimiter by coercing the list with as text. That does not occur in this scenario since the purpose of the split is simply to get the length of each string rather than to alter it.
Here is a simple example to demonstrate changing every instance of the left chevron to a right chevron.
set str to "first second « third fourth « fifth sixth"
set text item delimiters to "«"
set strList to text items of str
--> {"first second ", " third fourth ", " fifth sixth"}
set text item delimiters to "»"
set str to strList as text
--> "first second » third fourth » fifth sixth"
An index is simply the position of an ordered element within its grouping. For example, the letter ‘e’ is the 5th character of the string ‘apple’, so its index is 5.
Getting the locations is fast.
Changing the font / color / size is slow
use AppleScript version "2.7"
use framework "Foundation"
use scripting additions
-- classes, constants, and enums used
property NSRegularExpression : a reference to current application's NSRegularExpression
property NSMutableArray : a reference to current application's NSMutableArray
property NSString : a reference to current application's NSString
--property thePattern : "(«|»)"
property thePattern : "(\\?)"
property theRegEx : missing value
property newFormatCharRef : missing value
set theRegEx to NSRegularExpression's regularExpressionWithPattern:thePattern options:0 |error|:(missing value)
set newFormatCharRef to {font:"Impact", color:{4386, 36250, 65437}, size:11.0}
tell application "Pages"
tell document 1
activate
set pageIndex to 0
set thePages to a reference to every page
repeat with aPage in thePages
set pageIndex to pageIndex + 1
-- log {"pageIndex:", pageIndex}
set theText to (a reference to body text)
set characterLocationlist to my getLocationsInText(theText)
repeat with i in characterLocationlist
try
set properties of character i of theText to newFormatCharRef
on error e
--if font or font--size changes push a located target character off the current page it will be located on a later page.
end try
end repeat
end repeat
end tell
end tell
on getLocationsInText(theText)
set theString to NSString's stringWithString:theText
set regexResults to theRegEx's matchesInString:theString options:0 range:{location:0, |length|:theString's |length|()}
set theRanges to (regexResults's valueForKey:"range")
set theLocations to NSMutableArray's new()
repeat with aRange in theRanges
set aRange to aRange as record
set aLocation to aRange's location
set aLocation to aLocation + 1
--(theLocations's addObject:aLocation)
(theLocations's insertObject:aLocation atIndex:0) -- add locations in reverse order
end repeat
return theLocations as list
end getLocationsInText
Unfortunately, it looks as if footnotes are not exposed to scripting in Pages. Short of attempting to do this with GUI scripting — which would be laborious and pretty fragile — I think you may be out of luck.
I know this is of no immediate help but, if you need to manipulate the content of documents like this regularly, Pages is probably not the right tool. Adobe InDesign, for instance, is scriptable to a pretty remarkable degree and even without resorting to scripting its find & replace and its paragraph and character styles are very powerful and could easily have solved this problem.