How to create a hot key to switch users in OS X

While OS X supports fast user switching, it does not have a built-in hot key to do so. Here is how to set one up.

OS X is a multiuser operating system so each user can tailor the system for his or her own needs without interfering with another user's workflow. With this setup, not only can multiple people use a system, but an individual can also configure it to have multiple work environments.

For example, if you use your computer for two jobs and would like to keep all work for one completely separate from another, then one option is to use multiple user accounts for this. Alternatively you might like to have a separate user account for testing purposes if you've been experiencing oddities with your system and are trying to troubleshoot the situation.

User menu extra in OS X
The user menu gives you quick access to multiple accounts, but requires that you access it with your mouse. Screenshot by Topher Kessler/CNET

Regardless of the need, if you have multiple accounts on the system that you regularly switch among, you might find doing so a burden. Apple does have a hot key (Option-Command-Q) for logging out of the current account and returning to the log-in window, but the system's Autosave and Resume features are not supported in all applications, so workflows may not be entirely preserved, and logging out will halt running applications.

Another option is to use fast-user switching and then select an account or return to the log-in window from the User menu that appears next to the Spotlight menu. This feature is more convenient; you can use it to quickly switch among accounts, but it does not have any hot-key options, so you will need to access it via the mouse. For most people this is perfectly fine to do, but if you want to speed this up, you can create a custom hot key that will switch users for you.

To do this, you can take advantage of a hidden Terminal command that is part of the User menu extra and is invoked by the menu to switch users. The command is called "CGSession" and can be used in the following ways to either bring up the log-in window or to switch to an account that is already logged in.

To invoke the log-in window:

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

To switch to an active user account:

/System/Library/CoreServices/Menu\ Extras/User.menu/Contents/Resources/CGSession -switchToUserID `id -u USERNAME`

With these options, you can then use Apple's Automator utility to set up a hot-key service for switching users. This can be implemented in several ways, but the following examples are quick options for setting up hot keys for the log-in window and for single user accounts:

Automator workflow to switch users
When run, this workflow will execute the specified Unix command to switch to the user "tkessler" (click for larger view). Screenshot by Topher Kessler/CNET
  1. Create a log-in window hot-key service
    To create a log-in window hot key, open Automator and create a new Service workflow. Then ensure that the service accepts "no input" and will work in "all applications," and then add a single instance of the "Run Shell Script" action to the workflow. In this action, copy the first command above for accessing the log-in window, replacing the default "cat" command in the action.

    Next save the workflow with a name like "Show Login Window."
  2. Create a user hot-key service
    The workflow for a user hot key will be the same as that for the log-in window, with the exception that you will instead use the second of the commands listed above, and then change the "USERNAME" text in it to the the short username of the account that you would like this workflow to invoke.

    Again save this workflow but call it something like "Switch to USERNAME," but this time repeat the process and create additional copies of this workflow for the various users on your system, and name them accordingly.

With these workflows saved, now go to the Keyboard system preferences and select "Services" in the Keyboard Shortcuts tab, where you should find the saved workflows listed by name (likely at the end of the list under the General section). In this list, check the workflows to enable them, and then assign a hot key of your choice. My recommendation is to use numbers (e.g., Option-Command-1 for the first user, and Option-Command-0 for the log-in window), but these specifics are up to you.

With the hot keys bound, you should now be able to quickly switch users by pressing them and then supplying the user's password when prompted. If a user is not logged in, then you can invoke the log-in window to quickly select that user and log in, all of which can be done by navigating with the keyboard.

A final option is instead of assigning separate services to each user, to create a workflow that will pop up a menu in which you can select a user via the keyboard's arrow keys. While similar to accessing the User menu, this allows you to invoke the menu and make a selection from the keyboard, so you do not have to access the mouse.

To do this, create the following Service workflow in Automator and save it, followed by assigning it to a hotkey of choice. When run it will then pop up with a list of the active user accounts which can then be selected and chosen to switch to.

User account menu in OS X
Create a workflow exactly like this one to create a selectable list of user accounts to log into, which can be invoked by a hot key. Screenshot by Topher Kessler/CNET

A final word of note is that custom hot keys are specific to a user account, so you will need to apply them in all accounts you use.



Questions? Comments? Have a fix? Post them below or e-mail us!
Be sure to check us out on Twitter and the CNET Mac forums.

Tags:
Computers
About the author

    Topher, an avid Mac user for the past 15 years, has been a contributing author to MacFixIt since the spring of 2008. One of his passions is troubleshooting Mac problems and making the best use of Macs and Apple hardware at home and in the workplace.

     

    Join the discussion

    Conversation powered by Livefyre

    Don't Miss
    Hot Products
    Trending on CNET

    HOT ON CNET

    Delete your photos by mistake?

    Whether you've deleted everything on your memory card or there's been a data corruption, here's a way to recover those photos.