Section 12.5. Deciphering Interface Hierarchies


12.5. Deciphering Interface Hierarchies

Whenever there's an interface element (like the Sticky Keys radio button) that's deep inside a window, your best bet is to use Apple's UIElementInspector tool. This powerful program, available from www.apple.com/applescript/uiscripting/downloads/uiinspector.dmg, gladly reveals the hierarchy of your windows, so you can figure out precisely how to script an interface element.

Once you've downloaded the program, copy it into your Applications folder. From there, follow these simple steps to script the Sticky Keys radio button:

  1. Double-click UIElementInspector in the Finder.

    The program opens but without a menu bar or Dock icon. You deal with UIElementInspector entirely through its mini-title-barred windows.

  2. Hover your mouse over the Sticky Keys "On" radio button in System Preferences.

    UIElementInspector displays bits of geeky information, telling you the radio button's name, size, position, and so on.

    If you want to inspect a different interface elementsay, a text field or a checkboxsimply hover your cursor over that element instead.

  3. Press -F10 to lock UIElementInspector's current display (Figure 12-6).

    That'll allow you to move the mouse around without worrying about changing what's in the Inspector window.

    For a more thorough explanation of UIElementInspector's features, check out www.apple.com/applescript/uiscripting/02.html.

  4. Examine the top lines of the Inspector window to discover the interface hierarchy for the button.

    You should see an indented list, like this:

    <AXApplication: "System Preferences">  <AXWindow: "Universal Access">   <AXTabGroup>    <AXRadioGroup>     <AXRadioButton: "On">

    In plain English, that means that the "On" radio button (<AXRadioButton>) is stored inside a group of radio buttons (<AXRadioGroup>), which itself is stored inside a tab group (<AXTabGroup>) inside the Universal Access window (<AXWindow>) of System Preferences.

    The "AX" prefix stands for "accessibility," Apple's term for features that make it possible to control your Mac without the keyboard and mouse.

    Figure 12-6. After you press -F10, UIElementInspector turns its text red to indicate that it's locked onto the current interface element. To unlock the programso you can inspect other interface elementssimply press -F10 again.


  5. Quit UIElementInspector.

    Since the program doesn't have a menu bar or Dock icon, just click the Inspector's upper-left close box instead.

  6. Using your newly discovered interface information, incorporate the appropriate GUI Scripting command into your script.

    In this example, the final script would look like the following:

    tell application "System Preferences"     activate end tell tell application "System Events"     tell process "System Preferences"         click menu item ¬             "Universal Access" of menu "View" of menu bar 1         delay 4         click the radio button "Keyboard" of the ¬             first tab group of window "Universal Access"         --This is it:         click the radio button "On" of the first ¬             radio group of the first tab group of ¬             the window "Universal Access"     end tell end tell

    Power Users' Clinic
    PreFab UI Browser

    UIElementInspector is a powerful tool for discovering how your windows are laid out, but it's certainly not the most user-friendly program Apple ever made. If you use UIElementInspector to decipher the organization of windows with, say, a dozen nested interface elements, you may find yourself going crazy trying to transcribe all the "AX" terms to an AppleScript.

    Luckily, there's another choice for discovering how to script interface elements. PreFab UI Browser (www.prefab.com/uibrowser/) has tons of power-user GUI Scripting features, but still manages to stay user-friendly. Here's just a sampling of what UI Browser can do:

    • Navigate deep into the menus of your programs, without you actually opening the menus. UI Browser has the almost spooky ability to inspect the interfaces of programs that aren't in front at the momentand to tell you how to script them.

    • Find the order of interface elements onscreen. For instance, some programs have several different tab groups within the same window. If you wanted to script a particular tab group, you'd have to direct the commands at the first tab group, the second tab group, and so onand UI Browser can tell you which tab group is which.

    • Write GUI Scripting code automatically. Using the AppleScript pop-up menu, you can have UI Browser write code that clicks, selects, and otherwise plays with your interface elementsall without typing a single command yourself. That's a welcome break from Apple's own UIElementInspector, which wouldn't write you a line of code even if you got down on your knees and begged.

    • Highlight interface elements onscreen. While UIElementInspector can do this too, there's no comparison between the two programs. For one thing, Prefab UI Browser can highlight interface elements of programs that are hidden, displaying where elements would be if the programs were visible at the moment. Plus, UI Browser can list interface elements you didn't even know existedlike invisible buttonsand reveal them to you onscreen. Taken together, these features let you not only discover new interface elements in your existing programs, but also control these interface elements from AppleScript.

    Unfortunately, all this power comes at a cost$55, to be exact. If you make GUI Scripting you life's work, that's well worth the price. However, if you're just a casual scripter, it might make more sense to stick with the free UIElementInspector.

    And on the subject of PreFab Software: take a look at PreFab's UI Actions technology (www.prefab.com/uiactions/). UI Actions are based on the same idea as folder actions; however, instead of running scripts when you add files to a folder or disk, a UI Action runs a script whenever you visit a particular Web site or bring a new TextEdit window forward (in other words, any time a program's interface changes). If you're a hard-core AppleScripter, the ability to trigger scripts straight from programs' interfaces is not to be missed.


    As shown here, you simply translate the hierarchy you discovered back in part 4 into an AppleScript command. Then, when you run your script, System Preferences comes forward, opens Universal Access, and turns on Sticky Keys.

    If you'd like to learn more about Sticky Keys and other keyboard-assisting features, visit www.apple.com/accessibility/physical/.

    And don't forget: a radio button isn't the only kind of interface element you can click. If there were a push-style button you wanted to click (like the reload button in Safari) you would simply call it a plain old button in your script.



AppleScript. The Missing Manual
AppleScript: The Missing Manual
ISBN: 0596008503
EAN: 2147483647
Year: 2003
Pages: 150

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