Section 24.2. Getting Started with Accessibility


24.2. Getting Started with Accessibility

The prerequisite for GUI scripting to work is that the Accessibility API must be turned on. This may be done through the "Enable access for assistive devices" checkbox in the Universal Access system preferences. Unless this checkbox is checked, the scripts in this chapter will fail.

Now examine System Events's dictionary; in particular, look at the Processes Suite. Here you'll find events such as click and keystroke and classes such as radio button and menu item. The classes in question are all UI element subclasses. So your task is to use these events to operate on these classes, as a way of simulating clicks and keystrokes in the target application. The target application itself is expressed as an application process element of System Events. Thus, AppleScript code that does GUI scripting will have a structure similar to the following:

 tell application "TextEdit" to activate tell application "System Events"     tell application process "TextEdit"         tell menu 1 of menu bar item "Format" of menu bar 1             click menu item 4         end tell     end tell end tell

If you try that script, you'll see that it toggles the format of the frontmost TextEdit document between plain text and rich text (RTF). And you can see why: it literally chooses the fourth menu item of the Format menu. Observe that the target here is not TextEdit! It's System Events. The phrase tell application process "TextEdit" is not the same as tell application "TextEdit"it isn't an application specifier, it's merely an element specifier sent to System Events as part of the chain of ofs and tells specifying the complete target (see "The Chain of Ofs and Tells" in Chapter 11).

The problem now, for any given task you'd like to perform by means of GUI scripting, is to express each desired interface element in terms of the UI element object model. This is not at all easy; ultimately, you must probably resign yourself to a certain amount of experimentation. There are, however, some utilities that can help you. These take advantage the fact that the Accessibility API can not only drive interface elements; it can also see them.


AppleScript

AppleScript itself can be used to drive System Events to use the Accessibility API to explore the interface of an application. Example utility scripts appear in Apple's global Script Menu (see "Script Runner" in Chapter 2), or you can find them on your hard disk at /Library/Scripts/UI Element Scripts. Some of these are scripts you can run; others are intended just to show you the sort of thing you can say.


UIElementInspector

This is an application from Apple that probes an interface. Confusingly, there is more than one version of this application floating around at Apple's site; download the most recent one, included with the source code at http://developer.apple.com/samplecode/UIElementInspector/UIElementInspector.html. It's clumsy to use, however, as you can focus on only one interface item at a time, and the interface item is described in Accessibility API terms, not in terms of System Events and AppleScript.


PreFab UI Browser

This inexpensive commercial utility is indispensable if you're going to be doing any serious GUI scripting. You can use a column browser or the mouse to reach the interface item you're interested in, and UI Browser tells you about the attributes of that item and the entire UI element hierarchy leading to it, and even generates AppleScript code for talking to it. Figure 24-1 shows how I used UI Browser to learn how to refer to the menu item of TextEdit for the preceding example code.

Figure 24-1. PreFab UI Browser exploring TextEdit's menu





AppleScript. The Definitive Guide
AppleScript: The Definitive Guide, 2nd Edition
ISBN: 0596102119
EAN: 2147483647
Year: 2006
Pages: 267
Authors: Matt Neuburg

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net