Online Book Reader

Home Category

AppleScript_ The Definitive Guide - Matt Neuburg [102]

By Root 1497 0
defined by some particular scriptable application. This is unfortunate, because such classes are much harder to work with than, say, a list. A list is always a list, but two classes with the same name, defined by two different applications, might well be nothing like one another. The Finder has a folder class and Entourage has a folder class, but a Finder folder and an Entourage folder have virtually nothing in common; they have totally different sets of attributes. Indeed, both the Finder and Entourage themselves belong to the application class, but they have very little in common too. And when it comes to messages, things are even worse; the Finder knows very well what messages it is legal to send to one of its folders, but the Finder's dictionary does little or nothing to tell you what they are. There will be much more groaning about this in Chapter 20.

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

Return Main Page Previous Page Next Page

®Online Book Reader