DISCOVERING AN OBJECT'S HIERARCHY

UI elements are positioned in a hierarchy of other elements that make up the visual interface of an application. As with objects in scriptable applications, UI elements are referenced by their position in their object hierarchy. For example, a button could be in a scroll list in a view group in a sheet attached to a window.

In order to script a UI element, it is essential to know the exact series of objects that contain it. The discovery of an object's hierarchy can be done with a free utility called the UI Element Inspector, which can be downloaded here.

When launched, the UI Element Inspector presents a small floating palette that displays information about any object that the cursor is over. The target object's containment hierarchy is displayed at the top of the palette window. To script the target object, translate the hierarchy into AppleScript terms using the Processes Suite from the System Events dictionary, as shown in the following example.

The following example demonstrates how to translate an object's hierarchy into a script by scripting the Arrange menu item of the Finder's View menu.
  1. Close all windows in the Finder and launch the UI Element Inspector utility.
  2. Highlite but do not execute the menu item: View > Arrange > by Size
  1. Notice the data displayed at the top of the UI Element Inspector palette. The object containment hierarchy for the menu item is shown with indentation indicating possession.

    In this example, the application process Finder contains a menu bar that contains a menu item named View that contains a menu named View that contains a menu item named Arrange that contains a menu named Arrange that contains a menu item named by Size.


  1. Duplicate the object hierarchy in AppleScript using tell blocks for each container object. The targeted menu item is executed with a statement using the verb click.

tell application "Finder"
activate
end tell

tell application "System Events"
tell process "Finder"
tell menu bar 1
tell menu bar item "View"
tell menu "View"
tell menu item "Arrange"
tell menu "Arrange"
click menu item "by Size"
end tell
end tell
end tell
end tell
end tell
end tell
end tell


Extras
Those wishing to explore the more advanced features of the UI Element Inspector should try putting the cursor over a target object, such as a Finder window, and type the keys Command-F10 to put the UI Element Inspector in a UI lock mode.

In the forthcoming Locked on... palette, select the Highlight checkbox to place a pink highlight over the target object. Use the goto popup menu to discover and highlight the children or objects belonging to the target object. Their information will display in both palettes. To exit UI lock-mode, type Command-F10 again.



And for those desiring an easier, less technical means of generating script statements for UI elements, visit Prefab Software's website for the Prefab UI Browser, an easy-to-use UI scripting utility.

Next Page


TABLE OF CONTENTS

Introduction

Activating UI Scripting - Enabling the GUI Scripting architecture

UI Element Inspector - Discovering an object's heirarchy

Example Scripts - Example scripts and routines