Online Book Reader

Home Category

AppleScript_ The Definitive Guide - Matt Neuburg [189]

By Root 1548 0

tell application "TextEdit"

tell text of document 1

make new word at word 2 with data "howdy"

end tell

end tell

This code inserts a new folder inside folder 1:

tell application "Finder"

make new folder at folder 1

end tell

Then there's the with parameter. This is the most troublesome at all. Actually your choices are usually with data and with properties; some applications let you say only a particular one of these, but some let you say either (or both).

In the case of with data, you are probably setting some fundamental aspect of the new object's initial state; but you don't know, until you try it, exactly what aspect this will be. In the examples with BBEdit and TextEdit, it turns out that with a word, its data is its text.

In the case of with properties, what you're providing is a record; the properties and values you supply will be used to set the inital values of the corresponding properties in the newly created object. You don't usually need to supply an initial value for all of the new object's properties, but sometimes you have to supply an initial value for at least some of them. You might expect that you would be permitted to make a new, essentially blank object; the make command returns a reference to the new object, so you could then proceed to populate its properties afterwards. But some applications don't let you do this; they require you to provide certain initial values—and the problem is that you don't know which ones.

For example, the way to add an address to a Mailsmith message window is as follows:

tell application "Mailsmith"

tell message window 1

make new to_recipient with properties {address:"matt@tidbits.com"}

end tell

end tell

If you leave out the with properties parameter, you'll get a runtime error.

Selection


Another extraordinarily difficult concept to express in AppleScript is the selection , which is usually implemented through a selection property. The selection can be tricky to get and tricky to set.

Some applications give you no access to the selection at all. For example, in TextEdit there is no provision for learning what is selected. Similarly, Apple Mail lets you learn what messages are selected but not what text is selected in a message. This is a clear deficiency and should be regarded as a bug, as it is perfectly reasonable that you would want a script to operate on the selected text.

Of what should the selection be a property? Different applications have different philosophies about this. Be sure to look through the dictionary to see where the selection is implemented. The safest assumption is that there is an application-wide selection, and so it is the application class's selection property that is most likely to be appropriate. If an application has multiple windows, and you have selected something in several of them, only the selection in the frontmost window is the application-wide selection. But an application may also let you specify the selection in terms of some other physical container, such as a window. Thus, for example, BBEdit lets you work with the selection of a non-frontmost window; but the Finder does not.

When you ask for the selection, what kind of result is returned? The answer will vary from application to application, but what you hope for is a reference (or list of references). Learning what kind of object this is a reference to, and what you can do with this information, might require experimentation. If it is a list, you might have to cycle through its items to work further with them. For example, in Address Book, this doesn't work:

tell application "Address Book"

name of selection

-- error: Address Book got an error: Can't make name of selection into type reference

end tell

The selection is a list, and Address Book is not willing to distribute a property over the items in that list (see "Operations on Multiple References" in Chapter 11). You have to cycle through the list yourself:

tell application "Address Book"

set L to {}

repeat with aThing in (get selection)

set end of L to (get name of aThing)

end repeat

Return Main Page Previous Page Next Page

®Online Book Reader