System Preferences: get and restore current pane?

I’m writing a script that changes the settings in the Keyboard pane of System Preferences. Because I like to be polite, I would like to test whether the user has a different pane already open in System Preferences, and, if so, restore the original pane after I change a setting the the Keyboard pane.

I know that I can set the current pane this way:

tell application "System Preferences" to set current pane to pane "com.apple.preference.keyboard"

But I cannot find a way to put the name of the current pane (if any) into a variable. This fails:

set originalPane to current pane

and it also fails if I try “to the name of the current pane” or “the id of the current pane”.

Is there any way to do this?

Many thanks for any help.

Hi,

current pane holds an object of System Preferences or it’s empty (nil) if the overview is displayed.
You cannot restore the current anchor because it’s not accessible by AppleScript unless you’re able to get the information also with GUI scripting

try this, it saves the current pane (if valid)
selects the General pane and switches back after 2 seconds.
If no pane is selected, System Preferences will be quit


tell application "System Preferences"
	set currentPane to current pane
	set flag to true
	try
		currentPane -- throws an error if current pane is empty
	on error
		set flag to false
	end try
	reveal anchor "Main" of pane id "com.apple.preference.general"
	delay 2
	if flag then
		set current pane to currentPane
	else
		quit
	end if
end tell

Stefan,

Your code works perfectly! Thank you!

One last question: if System Preferences is set to “Show All” when I run this script, can I make it return to “Show All” instead of quitting?

I have tried some old code that says to use “click button ‘Show All’ of group 1 of group 2 of tool bar 1” but that doesn’t seem to work under Lion.

Thank you again!

LOL, I just discovered this is pretty easy


if flag then
	set current pane to currentPane
else
	set show all to true
end if

You could probably use it also instead of the error method

Perfect again! Thank you twice!