2

I would like to automate fast user switching for a set of machines. My current, broken solution is this applescript snippet, adapted from Leopard/SL scripts found online:

set thePassword to "foo"
set N to "1027"
do shell script "/System/Library/CoreServices/Menu\\ Extras/User.menu/Contents/Resources/CGSession -switchToUserID " & N
delay 0.8
tell application "System Events"
  keystroke thePassword
  delay 1
  keystroke return
end tell

The password seems to be entered successfully, but keystroke return fails to log the user in (the screen "vibrates" to indicate a failed attempt). The funny thing is that pressing the enter key manually does complete the login (hence my assertion that the password is entered successfully)

Any ideas?

Thanks!

3
  • No concrete idea, but maybe you have to tab out and then press the button for logging in?
    – slhck
    Commented Mar 13, 2012 at 13:21
  • @slhck: nope, tried it, no luck. Thanks for the suggestion though!
    – Rom1
    Commented Mar 13, 2012 at 13:29
  • You will rather want to automate the switch to the user than to automate typing their passwords, consider that any event would break your script. Commented Mar 20, 2012 at 16:24

2 Answers 2

1

I can't tell you what might be going wrong, but here's a workaround from many years ago:

'tell application "System Events" to keystroke return' on Intel iMacs

Essentially, the author of this post says that in a very similar scenario, the return key would only successfully trigger a login on PowerPC iMacs; on Intel machines, the script sets the display sleep timeout to 1 minute, waits 65 seconds, and then somehow, once the display is asleep, the "keystroke return" works. (!)

Sounds pretty horrible, but maybe you can adapt something workable from this.

0

There seems to be two major versions of the script floating around.

The first is similar to yours, except that "keystroke return" is duplicated twice (source) :

osascript <<EOF
tell application "System Events"
keystroke "USERNAME"
keystroke tab
delay 0.5
keystroke "PASSWORD"
delay 0.5
keystroke return
keystroke return
end tell
EOF 

Somebody even figured out that twice is not enough :

osascript <<EndOfMyScript
tell application "System Events"
keystroke "USERNAME"
keystroke tab
delay 0.5
keystroke "PASSWORD"
delay 0.5
keystroke return
keystroke tab
delay 0.5
keystroke "PASSWORD"
delay 0.5
keystroke return
keystroke return
keystroke return
keystroke return
keystroke return
end tell
EndOfMyScript

While somebody else prefers this solution, which will work if you have a list of users with "Other..." displayed in the login window :

osascript <<EOT
set username_ to "username"
set password_ to "password"
tell application "System Events"
key code 125 -- Down Arrow
key code 125 -- Down Arrow
delay 1
key code 125 -- Down Arrow
key code 125 -- Down Arrow
key code 125 -- Down Arrow
key code 125 -- Down Arrow
key code 125 -- Down Arrow
key code 125 -- Down Arrow
delay 0.5
key code 36 -- Return
delay 1
tell process "SecurityAgent" to set value of text field 1 of group 1 of window 1 to username_
tell process "SecurityAgent" to set value of text field 2 of group 1 of window 1 to password_
click button "Log In" of window 1 of application process "SecurityAgent"
end tell
EOT

(Answered by a non-Mac user)

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .