Get preference panel names for UI Scripting

Mar 23, '04 09:34:00AM

Contributed by: kyliu99

Apple's GUI Scripting for AppleScript allows you to automate many applications that do not have usable AppleScript dictionaries. For example, you can use GUI Scripting to automate the process of toggling certain settings in System Preferences that do not have keyboard shortcuts (e.g., the "Set Display to Grayscale/Color" option in Universal Access). The way to script System Preferences works is to activate the application and then set the "current pane" to the pane whose settings you want to modify. This is usually done with a snippet like:

tell application "System Preferences"
  activate
  set current pane to pane "com.apple.preference.universalaccess"
end tell
But how do you get the name of the preference pane you want to change? As you can see, the "name" you must use is not the obvious name visible to the user in System Preferences. It is true that you can do without the name if you simply index into the visible window -- but this is not a reliable method (scripts may fail randomly) and your scripts may need to be rewritten if you install new preference panes or if you pass your script to your friends who have a different collection of preference panes than you do. Using the name is much more portable and reliable. Apple does not seem to follow a consistent pattern for the names. The "Accounts" preference pane, for example, is named "com.apple.preferences.users" (note the "preferences" instead of "preference").

In order to get this name that's useful for GUI Scripting, you need to navigate to where the preference pane is actually located (usually in one of /System -> Library -> PreferencePanes, /Library -> PrefereencePanes, or ~/Library -> PreferencePanes), control-click on the .prefPane file and select "Show Package Contents." Then navigate into the "Contents" folder and open up the Info.plist file; there you will find a key called "CFBundleIdentifier" whose value is the name you want.

Once you've managed to get GUI Scripting to select the preference pane you want, the rest of the scripting can be done in the usual way. As an example, here's the full script for toggling gray scale display.


tell application "System Preferences"
  activate
  set current pane to pane "com.apple.preference.universalaccess"
end tell

tell application "System Events"
  if UI elements enabled then
    tell tab group 1 of window ¬
     "Universal Access" of process "System Preferences"
      click button 4 ¬
       (* may be "Set Display to Grayscale" or "Set Display to Color" *)
    end tell
  else
    tell application "System Preferences"
      activate
      set current pane ¬
       to pane "com.apple.preference.universalaccess"
      display dialog ¬
       "UI element scripting is not enabled. Check \"Enable access for assistive devices\""
    end tell
  end if
end tell

delay 2

tell application "System Preferences"
  quit
end tell

Comments (5)


Mac OS X Hints
http://hints.macworld.com/article.php?story=20040317131326880