AppleScript_ The Definitive Guide - Matt Neuburg [102]
Target
At every moment in AppleScript code, you are speaking to some object. That object is the target , to which all messages will be sent, unless you specify otherwise. Knowing what object is the target, and how to specify a desired target, is very important to your successful use of AppleScript.
The implicit target is the current script or script object. (See "Handler Calls, Commands, and Script Objects" in Chapter 8.) In this code, the implicit target is the script itself:
count
In this code, the implicit target is the script object myScript:
script myScript
count
end script
There are three ways to specify an explicit target: as a direct object of a command , with a tell block, and with the of operator or one of its synonyms. These three ways may be combined to specify the complete target. (If they remind you of ways you can talk to a script object and access its top-level entities, they should; see Chapter 8.)
Direct Object
Most commands take at least one parameter. The unnamed parameter that directly follows the name of the command is the direct object. In the absence of any other target information, the target can occupy the place of the direct object.
In this example, we see the count message being sent to various targets, each of which interprets it in a different way:
script s
on count
return "1, 2, 3, ha ha ha"
end count
end script
count s -- "1, 2, 3, ha ha ha"
count "hello" -- 5
count {1, 2, 3} -- 3
count application "Finder" -- 32(the number of desktop items)
It is permitted to insert the word of between the command and the direct object (unless the command is get, set, or copy). It is also permitted to insert the word get before the command. This is rather confusing because it makes a command (such as count) look like an attribute, and I do not recommend talking this way.
count of "hello" -- 5
get count of "hello" -- 5
script s
display dialog "howdy"
end script
get run of s -- howdy, but no one ever talks like this
get display dialog of "howdy" -- howdy(now stop that!)
This of is related to the special of that can mark the first parameter when using prepositional parameters in a handler call (see Chapter 9). The 's operator is not a synonym.
Don't confuse a command's direct object with the target. The target can occupy the direct object slot even if the command itself does not take a direct object. For example, the quit command has no direct object; it is sent to the target, and that is what quits.
quit application "Finder"
Tell Block
A tell block is a block introduced by the keyword tell. Immediately after that keyword comes the target. Commands within the block are directed to the target, unless they specify a different target. A tell block consisting of a single command can be written as a single line, using the syntax tell target to. Things I say about a tell block apply to this variant as well.
Here are some of the earlier direct object examples rewritten to use a tell block. They could all be rewritten in the same manner:
tell "hello"
count -- 5
end tell
tell "hello" to count -- 5
tell application "Finder"
count -- 32
end tell
tell application "Finder" to count --32
An important