Online Book Reader

Home Category

AppleScript_ The Definitive Guide - Matt Neuburg [171]

By Root 1591 0
that application. But when a scripting addition makes the same kind of mistake, it conflicts everywhere, and there's nothing you can do about it (short of not using that scripting addition). You can't target a scripting addition explicitly, and you can't hide the scripting addition's terminology from your script. And an application developer, creating an application's dictionary, even by doing all the homework in the world, can't guess what third-party scripting additions you might install and what conflicts these might cause. Thus the real trouble lies in the nature of the scripting addition mechanism itself, which invites such clashes; this is one of the reasons why Apple discourages developers from writing scripting additions (Chapter 21).

Not surprisingly, there have historically been many instances of clashes caused by scripting additions. Thoughtful scripting addition developers give their terms deliberately improbable names to reduce the likelihood of clashes. Sometimes a user notices a clash and notifies the scripting addition's developer, who responds by creating a new version of the scripting addition with an altered dictionary. Nonetheless, there are probably still some scripting additions where terminology clash is simply part of the price of using them.

Here's a mysterious terminology clash that isn't solved by pipes:

local folder

set folder to 5

tell application "Finder"

set |folder| to 10

end tell

folder -- 5, not 10

The trouble is that folder is also defined in a scripting addition, as the name of a property. (It's a property of the file information record returned by the info for command. The scripting addition defines folder as 'asdr', but the Finder defines it as 'cfol'.) To use folder as a variable name successfully, if we are going to put pipes around it anywhere, we must put pipes around it everywhere, so that it isn't identified with this scripting addition property:

local |folder|

set |folder| to 5

tell application "Finder"

set |folder| to 10

end tell

|folder| -- 10

An infamous example for many years was this:

tell application "BBEdit"

tell text window 1

offset of "i" in (get contents of word 1)

-- compile-time error: access not allowed

end tell

end tell

The trouble was caused by a conflict between BBEdit's offset property and the offset scripting addition command. Consequently there was no straightforward way to use this scripting addition command while targeting BBEdit. Fortunately, recent changes in BBEdit's dictionary have effectively resolved the problem.

Nonsensical Apple Events


As we saw in a previous section ("Compile-time Error"), AppleScript will sometimes blow the whistle at compile time to indicate that you are using a term as the wrong "part of speech." This is extremely helpful. For example, you can't use a verb as a noun:

tell application "Finder"

get name of eject -- compile-time error: Expected expression but found command name

end tell

And you can't assign to a class:

tell application "Finder"

set container to "howdy"

-- compile-time error: Can't set «class ctnr» to "howdy". Access not allowed

end tell

You have to supply valid parameter names:

tell application "Finder"

duplicate x by y -- compile-time error: Expected end of line but found "by"

end tell

The duplicate command doesn't have a by parameter, and the compiler knows this.

You can't make an element specifier out of something that isn't a class name:

tell application "Finder"

get name 1 -- compile-time error: Expected end of line but found number

end tell

The compiler also displays some intelligence about singular and plural forms of a class name. The plural form of a class name is taken to be a synonym for the every element specifier; otherwise, if you use a plural where a singular is expected or vice versa, the compiler will usually change it for you, silently:

tell application "Finder"

folder -- folder (the class name)

folders -- {...}, a list of references to every folder

folders 1 -- compiles as folder 1

folder 1 thru 2 -- compiles as folders 1 thru

Return Main Page Previous Page Next Page

®Online Book Reader