AppleScript_ The Definitive Guide - Matt Neuburg [188]
Make
Some common commands are particularly troublesome, and none more so than the verb make, used to create objects in AppleScript. No action is so fundamental as creating something—a window, a document, a mail message, a piece of text—and yet nothing in AppleScript is more difficult to do. The make command typically has the same basic set of parameters in every application, yet it is implemented in a bewildering variety of ways (sometimes it seems that every application implements it differently) and usually little or no assistance is provided by the dictionary.
Tip
The make command appears in the dictionary with its first parameter preceded by new. But in actual fact you can omit the word new; what follows the word make, where the direct object would go, is taken to be the new parameter. The dictionary fails to express this fact, which is hard-coded into the inner workings of AppleScript itself.
The first question is what to make. It's going to be a class belonging to the target application, and it will almost surely be a class that functions as an element in the application's object model. Nevertheless, for a given act of creation, it is not always clear what class you should try to make. You might think, for example, that to make a new email message in Mailsmith —I mean a new outgoing message, one that you intend to populate with an address, a body, and so forth, so as to send it later—you'd ask to make a new message; but it turns out that this creates a new incoming message (which makes no sense whatsoever—why in the world would you ever want to do such a thing?). The way to make a new outgoing message is to ask for a new message window. Nothing in the dictionary would lead you to this solution. Similarly, the way you create a new window in AppleWorks isn't to ask for a new window, which just gets you an incomprehensible error message, but to ask for a new document. The way you create a new window in the Finder isn't to ask for a new window but to ask for a new Finder window.
Often (but not always) you must also say where to create the new object. The dictionary lists this parameter:
at location reference --the location at which to insert the element
Every application seems to have a different idea of what constitutes an appropriate location. (See also the section "Relative" in Chapter 11.) In Eudora, for example, if you're trying to make an outgoing message, it turns out that the place to create it is at the end of the "Out" mailbox:
tell application "Eudora"
make new message at end of mailbox "Out"
end tell
This seems particularly nutty. In Eudora's interface, it's easy to make a new outgoing message: you just choose Message → New Message. You don't have to say where to make this new message! You just say you want one, and Eudora makes it in some sensible place. This is exactly what I'm trying to do in AppleScript; surely I should be able to say simply "make new message" and Eudora should create the outgoing message in precisely the same manner. But that's not the case. You must say the magic words or you won't get any new message. And don't expect the dictionary to tell you what the magic words are, either.
With Cocoa applications, the at parameter must typically refer to a collection (sometimes imaginary) of the same things you're trying to make one of. For instance, in the outliner NoteTaker , to make a new outline entry, you have to say something like this:
tell application "NoteTaker"
tell current notebook
tell current page
make new entry at beginning of entries
end tell
end tell
end tell
If you target the wrong object, or if you don't say at beginning of entries, you get an error, or nothing happens, or NoteTaker crashes.
Not only the at parameter, but also the meaning of at, varies from application to application. This code inserts a new word after word 2:
tell application "BBEdit"
tell window 1
make new word at word 2 with data "howdy"
end tell
end tell
This code inserts a new word replacing word 2: