Section 11.6. It


11.6. It

The keyword it represents the target. (In situations where you would say of it after a word, you may say its before that word instead.)

The keyword it can be useful in helping you understand who the target is. It can also be useful as an explicit target, in situations where AppleScript would otherwise misinterpret your meaning.

This example shows it used while debugging, to make sure we understand who the target is:

 tell application "Finder"     tell folders         it -- every folder of application "Finder"     end tell end tell

Sometimes when you get yourself deep in a nest of tell blocks (see "The Chain of Ofs and Tells," earlier in this chapter), it can be helpful for referring to the target one level up. For example, this code opens file 1 of folder 1 and also opens folder 1 itself:

 tell application "Finder"     tell folder 1         open file 1         open it     end tell end tell

You have already seen ("Accessing Top-Level Entities" in Chapter 8) the need for it when accessing a script object's top-level entities within a tell block addressed to the script object. When targeting an attribute of a scriptable application, there is generally no need for it used in this way. Unlike a script object, an application has a dictionary, so AppleScript knows when you're saying the name of an attribute of that application. For example, the Finder has an attribute home; there is no need for its to tell AppleScript that we mean the Finder's home rather than a variable in scope:

 set home to "Ojai" tell application "Finder"     get home -- folder "mattneub" of folder "Users"... end tell

In fact, here the problem is more likely to be how to refer to the variable home from within this tell block. The next section ("Me") solves the problem.

However, there is one situation where it is needed when targeting an application. It may happen that an application's dictionary gives a property (a kind of attribute) and a class the same name. Applications really shouldn't do this, but in fact they do it quite often. Without it, such a term may be treated as the class, causing the script to malfunction. For example:

 tell application "Finder"     tell folder 1         get container -- container, a class     end tell end tell

This was not the desired result. To get the value of the container property of a folder, we must use its or (what amounts to the same thing) the of operator:

 tell application "Finder"     tell folder 1         get its container -- folder "Desktop" of...     end tell     get container of folder 1 -- folder "Desktop" of... end tell

This problem is particularly insidious, because there is usually no error, so you're mystified when the script doesn't behave as you expect. The following script is intended to change all instances of "o" to "a" in Word. The script compiles and runs, but the change is not performed:

 tell application "Microsoft Word"     tell find object of selection         set content to "o"         tell replacement             set content to "a"         end tell         execute find replace replace all     end tell end tell

The trouble is that replacement is both a property of the find object and the name of a class. The solution is to change the fourth line to this:

         tell its replacement

Sometimes when a script is being troublesome I make both get and it explicit everywhere just to eliminate misunderstandings as a possible source of error. For example, I might rewrite the previous script like this:

 tell application "Microsoft Word"     tell (get find object of its selection)         set its content to "o"         tell (get its replacement)             set its content to "a"         end tell         execute find replace replace all     end tell end tell

You'd be amazed by how many problems vanish before a little extra verbosity of this sort.




AppleScript. The Definitive Guide
AppleScript: The Definitive Guide, 2nd Edition
ISBN: 0596102119
EAN: 2147483647
Year: 2006
Pages: 267
Authors: Matt Neuburg

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net