Accessing System Preference pane elements by Applescript

I'm new to Applescript, and I'm trying to figure out how to set a hot corner in the Expose pane of System Preferences. I've been searching for Apple documentation, but I can't seem to find a good reference page.

Does anyone know what I'm doing wrong here?

<code>
tell application "System Events"
tell process "System Preferences"
tell menu bar 1
tell menu "View"
click menu item "Exposé & Spaces"
delay 1
tell tab group 1
click radio button "Exposé"
tell popupbutton
tell menu 1
click menu item "Spaces"
end tell
end tell
end tell
end tell
end tell
end tell
end tell
</code>

Macbook Pro (silver), Mac OS X (10.5.5)

Posted on May 14, 2009 8:23 AM

Reply
8 replies

May 14, 2009 10:20 AM in response to g-pod

The script below should work.

tell application "System Preferences" to activate
tell application "System Events"
tell process "System Preferences"
tell menu bar 1
tell menu bar item "View"
delay 1
tell menu 1
click menu item "Exposé & Spaces"
end tell
end tell
end tell
tell window "Exposé & Spaces"
tell tab group 1
click radio button "Exposé"
tell group "Active Screen Corners"
tell pop up button 1 -- or 2 or 3 or 4
click
tell menu 1
click menu item "Start Screen Saver" -- or any other menu item
end tell
end tell
end tell
end tell
end tell
end tell
delay 1
keystroke "w" using command down
end tell

Good luck!

May 14, 2009 10:50 AM in response to Pierre L.

That worked, thanks!

Some questions on the approach, since I'm still learning:

1.) Is there a reason why we need to "*end tell*" "*menu bar 1*" after "*click menu item 'Exposé & Spaces*"' is done? (lines 4-11)

2.) What is the "window" in '*tell window "Exposé & Spaces"*'?

3.) Does "*tab group*" refer to the Expose | Spaces tab group? And is every tab actually a radio button underneath?

4.) How come we need to "*tell pop up button 1*" before a click, instead of "*click pop up button 1*"?

5.) And does every pop up button have only one menu nested inside ("*menu 1*")? (line 18)

6.) What's the general rule when to place delays?

And finally, is there a good reference on what each element is called? For example, I wouldn't have guessed that select dropdown as a popup button. I've been using UIElementInspector.app, but it's not telling me exactly what each thing is called (and instead says AXthis and AXthat).

I know it's a lot of questions, but you don't have to answer all of them. Thanks again!

May 14, 2009 11:04 AM in response to Pierre L.

I have another question. When I'm trying to set two pop up buttons right after each other, I get an error saying, "System Events got an error: Can’t get menu 1 of pop up button 2 of group "Active Screen Corners" of tab group 1 of window "Exposé & Spaces" of process "System Preferences". Invalid index."

<pre>
tell pop up button 2
click
tell menu 1
click menu item "Application Windows" -- or any other menu item
end tell
end tell
</pre>

How do I set multiple pop up buttons?

May 14, 2009 12:11 PM in response to g-pod

I'll answer just three of your questions:

1. Is there a reason why we need to "end tell" "menu bar 1" after "click menu item 'Exposé & Spaces"' is done? (lines 4-11)

The reason is that the menu item "Start Screen Saver" is not part or the same hierarchy than the menu item "Exposé & Spaces". The menu item "Start Screen Saver" belongs to a menu which belongs to a pop up button which belongs to the group "Active Screen Corners" which belongs to a tab group which belongs to window "Exposé & Spaces" which belongs to the application "System Preferences".

2. What is the "window" in '*tell window "Exposé & Spaces"*'?

The window "Exposé & Spaces" is the window that replaces the "System Preferences" window when you click the menu item "Exposé & Spaces" of the "View" menu.

4. How come we need to "tell pop up button 1" before a click, instead of "click pop up button 1"?

Because we tell pop up button 1 to click AND tell menu 1 to do something. We could also have written:

tell group "Active Screen Corners"
click pop up button 1 -- or 2 or 3 or 4
tell menu 1 of pop up button 1
click menu item "Start Screen Saver" -- or any other menu item
end tell
end tell

As for your other questions, here are some references:

For GUI Scripting, have a look at this Web page:
http://www.macosxautomation.com/applescript/uiscripting/index.html

You should also download and use "UIElementInspector 1.3":
http://developer.apple.com/samplecode/UIElementInspector/

But before all, to get a better understanding of AppleScript, you should look at these pages:
http://www.macosxautomation.com/applescript/learn.html
http://developer.apple.com/documentation/AppleScript/Conceptual/AppleScriptLangG uide/introduction/ASLR_intro.html

(From my own point of view, AppleScript is rather difficult to learn, and GUI Scripting still more.)

May 14, 2009 12:34 PM in response to g-pod

It's often necessary to wait until the end of the previous command's execution. The following lines work OK:

tell group "Active Screen Corners"
tell pop up button 1 -- or 2 or 3 or 4
click
tell menu 1
click menu item "Start Screen Saver" -- or any other menu item
end tell
end tell
delay 1
tell pop up button 2 -- or 3 or 4
click
tell menu 1
click menu item "Disable Screen Saver" -- or any other menu item
end tell
end tell
end tell

Jul 29, 2009 8:08 PM in response to Pierre L.

In a similar vein I am an absolute scripting neophyte (basically tried and failed several forays in the past) and I am looking to script or automate another preference pane. I use Cisco VPN to log into my work computer. Unfortunately it does not play nice with Back to My mac and won't load unless BTMM is turned off. Unfortunately doing this is a multistep process to turn it off before opening the VPN then a likewise laborious process to to turn it back on afterwards. . I thought this would be a perfect thing for automator but i can't see any commands pertaining to system preferences. Thus my (hopeful) fall back to AS. However I wouldn't even begin to know where to start

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

Accessing System Preference pane elements by Applescript

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.