Authenticating in System Preferences in an AppleScript

I'm trying to learn about Applescripting and GUI scripting. I know my code below isn't tidy, but I was learning how to do the menus on the top, so its the long way of doing everything. Ultimately, I want to create a new user account in the system preferences called "Testuser" with "testuser" as the short name and "testpass" as the password.

Right now, I'm trying to figure out how to authenticate as Admin in the system preferences. All this script does right now is gets to the Accounts pane of System Prefs and clicks the lock button at the bottom of the screen. After that, it does nothing. I am using Mac OS X 10.4.2.

activate application "Finder"
tell application "System Events"
tell process "Finder"
tell menu bar 1
tell menu bar item "Apple"
tell menu 1
click menu item "System Preferences…"
end tell
end tell
end tell
end tell
end tell

activate application "System Preferences"
tell application "System Events"
tell process "System Preferences"
application "System Preferences"
tell menu bar 1
tell menu bar item "View"
tell menu 1
click menu item "Accounts"

end tell
end tell
end tell
end tell
end tell

activate application "System Preferences"
tell application "System Events"
tell process "System Preferences"
application "System Preferences"
click button "Click the lock to make changes." of window "Accounts"

end tell
end tell

___________________________________________________
___________________________________________________
The part above gets me to the Authentication window. The part below isn't my real password, but you get the idea what I'm doing.
___________________________________________________
___________________________________________________

tell application "System Events"
tell process "SecurityAgent"

tell window "Authenticate"

tell group 1

set value of text field 1 to "Administrator"

set value of text field 2 to "examplepassword"

end tell

tell group 2

click button "OK"

end tell
end tell
end tell
end tell

Posted on Sep 17, 2005 4:14 PM

Reply
10 replies

Sep 17, 2005 5:21 PM in response to James Engelby

Nevermind on that part, I just added an

activate application "SecurityAgent"

Before the:
tell application "System Events"
tell process "SecurityAgent"

and then it works.

A user account now creates, but I want the computer to automatically log in to the computer with this new account. I am having problems figuring out how to select the "Login Options" on the Accounts preference pane. Can anybody help with this?

Sep 18, 2005 11:22 AM in response to James Engelby

Try this script, it's works

tell application "System Preferences" to set current pane to pane "com.apple.preferences.users"

tell application "System Events" to tell process "System Preferences"
click button "Click the lock to make changes." of window "Accounts"
set Valid to my authenticate_window()
if Valid then --authentication accepted
set frontmost to true
tell window "Accounts"
keystroke tab
repeat (count groups of list 1) + 1 times -- count of groups = number of accounts
keystroke (ASCII character 31) --arrow key down
end repeat
--Login Options is selected
click pop up button 1 of group 1
click menu item "new user name" of menu 1 of pop up button 1 of group 1
delay 2
tell sheet 1
set value of text field 1 to "new user password"
click button "OK"
end tell
end tell
end if
click button "Click the lock to prevent further changes." of window "Accounts"
end tell

on authenticate_window()
activate application "SecurityAgent"
tell application "System Events" to tell process "SecurityAgent" to tell window "Authenticate"
tell group 1
set value of text field 1 to "Administrator"
set value of text field 2 to "example password"
end tell
click button "OK" of group 2
delay 2
return not (exists of button "OK" of group 2)
end tell
end authenticate_window

Sep 18, 2005 11:55 AM in response to Jacques Rioux

So far, thats a really awesome script the way the authentication works and everything. It's a lot less extra stuff than what mine had. The only problem is at the Keystroke tab part. When I do a tab in system preferences, at least in 10.4.2, it moves from the Search field in the top right down to the Name field. And the tab only switches between those two things.

Sep 18, 2005 11:59 AM in response to James Engelby

I forgot the focused of search field.

this works

tell application "System Preferences" to set current pane to pane "com.apple.preferences.users"

tell application "System Events" to tell process "System Preferences"
set frontmost to true
click button "Click the lock to make changes." of window "Accounts"
set Valid to my authenticate_window()
if Valid then --authentication accepted
set frontmost to true
tell window "Accounts"
if focused of text field 1 of group 3 of tool bar 1 is false then
repeat while focused of text field 1 of group 3 of tool bar 1 is false
keystroke tab
delay 0.1
end repeat
end if
keystroke tab
repeat (count groups of list 1) + 1 times -- count of groups = number of accounts
keystroke (ASCII character 31) --arrow key down
end repeat
--Login Options is selected
click pop up button 1 of group 1
click menu item "new user name" of menu 1 of pop up button 1 of group 1
delay 2
tell sheet 1
set value of text field 1 to "new user password"
click button "OK"
end tell
end tell
end if
click button "Click the lock to prevent further changes." of window "Accounts"
end tell

on authenticate_window()
activate application "SecurityAgent"
tell application "System Events" to tell process "SecurityAgent" to tell window "Authenticate"
tell group 1
set value of text field 1 to "Administrator"
set value of text field 2 to "example password"
end tell
click button "OK" of group 2
delay 2
return not (exists of button "OK" of group 2)
end tell
end authenticate_window

Sep 18, 2005 12:14 PM in response to Jacques Rioux

I really appreciate your help so far! But I can't seem to get that to work either. That pretty well gets me to the same spot, so I'm not sure if the focus part is working and I don't know enough about that part yet to really figure it out. The focus starts at that search field, and it only moves between that and the Name field no matter how many times you hit tab.

Is the focus part suppose to select that left pane to make it focused?

Thanks again!

Oct 1, 2005 7:46 PM in response to James Engelby

I had hoped I could do this with Automater, but it doesn't seem to allow you to do jack with the System Preferences. My problem is that I like to conserve energy and sleep my computer when needed, logout to conserve and refresh system resources, and have my screen saver active for security purposes.

Eg, right now, it's set to turn on the saver after 5 mins, log out after 17, and sleep in 20. BUT.... depending on what i'm doing (examples being downloading all day, playing iTunes w/ visualizer on, etc) I find myself constantly changing these settings. Sounds trivial, but you have to open Sys Prefs, and navigate 3 panes to do this. When you do this often it is a pain!

I was hoping I could write an applescript to pop up a dialog box with the following numerical fields (maybe even a checkbox to disable some options, like logout) that would apply them to the Sys Prefs directly:

------------------------
| Screen Saver [ ] |
| System Sleep [ ] |
| Display Sleep [ ] |
| Logout Time [ ] |
| |
| (Cancel) ( OK ) |
------------------------

I then realized this is way above my head.... If somebody could write something like this you would be my HERO! If I knew more about applescript I wouldn't mind messing around to see if I can do it. Knowing how to apply the inputs to Sys Prefs without having the app open would be the most useful starting point I guess, from there I'd have to buy a good Applescript book 🙂

Thanks for any help or pointers in advance

Oct 2, 2005 3:18 AM in response to Mitchell Manthey

Hi Mitchell,

It's REALLY late right now, but here's a quick script that I just whipped up that should do the trick:

property SSITTimes : {0, 3, 5, 15, 30, 60, 120}
property SSTTimes : {0, 1, 15, 30, 60, 120, 180}
property DSTTimes : SSTTimes
property LOTTimes : {0, 15, 30, 60, 120, 180, 240, 300, 360, ¬
420, 480, 540, 600, 660, 720, 780, 840, 900, 960}
-------------------------------
--get current screen saver idle time (in seconds)
try
set ether to do shell script ¬
"/sbin/ifconfig en0 | grep ether | cut -d' ' -f 2 | sed 's/://g'"
set SSIT to (do shell script ¬
"defaults read ByHost/com.apple.screensaver." & ¬
ether & " idleTime") as number
on error
set SSIT to 0
end try
set SSITDefault to SSIT div 60
if SSIT is 0 then
set SSITText to "Never"
else
set SSITText to SSITDefault & " minutes" as string
end if
--get current disk sleep time (in minutes)
try
set SST to (do shell script ¬
"pmset -g | grep ' sleep' | awk '{print $2}'") ¬
as number
on error
set SST to 0
end try
if SST is 0 then
set SSTText to "Never"
else
set SSTText to SST & " minutes" as string
end if
--get current display sleep time (in minutes)
try
set DST to (do shell script ¬
"pmset -g | grep displaysleep | awk '{print $2}'") ¬
as number
on error
set DST to 0
end try
if DST is 0 then
set DSTText to "Never"
else
set DSTText to DST & " minutes" as string
end if
--get current auto log out delay (in seconds)
try
set LOT to (do shell script ¬
"defaults read /Library/Preferences/.GlobalPreferences " & ¬
"com.apple.autologout.AutoLogOutDelay") as number
on error
set LOT to 0
end try
set LOTDefault to LOT div 60
if LOT is 0 then
set LOTText to "Never"
else
set LOTText to LOTDefault & " minutes" as string
end if
-------------------------------
set newSSIT to ¬
(choose from list {SSITDefault} & SSITTimes with prompt ¬
"Screen Saver (" & SSITText & "):" & return & ¬
"(0 is Never)" default items SSITDefault) as string
if newSSIT is "false" then return
set newSST to ¬
(choose from list {SST} & SSTTimes with prompt ¬
"System Sleep (" & SSTText & "):" & return & ¬
"(0 is Never)" default items SST) as string
if newSST is "false" then return
set newDST to ¬
(choose from list {DST} & DSTTimes with prompt ¬
"Display Sleep (" & DSTText & "):" & return & ¬
"(0 is Never)" default items DST) as string
if newDST is "false" then return
set newLOT to ¬
(choose from list {LOTDefault} & LOTTimes with prompt ¬
"Log Out Time (" & LOTText & "):" & return & ¬
"(0 is Never)" default items LOTDefault) as string
if newLOT is "false" then return
--quit "System Preferences" (if running)
tell application "System Events"
if "System Preferences" is in name of processes then ¬
tell application "System Preferences" to quit
end tell
--set screen saver idle time (in seconds)
do shell script "defaults write ByHost/com.apple.screensaver." & ¬
ether & " idleTime -int " & newSSIT * 60
--set auto log out (in seconds)
do shell script "defaults write /Library/Preferences/.GlobalPreferences " & ¬
"com.apple.autologout.AutoLogOutDelay -int " & newLOT * 60
--set display sleep time (in minutes)
--set (system) sleep time (in minutes)
do shell script "pmset displaysleep " & newDST & ¬
" ; pmset sleep " & newSST with administrator privileges

I'm sure it can be cleaned up quite a bit (like I said it's REALLY late and I'm VERY tired). If you have any questions about the script feel free to ask and I'll try to do my best to answer them 😉

Hope this at least guides you in the right direction. I know it's not done exactly the way you were asking for, but what you're asking for is really best suited for an AppleScript Studio project. Vanilla AppleScript is pretty limited on user interaction.

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

Authenticating in System Preferences in an AppleScript

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.