Online Book Reader

Home Category

AppleScript_ The Definitive Guide - Matt Neuburg [187]

By Root 1439 0

tell application "GraphicConverter"

set s to "feathers:Users:mattneub:Desktop:joconde"

save window 1 in s as PICT

end tell

Interestingly, the AppleScript 'aeut' resource contains an alias or string class, presumably intended exactly for this situation—that is, it exists just so that a dictionary has a way to tell the user that an alias or a string is acceptable as a parameter. GraphicConverter's dictionary fails to take advantage of this. However, the real problem here is a shortcoming in the underlying dictionary format, which makes it hard to express alternative value types. (The new sdef format goes some way towards solving this.)

Then there's the matter of the result returned from a command. Sometimes a result's value type will be documented incorrectly. For example, the StandardAdditions dictionary says that do shell script returns a string, but it returns Unicode text (and the distinction can be crucial). It says that choose URL returns a URL; it returns a string. StuffIt Expander's dictionary contains just one entry—the verb expand—so wouldn't you think that the developers could be bothered to write it truthfully? Well, they couldn't. The dictionary says that this verb returns an integer representing the number of files that were successfully expanded. It doesn't. Because of this falsehood in the dictionary, a simple script that should take minutes to write can take hours (I speak from experience).

Sometimes a command will be documented as not returning any result when in fact it does, or vice versa. The Finder's reveal command is an example; the dictionary does not say it returns any value, but it actually returns a reference to the item revealed. This sort of thing is crucial to know, especially as it can affect the value of the implicit result of a line of your code, or even of a handler or the script as a whole.

A related problem is that the dictionary doesn't distinguish between a reference and a record as returned value types (see "Records," earlier in this chapter). This can make a big difference to your script. As a careful programmer you will probably want to know whether what you have is a reference, and whether fetching a property of it will send an Apple event. Yet experimentation is the only way to find out.

It also doesn't help matters that the very same command can return different types of value under different circumstances. A common source of error is a construct of this form:

tell application "Finder"

repeat with d in (get every disk)

-- do something here

end repeat

end tell

The problem is that every disk returns a list unless there is only one disk, in which case it returns a reference to a single disk object. Thus, this script can break in a subtle way: if you have more than one disk, you're cycling through disks; if you have just one disk, you're cycling through top-level folders. And you may have no way to find this out! You can test your script until you're blue in the face, believe that it works fine, and distribute it to others, only to learn later that it mysteriously breaks on someone else's computer.

Wrong Parameter Details


Aside from their value types, command parameters are all too often described incorrectly in the dictionary. First, is a parameter optional ? The dictionary has a place for this information, but developers sometimes get it wrong. Second, if a parameter is optional, what is the default? (That is, what will happen if the parameter is omitted?) There is no formal provision for answering this question, but there's a place for a comment on each parameter, and you'd think the developers would answer it here. But sometimes they don't.

For example, the Finder's dictionary says that when you're using the make command, the at parameter is compulsory, not optional. That just isn't true; this works:

tell application "Finder" to make Finder window

With iTunes's add command, the to parameter is optional. If you omit it, what will this file be added to? You can probably guess—but you shouldn't have to. The dictionary should tell you what this command

Return Main Page Previous Page Next Page

®Online Book Reader