Unlock System Preferences using GUI Applescript
I am trying to unlock the System Preferences using applescript.
I have managed to get my script to click the "Click the lock to make changes" part, and I was trying to get the applescript to enter the user name, but I keep getting the error
error "System Events got an error: Can’t get window 1 of process \"SecurityAgent\". Invalid index." number -1719 from window 1 of process "SecurityAgent"
Here is my code, can anyone give me a hand?
activate application "System Preferences"
tell application "System Events"
set preferencesLocked to false
tell process 开发者_如何学运维"System Preferences"
delay 1
click menu item "Security & Privacy" of menu "View" of menu bar 1
delay 2.5
if title of button 4 of window 1 is "Click the lock to make changes." then
set preferencesLocked to true
click button "Click the lock to make changes." of window 1
end if
end tell
if preferencesLocked is true then
delay 2.5
activate application "SecurityAgent"
tell application "System Events"
tell process "SecurityAgent"
set value of text field 1 of scroll area 1 of group 1 of window 1 to "username"
end tell
end tell
end if
end tell
Please help. Thank you.
This can be done using the System Events' "keystroke" command to type in a password. Yosemite version (UI elements have moved around):
set thePW to "MY_PASSWORD"
set thePane to "Security & Privacy"
activate application "System Preferences"
delay 1
tell application "System Events"
tell process "System Preferences"
click menu item thePane of menu "View" of menu bar 1
delay 3
if title of button 1 of window 1 is "Click the lock to make changes." then
click button 1 of window 1
delay 2
keystroke thePW
keystroke return
end if
end tell
end tell
The authentication dialog is a special sort of thing in OS X; it is implemented in a secure way where it at least cannot be read by key loggers.
I would be surprised if it was possible to script, both as a side effect of this and because it seems to introduce potential security holes.
So you're probably out of luck, sorry.
精彩评论