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.