12.4. Clicking ButtonsAnother useful feature of GUI Scripting is the ability to automate button clicks. In fact, if a program doesn't have a dictionary, this ability is often the only way you can use AppleScript with that program. System Preferences (the control panel hub of Mac OS X), for example, has such a measly dictionary that it's a common target for GUI-scripted button clicks. Plus, System Preference panes are chock full o' buttons, leaving you plenty of targets for your GUI scripts. For example, you can change your computer's network name by visiting System Preferences Sharing and clicking Edit. Or, if you change your computer's network name oftensay, to throw off your nosy spouse downstairsyou can automate the process with this script: tell application "System Preferences" activate end tell tell application "System Events" --Part 1: tell process "System Preferences" --Part 2: click the menu item "Sharing" of the menu "View" of menu bar 1 --Part 3: delay 4 --Part 4: click the button "Edit..." of window "Sharing" end tell end tell
When you run that script, System Preferences springs forward and happily opens its Sharing pane. The script then performs a virtual click on the Edit button, displaying a dialog sheet to let you change your Mac's network name. Here's how the script works its magic:
From now on, you don't have to remember how to change your Mac's network name; you can just run your script instead.
12.4.1. Clicking Tab ButtonsSome System Preference panes have all their options laid out in one screen (Appearance and Exposé, for example). Other preference panes, however, have multiple different screens, and you have to access each screen by clicking on different tabs (Figure 12-4). Therefore, before you can script such preference panes as International and Universal Access (which have multiple tabs) you need to know how to script the tabs themselves.
Fortunately, it's easy to use GUI Scripting for selecting a tab in a tab group. Say you wanted to open System Preferences Universal Access Keyboard tab: tell application "System Preferences" activate end tell tell application "System Events" tell process "System Preferences" --Part 1: click the menu item "Universal Access" of the menu "View" of menu bar 1 --Part 2: delay 4 --Part 3: click the radio button "Keyboard" of the first tab group ¬ of window "Universal Access" end tell end tell Here's how the script breaks down:
Of course, simply opening a tab in System Preferences is something a trained monkey could do. To get some real use out of your script, you should have it turn on Mac OS X's Sticky Keys feature, so you can press a special key (Command, Option, Control, or Shift) and have that key "stick" in place while you press other keys. That feature is convenient if you have pains in your fingers, for example, and can't hold down the keystroke -S comfortably (to select File Save). Using Sticky Keys, if you first pressed the key (and lifted your finger), you would see the cloverleaf symbol appear on your screenand then if you pressed the S key (and lifted your finger), you'd complete the keystroke. In other words, if Sticky Keys is turned on, you only have to type keystrokes in sequenceyou don't have to hold down all the keys at once. The only problem is, the Sticky Keys radio button is stuck deep inside your System Preferences panetoo deep, in fact, to figure out how to AppleScript the button on your own. All hope isn't lost, though; you can still use GUI Scripting to enable Sticky Keysprovided you read on. |