some passwd in a script ?

I need to use the bash command passwd to put a password on a new user account. How can i put this in my applescript ?

do shell script ("sudo passwd" & user)

when i do that the passwd command wait for a keyboard input, anyone can help me ?

hi dark,

i’m sure you’ve figured out that the script you’ve posted doesn’t do anything. i don’t think you can ‘sudo’ with a do shell script (although you could with a ‘do script’ in the Terminal, but that’s another matter).

i looked into this and i’m going to suggest you use the ‘Expect’ facility from the command line. from the man page:

Expect is a program that "talks" to other interactive programs accord- ing to a script. Following the script, Expect knows what can be expected from a program and what the correct response should be. An interpreted language provides branching and high-level control struc- tures to direct the dialogue. In addition, the user can take control and interact directly when desired, afterward returning control to the script.
the trouble with an Expect script is you really can’t run it (or at least i have not been able to) directly from the ‘do shell script’ facility of AppleScript. therefore, we will write a shell script on the fly, and delete it when we are done.

here is the script. so far my testing shows this works (i was able to run it on a dummy acount on my computer and then use ‘fast user switching’ to log in and verify the new password) but i’d be interested in any comments.


property myExpect : "#!/usr/bin/expect 
spawn passwd [lindex \\$argv 0]
set password [lindex \\$argv 1]
expect \"password:\"
send \"\\$password\\r\"
expect \"password:\"
send \"\\$password\\r\"
expect eof"

set myUser to text returned of (display dialog "Please give the name of the User who's password you want to change" default answer "")

set myPass to text returned of (display dialog "Please give the password you want to use" default answer "" with hidden answer)

do shell script "/bin/cat > /tmp/expectPass << EOF 
" & myExpect & "
EOF" with administrator privileges
do shell script "/bin/chmod +x /tmp/expectPass" with administrator privileges
do shell script "/tmp/expectPass " & myUser & space & myPass with administrator privileges

do shell script "/bin/rm -rf /tmp/expectPass" with administrator privileges

here is a page i used as a reference. you can see i’m just using their Expect script:

http://floppsie.comp.glam.ac.uk/Glamorgan/gaius/scripting/5.html

NOTE: one interesting thing i found was that i needed to ‘escape’ the dollar signs in the text i write to the file. once i had that it was pretty easy, but i don’t know why any word with a dollar sign was not written to the file. could be a ‘cat’ issue i guess.

Thats perfect ! Exaclty what i need. Thanks a lot !

That’s very ingenious, waltr, but since you’ve used a single shell script to do the myExpect script, is there a reason why you didn’t combine the last three (or perhaps all four) shell script commands into a multiline (like myExpect) variable and do it once?

hi adam,

you are correct that this would be better code, but sometimes when i’m trying to explain a concept i just spell it out line by line. in other words, i don’t really have a good reason. since the code is not really my project, it’s probably better to make it readable than compact.

i don’t know if i have to make an other topic for my new question. I try here :wink:

I have to let 4 choices for the user. One, One and Two, One and Three or All :


choose from list {"One", "Two", "Three"} with prompt "Wich options ?" default items "One" with multiple selections allowed
set vartype to result
log vartype

That’s ok

Then I want to format a text by the choice of the user :

set bodymsg to "Text introduction blabla...." as text


if {"One"} is in vartype then
	set bodymsg to bodymsg & "Text Part 1 blablabla..."
	
	if {"One", "Two"} is in vartype then
		set bodymsg to bodymsg & "Text Part 2 blablabla..."
		
		if {"One", "Three"} is in vartype then
			set bodymsg to bodymsg & "Text Part 2 alternative blablabla..."
			
			if {"One", "Two", "Three"} is in vartype then
				set bodymsg to "--->> I want all text part here <<---"			end if
		end if
	end if
end if

I think my if / end if are not good, but i don’t know exactly why…

I got option One, One/Two and One/Three but not full option. Can you help me ?

Do you want something like this?



choose from list {"One", "Two", "Three"} with prompt "Which options ?" default items "One" with multiple selections allowed

set vartype to result

set one_ to ("Text Part 1" & return & return)
set one_two to ("Text Part 2" & return & return)
set one_three to ("Text Part 2 alternative" & return & return)
set one_two_three to (one_ & one_two & one_three)

set bodymsg to ("Text introduction" & return & return) 


if vartype is {"One"} then
	set bodymsg to bodymsg & one_ & return
else
	
	if vartype is {"One", "Two"} then
		set bodymsg to bodymsg & one_two & return
	else
		
		if vartype is {"One", "Three"} then
			set bodymsg to bodymsg & one_three & return
		else
			
			if vartype is {"One", "Two", "Three"} then
				set bodymsg to bodymsg & one_two_three
			end if
		end if
	end if
end if
display dialog bodymsg

This doesn’t deal with if vartype is {“Two”} or {“Two”, “Three”}

j