System Preferences Applescripting

I’d not really been following this topic, but thought I’d take a quick look at your script, jc - just in case you’d prefer to stick with the UI scripting approach.

While the following version may not yet do all you want, it does everything you described in your initial notes. It should certainly take you beyond the impasse over accessing the Login Options - and without a keystroke in sight. In fact, it completes the creation of a new user account and quits System Preferences, so any additional tweaks to the settings should now be a pretty straightforward matter. Hope it helps…


to authenticate_changes()
	tell application "System Events" to tell window "Authenticate" of process "SecurityAgent"
		tell group 1
			set value of text field 1 to "Administrator"
			set value of text field 2 to "myfakepassword"
		end tell
		click button "OK" of group 2
	end tell
end authenticate_changes

tell application "System Preferences"
	reveal anchor "passwordPref" of pane "Accounts"
	activate
end tell

tell application "System Events" to tell window "Accounts" of process "System Preferences"
	tell button "Click the lock to make changes." to if exists then
		click
		my authenticate_changes()
	end if
	click (first button whose name is "Button" and help is "Add a user account")
	
	tell sheet 1
		set value of text field 1 to "FRStudent"
		set value of text field 2 to "frstudent"
		set value of text field 3 to "frlab"
		set value of text field 4 to "frlab"
		click button "Create Account"
		repeat while exists
			delay 0.3
		end repeat
	end tell
	
	tell application "System Preferences" to reveal anchor "loginOptionsPref" of pane "Accounts"
	
	tell group 1
		click checkbox "Automatically log in as:"
		tell pop up button 1
			click
			click (first menu item of menu 1 whose name is "FRStudent")
		end tell
	end tell
	
	tell sheet 1
		repeat until exists
			delay 0.2
		end repeat
		set value of text field 1 to "frlab"
		click button "OK"
	end tell
	click button "Click the lock to prevent further changes."
end tell

quit application "System Preferences"


hello kai and jc,

I have a similar issue. I need to script some changes to the second user in the Accounts list. Im a lab manager, and the admin account is first in the list with a managed account second on the list which the students use. I cant find a way to select or activate the second account in order to script changes to its settings. Ive tried everything I can think of from attempting to click by position to searching for a way to focus on “List 1” which is what that area is described as in Pre Fab UI browser (so I could use up and down arrow keystrokes to change which account Im on). I just cant find a way to select the second user. Any ideas?

Thanks in advance,

Rowie

Hi Rowie

This Works but please treat it like the blunt instrument it is. a few other factors could come into place
and usually do when UI scripting.
this selects the next user in the list

tell application "System Preferences"
	reveal anchor "passwordPref" of pane "Accounts"
	activate
end tell
tell application "System Events"
	tell process "System Preferences"
		tell window "Accounts"
			keystroke tab
			keystroke (ASCII character of 31)
		end tell
	end tell
end tell

Hi Pidge, thanks for the response but this does not work for me. The tab key only toggles between the search field at the top right and the fields within the various panes (password, parental control etc) but never sets the focus on the users list. I dont know if this is something system specific (Im using OS 10.4.8) but even without the scripting, just sitting here with my System Prefs open and attempting to get the focus on the users list, neither tab nor any other key seems to work. Ive tried it on other computers as well with the same result. Ive been fairly thorough in attempting to find a way to script this and have exhausted all ideas that involve any keystroke combinations or anything like that.

I was hoping someone might know an anchor name for the users list in the Accounts pane, or some other way to reference it, set the focus on it, or even just how to click it using x,y position. UI Browser generates the following code which, according to them, should click on the position of the icon of the second user, but it has no effect for me and I dont know why (Ive run tests to see if its clicking somewhere else, even in other programs such as a text edit doc where clicking somewhere would change the location of the cursor and the code appears to be ineffective) Of course, I used the appropriate Tell block to reference the Sys Prefs (or Text Edit during my test) before and after the code:

try
tell image 1 of group 2 of list 1 of window “Accounts”
set {xPosition, yPosition} to position
set {xSize, ySize} to size
end tell
– modify offsets if hot spot is not centered:
click at {xPosition + (xSize div 2), yPosition + (ySize div 2)}
end try

Any ideas why this doesnt work, or any other approaches to try? Many thanks…

Browser: Firefox 2.0.0.1
Operating System: Mac OS X (10.4)

Hi Rowie

Sorry this didn’t work for you. like i said blunt instrument.
It’s probably just the way are machines are set up i guess (i’m on 10.4.8 as well) and it works on mine
Is your padlock locked or unlocked this may make a difference it might be an idea to add the unlock bit
into this script…
if i get chance i’ll look further into this, i’m sure someone else with a bit more experience will no how
to solve this for you.
does this have a better result:

tell application "System Preferences"
	reveal anchor "passwordPref" of pane "Accounts"
	activate
end tell

tell application "System Events" to tell window "Accounts" of process "System Preferences"
	tell button "Click the lock to make changes." to if exists then
		click
		my authenticate_changes()
	end if
	delay 4
	keystroke tab
	keystroke (ASCII character of 31)
end tell

to authenticate_changes()
	tell application "System Events" to tell window "Authenticate" of process "SecurityAgent"
		tell group 1
			set value of text field 1 to "Administrator"
			set value of text field 2 to "yourpassword"
		end tell
		click button "OK" of group 2
	end tell
end authenticate_changes

good luck

Hmmm…interesting. Pidge, are you using an Intel machine or Power PC? I cant understand why it would work for you and not for me. The machines I have been using are all PPC running OS 10.4.8. I have the unlock portion of the code in my script already and have tried it manually with the lock unlocked, it makes no difference, I just cant use tab or any key combinations to focus on the users list. Even if I just manually click on the users list and hit tab, it will toggle to the search field and then to the fields within the pane, but never back to the users list.

What version of Sys Prefs are you running? Mine is 3.3

Thanks for the help.

-Rowie

Hi rowie

system prefs version 3.5
running on intel macbook pro!
maybe there lies the answer!
will test it on a power pc when i get chance…

sorry rowie just reading your last post again.
The tab for me just highlights the list of users the ascii keystroke 31 which is the down arrow skips to the second user
in the list.

Hi,

UI scripting is very sensitive with different localizations.
I tried to change the script to an language independent version.
On my Dual G5 with german 10.4.8 system this works:

tell application "System Preferences"
	reveal anchor "passwordPref" of pane id "com.apple.preferences.users"
	activate
end tell

tell application "System Events" to tell window 1 of process "System Preferences"
	tell button 4 to if exists then
		click
		my authenticate_changes()
	end if
	delay 4
	keystroke tab
	keystroke (ASCII character of 31)
end tell

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

Thanks for the attempt Stefan, but this doesnt work. In this script, and the previously discussed ones, the tab key is being used to set the focus on the users list and then the down arrow to select the second user. The issue here appears to be that on the computers I am working with, the tab key does not activate or set the focus on the users list. Without even using Applecript, just manually attempting to use the tab key to set the focus on the users list in the Sys Prefs, it just doesnt work. The focus toggles between the search field at the top right and the fields within the various panes, but never sets the focus on the users list. I am using Power PC machines with OS 10.4.8 and System Preferences 3.3.

I thought maybe the difference in systems between myself and Pidge might have explained why it doesnt work for me, but Stefan, you said you are using a PPC mac with 10.4.8, so now Im really baffled. What version of Sys Prefs do you have Stefan?

Hi rowie,

sorry, I didn’t get the crucial point to select a second user.
I found out, that properly working of the script doesn’t depend on SysPref version (I have also 3.3)
but on the settings of “Full keyboard access” in Keyboard & Mouse > Keyboard Shortcuts.
If “All controls” are selected, tab key works

Ah, Stefan, you are truly my hero. I have to admit I feel a bit silly not ever having seen that option, but I guess I never had a use for it before. My sincerest thanks for bringing it to my attention…problem solved! :slight_smile:

Hi.

In kai’s original script, he used the following to click the ‘padlock’ button. (I’ve split the ‘tell’ lines here for clarity.):

tell application "System Events"
	tell window "Accounts" of process "System Preferences"
		tell button "Click the lock to make changes."
			if exists then
				click
				my authenticate_changes()
			end if
		end tell
		
		-- etc.
	end tell
end tell

Presumably the check to see if button “Click the lock to make changes.” exists is because when the padlock’s open, the button’s name changes to “Click the lock to prevent further changes.” The non-existence of a button called “Click the lock to make changes.” means that the padlock’s already open and the click and the authentication handler are to be skipped.

Stefan’s version of the script identifies the button by index number instead, presumably for greater compatibility with the buttons on his machine, which are named in German. The problem with this is that ‘button 4’ always exists, regardless of its name, so the click and the authentication are always performed. One possible way round this is to check the button’s size, since the ‘open padlock’ is slightly larger than the ‘closed padlock’. On my machine, the sizes are {190, 16} (closed) and {246, 15} (open).

tell application "System Events"
	tell window "Accounts" of process "System Preferences"
		tell button 4
			if (size is {190, 16}) then -- {190, 16} when padlock closed, {246, 16} when open.
				click
				my authenticate_changes()
			end if
		end tell
		
		-- etc.
	end tell
end tell

I think this would work for everyone, but I’m not totally sure. Any feedback would be appreciated.

Works for me Nigel.

Hey Guys
Merry Christmas!
Works fine here too!:smiley:

Thanks, guys. :slight_smile:

This may be a little off topic now… and does use shell scripts, but might help someone?

If you want to your mac/s to autologin temporarily (eg. for deploying a lab) to an existing account, you’ll need to call the following shell scripts in your applescript. For example:

do shell script "shell script command" with administrator privileges 

To enable autologin:

You may also need the kcpassword file (/private/etc/kcpassword) - to get this, enable autologin manually, then make a backup of this file and keep in a separate location, then copy back to its original location as part of your script, eg:

And to disable autologin:

Hey guys,

I got a Kai’s script working great. I’m trying to create an Admin account and can’t figure out how to select the check box “allow user to administer the computer” I know it’s something as simple as setting the value of the Checkbox to 1 (1 for checked, 0 for unchecked). I think I just need some help with the syntax

tell application “System Events”
tell process “System Preferences”
application “System Preferences”
tell sheet 1 of window “Accounts”
set value of text field 1 to “Local Admin”
– Display Name
set value of text field 2 to “LocalAdmin”
– Short Name
set value of text field 3 to “"
– Password
set value of text field 4 to "

– Verify Password
set value of text field 5 to “default local admin pwd”
– Add’s Comment for password hint field
set value of checkbox 1 to 1

        click button "Create Account"
    end tell
end tell

end tell

Any ideas?

Thanks,

Jeff

P.S. Sorry for being a N00B

Model: Tiger 10.4.6
AppleScript: 2.11 (81)
Operating System: Mac OS X (10.4)

I tried "click checkbox “allow user to administer computer”

I also tried set value of checkbox 1 to 1

Never mind… Setting the checkbox value to 1 worked :smiley:

Many thanks for these posts. I had been wrestling with this for some time. Although the tab and down arrow solution works there is something odd about the UI scriptability of the Accounts pane. It just won’t allow you to click on a user with syntax like, say:

tell window “Accounts”
tell list 1
tell group 2
tell static text “username”
set {x, y} to position
set {xs, ys} to size
end tell
click at {x + (xs div 2), y + (ys div 2)}
end tell
end tell
end tell

If anyone knows why, I’d be interested, academically, since I now have my script working.

Thanks again