Apple Script For Creating Multiple User Home Directories

The title suggests it all & I probably don’t need to explain my script to you guys but I will explain a little as it will help explain the problem I am having.

The script was designed to over come having to make 200+ user home directories a year for new pupils.
The secrateries in the school put all the pupils into a access database which I when pull all their usernames off using a query into a txt file called user.txt with 1 pupil per line.
Once this is done I run the script.

Currently i have to tell the script how many paragraphs/pupils there are which is annoying having to count 200+ users.

what I would idealy like is to get the script to count for me by counting the carriage returns or the \n but I havn’t been able to get this to work. currently here is my script:

set the_path to text returned of (display dialog "Insert Full Path NOT Including Username ie. /volumes/data/homes/staff/perm" default answer "/volumes/data/homes") as string


(*gets usernames from txt file*)
set _userfile to "Server_HD:user.txt"
display dialog "PLEASE NAME YOUR TEXT FILE WITH USERS IN user.txt AND COPY IT DO THE SYSTEM DISK, THANK YOU!"
set _contents to read file _userfile
set _delimiter to (ASCII character 10) & (ASCII character 13)
set num_users to count (read alias "Server_HD:user.txt" using delimiter _delimiter)
display dialog num_users
set p1 to 0
repeat until p1 = num_users
	set p2 to p1 + 1
	set p1 to p2
	
	set _user to word 1 of paragraph p2 of _contents
	set no_error to true
	(*send out template*)
	do shell script "cp -R /template " & the_path user name "root" password "****" with administrator privileges
	(*rename template to _user*)
	do shell script "mv " & the_path & "/template " & the_path & "/" & _user user name "root" password "****" with administrator privileges
	(* set owner to user defined & group to staff *)
	try
		do shell script "chown -L -R " & _user & ":staff " & the_path & "/" & _user user name "root" password "****" with administrator privileges
	on error
		set no_error to false
		display dialog "The user " & _user & " Doesn't Exist, Delete Home Directory & Move To Next User?" buttons ["Yes", "No"]
		if button returned of result is "yes" then
			do shell script "rm -R " & the_path & "/" & _user user name "root" password "****" with administrator privileges
			
		end if
	end try
	if no_error then
		
		
		(* chmod 770 *)
		do shell script "chmod -R 775 " & the_path & "/" & _user user name "root" password "****" with administrator privileges
		(* no access on root of template*)
		do shell script "chmod 770 " & the_path & "/" & _user user name "root" password "****" with administrator privileges
		beep 1
	end if
end repeat
display dialog "Script Has Finished Number Of Users Created" & num_users

where **** is is my admin password which I’m obvously not going to give out.

the perticular area of the script I am trying to get to work is

set _contents to read file _userfile
set _delimiter to (ASCII character 10) & (ASCII character 13)
set num_users to count (read alias "Server_HD:user.txt" using delimiter _delimiter)
display dialog num_users

as you can see currently I am using ASCII characters but I’m not sure if this is correct. ANY light you guys can shed on this subject would be great.

Thanks again in advance.

Dave Maltby

Hi,

this is very easy
it’s just one line, no need for handling any delimiters,
AppleScript recognizes LF, CR and CRLF as a line delimiter

set num_users to count paragraphs of (read alias "Server_HD:user.txt")

Edit: Or in context

set _contents to read file _userfile
set num_users to count paragraphs of _contents
display dialog num_users

PS: I recommend to quote the POSIX paths
e.g

do shell script "cp -R /template " & quoted form of the_path user name "root" password "****" with administrator privileges

to avoid errors if there are spaces in the paths

thats that worked ace, loads less hassle now! yea thats a good point about posix paths! i’ll sort that out now!

thanks for your help

Dave Maltby