Target

At every moment in AppleScript code, you are speaking to some object. That object is the target, to which, unless you specify otherwise, all messages will be sent. The target can be implicit, or you can specify an explicit target. 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. In this code, the implicit target of set is the script itself:

set x to 5

In this code, the implicit target of set is the script object myScript:

script myScript
        set x to 5
end script

There are two ways to specify an explicit target. Not coincidentally, they bear a strong resemblance to the two ways of accessing a script object’s top-level entities (Chapter 9). You can specify an explicit target:

  • With the of operator or its synonyms. A synonym for of is in. (I never use this.) Another synonym, for most purposes, is the apostrophe-ess operator: instead of saying x of y, you can say y's x.

    The of operator specifies the target for just a single expression, and overrides all other targets, explicit or implicit.

  • With a tell block. Instead of an actual block, one may apply tell to a single command (Section 12.3), but this is still just a special case of a tell block.

    In the context of a tell block, the object specified in the announcement is the default target for everything you say. This can be overridden by specifying a different target, using either an embedded tell ...

Get AppleScript: The Definitive Guide now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.