AppleScript_ The Definitive Guide - Matt Neuburg [111]
Applications can be extraordinarily touchy about how they expect an insertion location to look, with results differing from application to application. Here are some examples:
tell application "TextEdit"
tell text of document 1
make new word at after word 2 with data "not "
-- changes "this is a test" to "this is not a test"
end tell
end tell
tell application "TextEdit"
tell text of document 1
duplicate word 1 to end
-- changes "fair is foul and foul is " to "fair is foul and foul is fair"
end tell
end tell
tell application "TextEdit"
tell text of document 1
duplicate word 1 to beginning of word 3
-- changes "wonder of s" to "wonder of wonders"
end tell
end tell
tell application "TextEdit"
tell text of document 1
duplicate word 1 to word 7
-- changes "fair is foul and foul is foul" to "fair is foul and foul is fair"
end tell
end tell
tell application "Script Debugger"
move window 2 to beginning
-- bring the second window frontmost
end tell
That last example shows that this locution manipulates more than text.
Boolean Test
It may be possible to get a list of those elements that satisfy a boolean test . What you test may be a property of the target, or it may be the target itself. The test may involve any boolean operator (see "Comparison Operators" and "Containment Operators" in Chapter 15).
A boolean test also involves an index-based specifier: index, range, or every (or some). This is because the answer is a list, and you can ask for the whole list or for particular elements of it.
To specify elements by boolean test, start with an index-based specifier for a class and the keyword where (or whose), followed by a property of that class or the word it, followed by a boolean operator and any value that can function as that operator's second operand. In this context, the word it means the element to be tested. How you select among the various synonyms and options will probably depend on what feels most English-like to you:
tell application "Finder" to get files where name begins with "s"
tell application "Finder" to get every file where name begins with "s"
tell application "Finder" to get files where name of it begins with "s"
tell application "Finder" to get files where its name begins with "s"
tell application "Finder" to get files whose name begins with "s"
Those are all equivalent. The index-based specifier is the every specifier, with file as its class; the plural files is a synonym. Then comes where or whose. Now every file will be tested, and name means the name property of the file being tested; the words of it or its are redundant but harmless. The boolean operator is begins with, and its second operand is the string "s".
When a boolean test specifier tests the value of a boolean property, you can say it is (or it is not) and the name of the property. Doing so can make your expression more English-like (if the property name is an adjective); the following two formulations are equivalent, the latter being more English-like:
tell application "System Events"
get process 1 whose frontmost is true
get process 1 where it is frontmost
end tell
The keyword it is needed when an application has defined a property with the same name as a class, just as we saw earlier ("It"). The problem is troublesome, because there's no error; you just don't get the right answer. This comes up empty:
tell application "Microsoft Entourage"
get every POP account whose email address contains "matt" -- {}
end tell
Saying its disambiguates. You might also change whose to where for the sake of English-likeness, but you don't have to:
tell application "Microsoft Entourage"
get every POP account whose its email address contains "matt"
-- {POP account id 1...}
end tell
You definitely