Fast User Switch with auto-entering the password

The main code of the script refers to an article on macosxhints,
I’ve added the part to enter the password automatically.

1) The shell part

create a shell script named fus, make it executable (chmod a+x) and save it in /usr/local/bin.
Of course it can be located everywhere, but then the path in the AppleScript(s) must also be changed!
(I put all my custom shell scripts in /usr/local/.)

[code]#!/bin/sh

Fast User Switching from the command line

CGSession=‘/System/Library/CoreServices/Menu Extras/User.menu/Contents/Resources/CGSession’

case “$#” in
0)
# display login screen
exec “$CGSession” -suspend
exit
;;
esac

No reason to display the login panel for the current user

case “$1” in
“$USER” | “$UID”)
exit 0
;;
esac

can pass in a (short) username or userid

id=/usr/bin/id -u "$1" || exit

display login panel for user

exec “$CGSession” -switchToUserID “$id”[/code]
2) The AppleScript part

create one script for each user you want, adjust the password in the property line and name it with the particular short user name.
E.g. you have two users john and foo, create a script john.scpt and foo.scpt.
Put the scripts in /Library/Scripts or somewhere in the main library to have access from every user.

When one of the scripts is executed, is passes its name (without extension) to the shell script,
which does the user switch. Afterwards the password will be inserted via GUI scripting (GUI scripting must be enabled)


property pass_word : "¢¢¢¢¢¢"

set N to quoted form of name of (info for (path to me))
do shell script "/usr/local/bin/fus `/usr/bin/basename " & N & " .scpt`"

tell application "System Events"
    repeat until exists process "SecurityAgent"
        delay 0.5
    end repeat
    tell window 1 of process "SecurityAgent"
        set value of text field 1 of group 1 to pass_word
        click button 2
    end tell
end tell

Notes:
¢ to assign a hotkey to the scripts a third party tool is required
¢ This method is tested on Leopard, but should work also in Tiger

Hi Stefan,

I’ve written script to do similar things. Normally mine are run using a hotkey app that triggers applescript,
But also have don some shell ones. They all normally have the user id number built in.
But I like your approach of using the name of the script. Which means you can just edit the name of a duplicate rather than the script it self

I’m unsure why you need two scripts?

set password_ to "password"
set N to quoted form of name of (info for (path to me))
set idnumber to do shell script "/usr/bin/id -u `/usr/bin/basename " & N & " .scpt`"
do shell script "/System/Library/CoreServices/Menu\\ Extras/User.menu/Contents/Resources/CGSession -switchToUserID " & idnumber
tell application "System Events"
	repeat until exists process "SecurityAgent"
		delay 0.5
	end repeat
	tell process "SecurityAgent" to set value of text field 1 of group 1 of window 1 to password_
	click button "Log In" of window 1 of application process "SecurityAgent"
end tell

Hi Mark,

I took the method from the macosxhints article.
The shell script also considers non valid user names and the case, if the user chooses the current user.

But you’re right. All these things can be also done with one (Apple)script