Unlocking SystemPreferences with a script ...

Hi,

Is there a way to unlock the system preferences with a script or a shell script ? By security, I would use Keychain scripting to acess admin name and password.

Is that possible ?

Thanks in advance.

Robert Lespérance

Hi Robert,

as far as I know it’s only possible with GUI scripting,
but you can change a lot of settings with shell scripts without the GUI

Hi StefanK,

Your are obvioulsy everywhere in this forum. To AppleScripting, I consider my self an adventurous novice. To shell and GUI scripting I know nothing except copying and pasting scripts.

So with your generous help, I would consider looking into shell and GUI scripting.

Do I understand that it is also possible with shell scripting ? Can you point a ressource to start. Funny enough, I didn’t find anything in this forum.

Regards.

Robert

As GUI and shell scripting must be very specified, because it depends on the purpose,
it would be easier to come to know what you are going to accomplish

I am scripting Power Manager (PM). PM is an system preference pane that can create/modify/delete events to put the computer to sleep or awake it. It is a very interesting tool.

I wrote a script that adds/modifies/deletes events by scripting the engine behind PM. This engine is called «Power Manager Scripting» (PMS). This engine is hidden. It is locked and needs admin name and password before accepting modifications to events.

I tried to find a way to unlock PMS by talking to PM’s developper, but couldn’t yet find a way to do it. Because unlocking System Preferences seems to unlock all locked preferences (Network, Startup, Date and time, etc …), I want to script that unlocking process in order for my modifications to be authorized.

I already have the Keychain Scripting part, that will retrieve admin name and password and pass it to the other part of my script, this thread is for. Here is a handler for the Keychain part:

on adminDatas()
	tell application "Keychain Scripting"
		set admKey to first generic key of current keychain where name is "myKeySessionOrWhatever"
		set admName to account of admKey
		set admPassword to password of admKey
	end tell
end adminDatas

Is unlocking System Preferences possible ?

Regards.

Robert

It’s quite strange, I downloaded PM to test it and I’m able to use all scripts without any authentication.
Do you run all the things in a managed account without admin privileges?

Anyway, here’s a script, which opens the PM-PrefPane and unlocks it, you have to add your routine to retrieve the password


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
		click button "Click the lock to make changes." of group 2 of window "Power Manager"
	end tell
	repeat until exists window 1 of process "SecurityAgent"
		delay 0.5
	end repeat
	tell process "SecurityAgent"
		tell window 1
			tell scroll area 1 of group 1
				set value of text field 1 to "user"
				set value of text field 2 to "pass"
			end tell
			click button "OK" of group 2
		end tell
	end tell
end tell

Hi StefanK,

If you can add, modify or delete events in PM without authentication, as far as I know, it is because your system preferences’s padlock is unlock in some other system preference. Unlocking the padlock in any system preference unlocks the padlock of all system preferences.

As for your script, thank you. I will give it a try and post the result in this forum. Giving all that time to help us is very generous. Hope you enjoy it yourself.

Regards.

Robert

Here is the script I am testing:

property admName : ""
property admPassword : ""

adminDatas()
-- say admName & admPassword

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
		click button "Pour modifier, cliquez sur le cadenas." of group 2 of window "Power Manager"
	end tell
	repeat until exists window 1 of process "SecurityAgent"
		delay 0.5
	end repeat
	tell process "SecurityAgent"
		tell window 1
			tell scroll area 1 of group 1
				set value of text field 1 to admName
				set value of text field 2 to admPassword
			end tell
			click button "OK" of group 2
		end tell
	end tell
end tell

-- Accéder aux données de l'administrateur
on adminDatas()
	tell application "Keychain Scripting"
		set admKey to first generic key of current keychain where name is "AccesAdministrateur"
		set admName to account of admKey
		set admPassword to password of admKey
	end tell
end adminDatas

I checked and adminDatas returns the admName and admPassword correctly to the script. I get an error when executing from the ScriptEditor. The authentification window opens but no name and password is passed to the input fields. Then in the background the Script Editor window opens an error message: System events error: «NSReceiverEvaluationScriptError:4»

this script works on my machine
and I can use the scripts of Power Manager regardless of the locked state

I am with 10.4.11 on an iMac G5. Are you using Leopard ? Could that be the reason of the script execution problem ?

Do you mean that you can add, modify or delete «events» in the Power Manager preference pane, even if the padlock in that preference pane is locked ?

Model: iMac G5 - 1.6 mgz
AppleScript: 2.1.1
Browser: Safari 525.18
Operating System: Mac OS X (10.4)

Yes, this could be the problem, the UI elements might be different

I was wrong, the behavior is exactly as you described, authorization is required

Hi, StefanK,

I didn’t find a solution to the problem of the GUI script. Where can I find information on the meaning and use of the GUI vocabulary ? Maybe that could help fix that in my Tiger environment.

Thanks again. Regards.

Robert

Sorry, I run Leopard on my machine and it is impossible to test a GUI script for Tiger on a Leopard environment, because detecting the UI elements is a lot of trial and error.
But you do it by yourself using a tool like UIElementInspector

I will dig into UIElementInspector possibilities. Thank you.

i have customized modifier keys in system preferences>>Keyboard and mouse>> keyboard.
and i frequently need to toggle between the customized ones and default ones. so i decided to use
UI element inspector. when i press “keyboard and mouse” in sys preferences window i get this
<AXApplication: “System Preferences”>
<AXWindow: “System Preferences”>

<AXButton: “Keyboard &
Mouse”>

Attributes:
AXRole: “AXButton”
AXRoleDescription: “button”
AXHelp: “(null)”
AXEnabled: “1”
AXFocused (W): “0”
AXParent: “”
AXWindow: “<AXWindow: “System Preferences”>”
AXTopLevelUIElement: “<AXWindow: “System Preferences”>”
AXPosition: “x=586 y=208”
AXSize: “w=82 h=63”
AXTitle: “Keyboard &
Mouse”

Actions:
AXPress - press

how do i put this into apple script? plz help me get started with it.

Hi Lance,

I cannot help you. I am trying to understand that my self. It would be better for you to start a new thread for your inquiry.

Regards.

Robert

I have gotten a little bit further. Modifying StefanK’s code to adat it to Tiger with code found in another MacScripter thread …


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
		click button "Pour modifier, cliquez sur le cadenas." of group 2 of window "Power Manager"
	end tell
	repeat until exists window 1 of process "SecurityAgent"
		delay 0.5
		my authenticate_changes()
		delay 4
		keystroke tab
		keystroke (ASCII character of 31)
	end repeat
end tell


on authenticate_changes()
	tell application "System Events" to tell window "Authentification" of process "SecurityAgent"
		tell group 1
			set value of text field 1 to "myAdminName"
			set value of text field 2 to "myAdminPassword"
		end tell
		click button "OK" of group 2
	end tell
end authenticate_changes

The script stops at the «Authentification» window. It does not type the adminName and adminPassword in the textfield. It just waits … Maybe there is «type text» command to add or something like that …

Can somebody help me with this one …

Regards.

Robert

rlesperance,

Keep hope and try this (on my int’l system it works):



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
		click button "Click the lock to make changes." of group 1 of window "Power Manager"
	end tell
	delay 0.5
	repeat until not (exists window 1 of process "SecurityAgent")
		delay 0.5
		my authenticate_changes()
		delay 4
		keystroke tab
		keystroke (ASCII character of 31)
	end repeat
end tell


on authenticate_changes()
	tell application "System Events" to tell window "Authentification" of process "SecurityAgent"
		keystroke "yourpass"
		keystroke tab
		keystroke "yourID"
		keystroke return
	end tell
end authenticate_changes

Thanks …

What OS version are you using ? The script does not work on my computer (OS 10.4.11)

Sorry I am using Leopard but the principle is the same as in Tiger.
Be sure that Security Agent’s Auth window is upfront and do a keystroke tab until your password field is selected.
Enter password, tab again, enter name, enter return and off you go.