Online Book Reader

Home Category

AppleScript_ The Definitive Guide - Matt Neuburg [190]

By Root 1625 0

L

end tell

Sometimes, the selection property is not what you really want. If you're simply trying to get a reference to what's frontmost in the application, look for properties with "current" in their name, such as iTunes's current playlist. Similarly, in Entourage, the selection might be a list of references (to messages selected in a window that lists messages, for example), or it might be the selected text (as Unicode text, not a reference); but Entourage implements a current messages property that always returns a list of messages, which is far more likely to be useful.

Then there is the matter of how to set the selection. An application that implements selection will usually implement a select command to go with it. Most applications don't let you say "set selection," and those that do mean something else. For example, in BBEdit, "set selection" is a way of replacing the selected text. Microsoft Word , where the selection is often crucial for performing operations on text, provides many ways to manipulate the selection.

Idioms for Common Tasks


Think of AppleScript as an interface to an application, just like the interface you see on the screen. One is a programming interface, the other is a graphical interface. It is not always easy to see how the latter maps to the former. You are accustomed to doing things in an application's graphical interface, in what you think of as a simple, straightforward way. When you're using AppleScript, you often want to "translate" a common interface operation into the AppleScript language. The trouble is that the command repertory, the object model, and the dictionary are often structured quite differently from the graphical interface. Thus the object model is often not at all like the mental picture of the application you've built up from using it in the ordinary way. The verbs you think you need aren't there, or the verbs that are there don't do what you expect. This is probably somewhat inevitable, given the nature of AppleScript (the AppleScript interface to an application is necessarily different from the graphical interface) and the nature of the dictionary (it is a list of words, not to a how-to manual). But it's also very frustrating. Your mind thinks in terms of tasks; you're presented with some vocabulary. You're told some words, but not how to do things with them.

No matter how inevitable this disconnect may be, there is little doubt that some applications carry it much further than necessary. Eudora is a classic example. As we've already seen ("Make," earlier in this chapter), you can't create a new outgoing message with AppleScript in Eudora as simply as you would do it in the graphical interface; you have to say the magic words that tell Eudora where to create the new message, even though there is only one place where it could ever be meaningfully created. Similarly, deleting a message in Eudora is extraordinarily difficult. Once again, the simplicity of the graphical interface misleads you. You're used to simply selecting a message and deleting it (with the Delete key). This sounds like the delete event, so you try it:

tell application "Eudora"

delete message 1 of mailbox "Out"

-- error: Message 1 of mailbox 'Out' doesn't understand the delete message

end tell

So how on earth are you supposed to delete it? The solution, it turns out, is to move the message into the trash:

tell application "Eudora"

move message 1 of mailbox "Out" to end of mailbox "Trash"

end tell

Who would ever have thought of saying something like that? And the dictionary doesn't tell you to say it, so how are you supposed to find out?

Another good example is how you insert text into a BBEdit window. There is no insert verb, and make turns out to be unreliable. The best way, apparently, is to position the selection where you want to insert the text and then say "set selection to" the new text.

Events and Classes


A dictionary lists events (verbs) and classes (nouns), but it doesn't tell you what verbs apply to what nouns. The verb make creates a new object, but what objects

Return Main Page Previous Page Next Page

®Online Book Reader