Unlocking SystemPreferences with a script ...

You might try something like this:

tell application "System Preferences"
	activate
	reveal anchor "Options" of pane id "uk.co.dssw.powermanager.preferencepane"
end tell
tell application "System Events"
	tell process "System Preferences"
		repeat until exists window "Power Manager"
			delay 0.5
		end repeat
		try
			click button "Click the lock to make changes." of group 2 of window "Power Manager"
			delay 0.5
				tell application "System Events"
						tell process "SecurityAgent"
							set value of text field 1 of group 1 of window 1 to "My Name"
							set value of text field 2 of group 1 of window 1 to "My Password"
							delay 0.5
							keystroke return -- clicks OK button
							
							-- perform further actions here --
							
						end tell
					end tell
		on error -- forces authentication if padlock is already unlocked
			tell application "System Events"
				tell process "System Preferences"
					repeat until exists window "Power Manager"
						delay 0.5
					end repeat
				end tell
			end tell
			click button "Click the lock to prevent further changes." of group 2 of window "Power Manager"
			delay 0.5
			click button "Click the lock to make changes." of group 2 of window "Power Manager"
			delay 0.5
			tell application "System Events"
				tell process "SecurityAgent"
					set value of text field 1 of group 1 of window 1 to "My Name"
					set value of text field 2 of group 1 of window 1 to "My Password"
					delay 0.5
					keystroke return -- clicks OK button
					
					-- perform further actions here --
					
				end tell
			end tell
		end try
	end tell
end tell

Notes:

The script takes into account the possibility that the Power Manager’s window may already be unlocked. For this reason the try statement is included. On error, the script will force authentication and allow the script to complete as if the window was locked to begin with. Without this consideration, the preference pane would open as expected, but the script itself would stall.

As to your concern for security, I would suggest replacing each instance of “My Name” and “My Password” with their real values and save the script as a Run Only application. This would prevent anyone with prying eyes from opening the saved script in the Script Editor and thus learning the administrator’s password.

Also, be patient while the value of text field 2 (Password) is populated. This can take several seconds. Good luck.

Hi andmr,

You worked hard … Thank you.

Is your script compatible with Tiger ? I cannot load it in my Script Editor. Compiling it gives me a «Syntax error: End of line, etc …». Are you sure I there is no statement missing ?

Regards.

Robert

My mistake … quotes were missing after my password. It works perfectly. Thank you so much. You are a Pro.

Regards.

Robert

Thanks to the help of many, this is the result of my work. I hope it could be helpful to others.

Since Power Manager (PM) is scriptable, it was easy to edit the app by scripting it. My problem was trying to edit a system preference pane that is locked (lockpad).

My way out of that problem what to work through the «user interface». I found a very interesting app for this purpose: Prefab UI Browser (follow this link: http://www.prefab.com/uibrowser). Very clever and well done app. I also got help from this thread.

I wrote the script to edit PM to my wishes and then inserted handlers created to «unlock» and «lock» PM’s padlock. Here are the handlers:


on UnlockPowerManager()
	tell application "System Preferences"
		launch
		reveal anchor "Options" of pane id "uk.co.dssw.powermanager.preferencepane"
	end tell
	tell application "System Events"
		tell process "System Preferences"
			repeat until exists window "Power Manager"
				delay 0.5
			end repeat
			if exists (button [i]"Pour modifier, cliquez sur le cadenas." of group 2 of window "Power Manager"[/i]) then
				click button [i]"Pour modifier, cliquez sur le cadenas." of group 2 of window "Power Manager"[/i]
				delay 0.5
				tell application "System Events"
					tell process "SecurityAgent"
						set value of text field 1 of group 1 of window 1 to adminName
						set value of text field 2 of group 1 of window 1 to adminPassword
						delay 0.5
						click button "OK" of group 2 of window "Authentification"
					end tell
				end tell
			end if
		end tell
	end tell
end UnlockPowerManager


on LockPowerManager()
	tell application "System Preferences"
		launch
		reveal anchor "Options" of pane id "uk.co.dssw.powermanager.preferencepane"
	end tell
	tell application "System Events"
		tell process "System Preferences"
			repeat until exists window "Power Manager"
				delay 0.5
			end repeat
			click button [i]"Pour empêcher les modifications, cliquez ici."[/i] of group 2 of window "Power Manager"
			delay 0.5
		end tell
	end tell
end LockPowerManager

As for retreiving securely your “adminName” and “admin Password”, here is another little handler that will do the job simply. It will pass the datas from a key of your Keychain app to your script:

on AdminDatas()
	tell application "Keychain Scripting"
		set adminKey to first generic key of current keychain where name is "nameOfYourAdminKey"
		set adminName to account of adminKey
		set adminPassword to password of adminKey
	end tell
end AdminDatas 

All tested on 10.4.11 and working … Insert those handlers where ever you need them and zip.

Please be patient when the «UnlockPowerManager» handler passes the «adminName» and «adminPassword» to «SecurityAgent». It is fully automatic, but a little bit long. Practice patience … It will be doubly rewarding. You will have to modify the name of the 2 buttons clicked by the handler. Mine are in french …

If you have any idea to make those handlers simpler, just drop a comment …

Hope this helps.

Robert

Hi,

Late thoughts …

Am I right to say that executing UI scripts requires that the «current user» is also the «active user» ? Am I also right to say that UI scripts executes only if there is no screensaver running and if all windows are manually accessible ?

Thanks in advance.

Robert