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