Finding Control and Menu Items for use in AppleScript User Interface Scripting

Dec 12, '11 07:30:00AM

Contributed by: squalene

OS 10.7 Lion seems to have brought about a renewed interest in AppleScript User Interface Scripting. A big part of the difficulty in getting a GUI script to work is figuring out how exactly to address the controls in an applications window.

Apple's Accessibility, which comes with Xcode, is some help with this, but in my hands, it's always been awkward to use for figuring out what code I need to write to click a particular check box in say, a System Preferences Pane.

Here's a bit of AppleScript I wrote years ago that delivers up easy to understand and correct lists of an application's window and menu items in a format that may easily be cut and pasted directly into Scripts.

-- Entire Contents Demo - mini
-- BP ages ago or so

-- This'll get all the controls and structures associated with an App's window and menus
-- In a form which is easily pasteable into your own scripts
-- and show them in the result pane below.
-- Copy that into a text editor and change commas to returns to get an easily  readable list.
-- The script can take a long time if there are LOTS of window items, such as
-- in the "music" pane of iTunes. It may even time out if you have a huge iTunes library
-- The script'll process most App's UI structures in under a minute

set appname to "System Preferences" -------------------------- Set this to the App you want to look at

set winstuff to "defaultval"
set menustuff to "defaultval"

tell application appname
end tell

tell application "System Events"
  tell process appname
    set winstuff to entire contents of front window
    set menustuff to entire contents of menu bar 1
  end tell
end tell
--return winstuff & "rrrr" & menustuff -- comment this out to get just winstuff
return winstuff -- comment this out too to get just menustuff
--return menustuff 
Pointing the code at 'System Preferences' with the 'Desktop & Screens Saver' pane open will return, along with 128 other UI objects, this line describing the slider which controls the delay before the screen saver activates:
slider 1 of group 1 of tab group 1 of window "Desktop & Screen Saver" of application process "System Preferences" of application "System Events"
With the proper form of address in hand, it's a simple matter to construct an AppleScript that changes the delay before the screen saver is activated:
tell application "System Events"
  set value of slider 1 of group 1 of tab group 1 of window "Desktop & Screen Saver" of application process "System Preferences" to 180
end tell
The script will return information on whatever application window is frontmost, be it a document window, or an obscure dialog box.

I've looked for similar code online, and it doesn't seem to be commonly available.

[crarko adds: I tested some of this out; as mentioned, be prepared to do some massaging of the data that is returned (in AppleScript Editor's Results window) to make it readable.

Note: comments indicate this also works in versions before Lion.]

Comments (15)

Mac OS X Hints