Online Book Reader

Home Category

AppleScript_ The Definitive Guide - Matt Neuburg [103]

By Root 1508 0
feature of a tell block is that it causes terminology within it to be resolved using the dictionary of the target application. Thus you can say things that would be illegal (without trickery) as a simple direct object.

sleep application "Finder" -- compile-time error, because the word "sleep"

is not understood

tell application "Finder" to sleep --snore...

The role of a tell block in resolution of terminology, and the trickery you can employ to resolve terminology without one, is discussed in detail in Chapters 19 and 20.

Of


Objects, as we have already seen, can have attributes. The only way, ultimately, to access an object's attributes is through some object that you can see directly. The way to express the relationship between the object that you can see directly and the attribute you want to access is with the of operator.

A synonym for of is in. (I never use this.) Another synonym, for most purposes, is the 's operator: instead of saying x of y, you can say y 's x.

(Do not confuse the of operator here with the of that can optionally appear before the direct object of a command.)

The of operator can appear in a target specifier either in a direct object or in the announcement line of a tell block. For example:

open file 1 of application "Finder"

tell file 1 of application "Finder" to open

open application "Finder"'s file 1

tell application "Finder"'s file 1 to open

The Chain of Ofs and Tells


Material nested within a tell block is joined by an implicit of to the target specified in the announcement line of the tell block. (This device doesn't always work, and I'm being deliberately vague about some of the details; I'll provide them in "Get" and "It," later in this chapter.) So the previous example can be rewritten like this:

tell application "Finder"

open file 1

end tell

This is in fact the canonical and most frequently used form. In the earlier forms, the entire target (with of) appears either after tell or after open. Here, the target is effectively divided into two: half of it appears in the announcement line in the tell block (application "Finder") and the other half appears as the direct object of the open command (file 1). Each half is a partial target . AppleScript joins them with of to form the complete target .

This form has two great advantages , explaining why it is the most frequently used:

Resolution of terminology

A tell block causes the terminology inside it to be resolved in accordance with the target application's dictionary. Thus you can say things in a tell block that you can say in no other way, because the terms you're using are not part of AppleScript itself and would cause a compile-time error outside a tell block.

Multiple commands to the same target

A tell block is a block, so it can contain more than one command. It is very frequently the case that you want to give more than one command to the same target or partial target, so a tell block is the most convenient construct. For example:

local n, c

tell application "Finder"

tell folder 1

tell file 1

set n to name

set c to comment

end tell

end tell

end tell

The chain of ofs needed to form a complete target is often longer than just two parts. In this case, you can divide the target into partial targets in whatever way is convenient: each part by itself in a nested tell block, or some parts joined together with of. These snippets all do the same thing:

tell application "Finder"

tell folder 1

tell file 1

open

end tell

end tell

end tell

tell application "Finder"

tell file 1 of folder 1

open

end tell

end tell

tell application "Finder"

open file 1 of folder 1

end tell

tell application "Finder"

tell folder 1

open file 1

end tell

end tell

(You'll notice that I didn't try to crowd any more of the target onto the first line; I didn't say tell folder 1 of application "Finder", for example. This expression is syntactically legal, but it won't compile, because the term folder is defined in the Finder's dictionary and must appear within a tell block targeting the Finder.)

Return Main Page Previous Page Next Page

®Online Book Reader