10.3: Use fast user switching from the Terminal

Nov 07, '03 10:05:00AM

Contributed by: makaio

For those who love Panther's Fast User Switching but don't like the menulet in the upper-right corner, here's a shell script to do it.


#!/bin/sh
if [[ -z $1 ]]; then
  # robg note: Please enter the next two lines as one without
  # any spaces between the "/" and the "R"
  /System/Library/CoreServices/Menu\ Extras/User.menu/Contents/
  Resources/CGSession -suspend
else
  USERID=`id -u $1`;
  if [[ -z $USERID ]]; then
    exit -1;
  fi;
  # robg note: Please enter the next two lines as one without
  # any spaces between the "/" and the "R"
  /System/Library/CoreServices/Menu\ Extras/User.menu/Contents/
  Resources/CGSession -switchToUserID $USERID
fi;
Save the script in your path and make it executable (chmod 755 script_name). If it is called without any arguments, the script will switch to the login window. If called with a user name or id, it will bring up the password dialog and (assuming the correct password is entered) switch to that user.

This is especially useful because fast user switching does not have to be enabled in the account preferences for this hint to work.

[robg adds: Read the rest of the hint for my method of making this very cool script easy to use from the Finder...]

[robg adds: Here's a little trick to make it really easy to use. After you have it working, open a new Terminal window, make it as small as you can by dragging the corner, and then do File -> Save As (it doesn't matter what's in the window at the time). The default save location should be in your user's Libary -> Application Support -> Terminal folder. Give the saved file a name -- let's assume you want to fast switch to the login window, so call it "login." In the bottom section of the dialog box, click "Execute this command (specify complete path)" and then enter the path to your new script. In my case, it would be:

/Users/robg/bin/usw

Uncheck the "Execute coommand in a shell" box, and click Save. Now switch to the finder, and navigate to the ~/Library -> Application Support -> Terminal folder. Drag the "login.term" file into your sidebar, or your toolbar, or make an alias on your desktop, or whatever -- you'll be able to activate the login window just by clicking (in the sidebar or toolbar) or double-clicking (elsewhere) the icon!

To switch to another user instead, just create another saved window (rememer to make it tiny to make it less visually shocking), and for the "full path" bit, add the user's name to the script call:

/Users/robg/bin/usw testuser

In both cases, you'll see a tiny Terminal window appear, but then you'll either be taken directly to the login screen, or you'll get the switch user password dialog box. Wow! Side note: I changed my script name to usw from switch as sjonke commented that there's already a system-provided command by that name.]

Comments (51)


Mac OS X Hints
http://hints.macworld.com/article.php?story=20031102031045417