Can anyone help wrap this unix script into applescript?

[b]Hi All!

I’m having a lot of problems wrapping this unix script into applescript … Heres the original: [/b]

#!/usr/bin/expect –
spawn pwpolicy -a username -u username -setpassword newpassword

expect Password:
send “oldpassword\r”
expect eof

Here’s the applescript I attempted that doesn’t give any errors, but doesn’t work nonetheless …

do shell script "#!/usr/bin/expect --" & "¶" & "¶" & "spawn pwpolicy -a username -u username -setpassword newpassword" & "¶" & "¶" & "expect Password:" & "¶" & "send \"oldpassword\"" & "¶" & "expect eof"

[b]NOTES: I’ve obviously changed the real usernames into just “username” and I have set the passwords as “oldpassword” and “newpassword” for the sake of demonsration.

Any help would be much appreciated![/b]

I do not think you need to include the shebang, just the path to expect.

Also I Think you can just do it like this. But i can not test it.

set TheExpect to do shell script "/usr/bin/expect --
spawn pwpolicy -a username -u username -setpassword newpassword

expect Password:
send \"oldpassword
expect eof"

By the way,
use :


instead of


Thanks for helping me out!

I tried your code and got this message:

sh: line 2: spawn: command not found
couldn’t read file “Password:”: no such file or directory
sh: line 6: send: command not found
couldn’t read file “eof”: no such file or directory

So looks like it doesn’t like it …

If I Put it all into 1 line it I don’t get any compile errors but the script still doesn’t work …

set TheExpect to do shell script "#!/usr/bin/expect --" & return & "spawn pwpolicy -a username -u username -setpassword newpassword" & return & return & "expect Password:" & return & "send \"oldpassword" & return & "\"" & return & "expect eof"

Any ideas?



Those errors are look like the problem is in the Shell script its self.

Does you shell script Actually work when you run it as an executable??

As an example to show the script should work in this form if there is now problem with the shell script.

run the script below. it will wait 10 seconds and then pop up your login window. (don’t worry it will not log you out, just it hit the cancel in the login window"

set t to do shell script "sleep 10
idl=$\"`ioreg -c IOHIDSystem | awk '/HIDIdleTime/ {print int($NF/1000000000); exit}'`\"
if [ $idl -gt $idletime ]; then
echo $idl,$idletime 
/System/Library/CoreServices/Menu\\ Extras/ -switchToUserID 502

yep, every time…

obviously, the username is a real username and the password is a real password…

Just edited my post above, does the example script work for you

No the script doesn’t work …

It doesn’t contain errors, and returns a “”.

No login window appeared or anything … I’m running OS X Server 10.4.11 if that makes a difference …


Ah, I see, I have no experience OS X Server.

As you did not explain that,
I assumed you where using the script on your G4 ‘10.4’ ( looked at a previous post of yours to see what you had)
It may be a good idea to put “I’m running OS X Server 10.4.11” and any other info that can get the right person and help for your request. :wink:

I assumed that Server 10.4 had the same Applescript as the client version? As we still have automator, Script Editor, etc.

Is this not the case?


I have not got a clue??

But in my example script, the “-switchToUserID 502” bit might be the problem.

You could change it to 501 or I just had a play and found using -switchToUserID % instead of a number brings up the Admin one. (In 10.4)

Hi Mark!

I gave up on using applescript exclusively… I don’t think it works well with ‘Expect’.

What I ended up doing was very more complex but it worked…

I created a php script that created a file containing a shell script which in turn changed the user’s password in ldap …

3 steps to achieve a simple goal … Well, at least now it’s automated! LOL

Thanks for your help, anyway! :wink:



Glad you found a solution :slight_smile: