Mac OS X,  Mass Deployment

Scripting Fast User Switching

Fast User Switching allows a user of Mac OS X to switch accounts without logging out of the account they are currently in. There are a number of uses for this, from troubleshooting to managing workflow. The back end functionality comes from the CGSession binary located in /System/Library/CoreServices/Menu Extras/User.menu/Contents/Resources. There are a couple of options you can use with CGSession, -switchToUserID and -suspend.

/System/Library/CoreServices/Menu Extras/User.menu/Contents/Resources/CGSession -switchToUserID 1022

If an account has no password then the switch should occur automatically. If there is  password then you can simply bring up a login window using the following command (you can also switch to a given user id but it will ask you for a password):

/System/Library/CoreServices/Menu Extras/User.menu/Contents/Resources/CGSession -suspend

You can also invoke CGSession from other languages. AppleScript seems to be a common one that we use. To do so you might use the following line in AppleScript to bring up the fast user switch screen via AppleScript:

do shell script ”

/System/Library/CoreServices/Menu Extras/User.menu/Contents/Resources/CGSession -suspend”

You could also use the following to switch to a known user en masse (rather than a UID), such as localadmin here:

set userid to do shell script “id -u localadmin”
do shell script “/System/Library/CoreServices/’Menu Extras’/User.menu/Contents/Resources/CGSession -switchToUserID ” & userid

If you would also like to add a password as a part of the AppleScript then you would tell “System Events” to tell a window of SecurityAgent to set the value of the password text field to the password you would like to use. You could then loop that through every option in a dictionary of possible password combinations if you aren’t then sure what that password should be…

Random sidenote: In that resources directory, there are some image files. I have changed my /System/Library/CoreServices/Menu Extras/User.menu/Contents/Resources/greenmarker.tiff file to be a different icon, allowing me to have a little custom image. It’s the little things…