Somebody was asking the other day for an Applescript that they could use to create users on their other Macs. While this has been done many times I’d not seen anybody offer a solution that did everything in from one script (i.e. create user, add user to group and set users password).
So off I went and created an Applescript which does just that. It runs from the command line with 3 arguments: the root password, the new users ID and the new users full name.
I cheated and used a couple of perl scripts and an expect script to get this done. In fact I’m only using Applescript as a wrapper to call the other scripts, this could be another script (bash/perl etc).
The first thing that happens when you call the script is writes three files to the current users home directory - these are the 3 scripts. They are called .isUserAvailable, .getNextUID and .changePassword.
.isUserAvailable checks to see if the new user ID you specified is already in use, if so it stops and tells you to choose another user ID.
.getNextUID gets the next highest user ID number starting at 500. It won’t fill in the blanks, i.e. choose 502 if 503 is taken. It always takes the highest number.
.changePassword is an expect script which sets the users password. It currently sets the password to that of the user ID - if I get round to it I may change this to be something random which it outputs back to the person creating the user.
If you look at the Applescript (below) you’ll see I’m making use of sudo in order to create the user etc, it’s getting piped in to STDIN. If you look closer you’ll notice I’m not piping it in except in the first command “ls”. This is because sudo caches the password so I don’t have to keep passing it - this is very useful because you’ll notice I’m using the niload command which also reads from STDIN.
Usage is simple, just run osascript AddUserScript.scpt [rootPassword] [newUserID] [newUserFullName].
Have fun and let me know if this is helpful for you. Of course you use this code at your own risk - I’ve only tested this on a few Macs all running 10.4.x.
-Chris-
(more…)