Section 12.4. Clicking Buttons


12.4. Clicking Buttons

Another 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

To type the three dots after "Edit" in part 3, press Option-semicolon. Don't simply press the period key three times.

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:

  • Part 1 tells System Events that you're interesting in scripting the interface of System Preferences (not Safari, like you did in the previous script).

  • Part 2 performs a virtual click on the View Sharing menu command. This part opens the Sharing pane of System Preferences.

  • Part 3 pauses for 4 seconds as System Preferences loads and displays your pane.

If you have a fast computer, your System Preference panes load quickly, so you can pause for a shorter period of time than 4 seconds. Similarly, if you have an absolutely ancient Mac, you'd be better off making your script pause for slightly more than 4 seconds while your preference pane loads.

  • Part 3 performs a virtual click on the Edit button in your Sharing pane. You'll see the dialog sheet shown in Figure 12-3, which lets you give your Mac a new network name.

From now on, you don't have to remember how to change your Mac's network name; you can just run your script instead.

Figure 12-3. You can enter letters, numbers, and hyphens in your Mac's network name, but no other symbols. The .local ending is common to all network names, so you can't delete it.


12.4.1. Clicking Tab Buttons

Some 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.

Figure 12-4. To access all the settings in certain preference panes, you have to navigate a tab group.


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:

  • Part 1 selects the View Universal Access command from System Preferences's menu, bringing forward the Universal Access pane.

  • Part 2 pauses as System Preferences loads and displays the pane.

  • Part 3 performs a virtual click on the Keyboard tab, bringing its options forward for you to see (Figure 12-5). Note how System Events refers to individual tabs: they're called radio buttons, in memory of old car radios that let you select only one button at a time.

Figure 12-5. The System Preferences Universal Access Keyboard tab is full of helpful tools for people who have difficulty typing. Try dragging the Acceptance Delay slider, for example, if you accidentally hold keys down too long. If you drag the slider to Long, Mac OS X will give you extra time to lift your finger before it starts repeating the letter your pressed.


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.



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