12.3. Controlling Menus
Earlier, you used FileMaker
Pro's do menu command to run an
action from the menu bar. Unfortunately, that command is specific to
FileMaker Pro; do menu won't
work in TextEdit, Photoshop, the Finder, and so on.
Luckily, you can use GUI Scripting to
extend
AppleScript's menu control to
any program. If you're sick of
Web site pop-up windows, for example, the following script will
choose the Safari
Block Pop-up Windows command from the
menu bar, all automatically:
--Part 1:
tell application "Safari"
activate
end tell
--Part 2:
tell application "System Events"
--Part 3:
tell process "Safari"
--Part 4:
click the menu item "Block Pop-Up Windows" of the menu "Safari" ¬
of menu bar 1
end tell
end tell
Here's how the script works:
 |
Bringing a program forward is the necessary first step in
any script that employs GUI Scripting.
|
|
 |
When you're using GUI Scripting, every AppleScript
command must go through System Events. The exception, of course, is
activate, which you should send directly to the
program you want to bring forward.
|
|
Part 3 directs your GUI Scripting
commands at Safari. The keyword process, as
described on Section 6.3.1, is
simply the way that System Events refers to a currently running
program. Part 4 is the meat of the script.
Here, you specify the menu command you want to run (Block Pop-Up
Windows) and the menu that it's inside of (Safari).
In other words, this command is just a fancy way of telling
AppleScript to run the
Safari
Block Pop-Up Windows command.
 |
Whenever you're commanding a menu with GUI
Scripting, you always have to append of menu bar
1 to the end of the command. That's
because, in theory, programs could have more than
one menu bar, and AppleScript needs to know which one
you're talking about. (In reality, of course,
programs on the Mac never have more than one menu bar, so
you'll always refer to menu bar
1.)
|
|
 |