Create a keyboard shortcut for fast user switching in Mac OS X

23,449

Solution 1

For 10.6 Snow Leopard, one can easily add a Service using Automator, and then assign any keyboard shortcut using System Preferences. See Fast User Switching/Apple Menu? for details.

Solution 2

I use BetterTouchTool for a lot more than just this but, one of the actions it allows you to do is "Show the Login Screen" (which is different from logging out and leaving you at the login screen). I use it ALL THE TIME to lock my machine when I leave it for extended periods of time… Here's a screenshot:

enter image description here

Solution 3

Strictly speaking, Command+Option+Q will log you out (with "Are you sure" prompt).

But I don't see any way to trigger a fast user switch via keyboard. Now you could write an AppleScript script, which you could associate with a keystroke. For example, see this for implementation details.

set thePassword to "password"
set N to name of (info for (path to me))
set AppleScript's text item delimiters to (".")
set N to first text item of N
set AppleScript's text item delimiters to ""
set N to do shell script "/usr/bin/id -u " & N
do shell script "/System/Library/CoreServices/Menu\\ Extras/User.menu/Contents/Resources/CGSession -switchToUserID " & N
tell application "System Events"
    tell process "SecurityAgent" to set value of text field 1 of group 1 of window 1 to thePassword
    click button 2 of window 1 of application process "SecurityAgent"
end tell

Solution 4

Check Fast User Switching with Butler and Butler.

Solution 5

To switch to a specific user I invoke a script I found in the comments of a hints.macworld.com article. I had problems with it so I tweaked it to get if to work. The password is stored in the keychain, so you don't have to worry about storing login password in cleartext. You can find the gist here.

--This script MUST be named "Switch to User.scpt", where User is the name of the user to switch to.
--You must first make a password item (a.k.a. a key) for the other user's password using Keychain Access, and call it "", where "user" is the other user's name and with the description "User Login". The script assumes that you make this key in your login.keychain, which is the default one.
--The first time you run this script, you will be prompted to allow Keychain Scripting to access the password of the key.
--This script requires "Enable access for assistive devices" to be enabled in the Universal Access system preference pane.

set username to word -1 of my findReplace(".scpt", "", (path to me as text))

-- Invoke Fast User Switching. The `id -ur username` part gets the uid number that corresponds to the username and substitutes it at the end of the CGSession command
do shell script "/System/Library/CoreServices/'Menu Extras'/User.menu/Contents/Resources/CGSession -switchToUserID `id -ur " & username & "`"

-- Use universal access to enter the text and to click the button
tell application "System Events"
    repeat
        if (do shell script "stat -f %Su /dev/console") is username then exit repeat

        -- Get the password for the username
        try
            set pswd to (do shell script "security find-generic-password -g -s \"" & username & "\" -D \"User Login\" 2>&1 1>/dev/null | sed -e 's/password: \"//' -e 's/\"//'")
        on error
            exit repeat
        end try

        if exists window 1 of application process "SecurityAgent" then
            tell process "SecurityAgent" to set value of text field 1 of window 1 to pswd
            key code 36
            exit repeat
        else
            tell application "SecurityAgent" to quit
            do shell script "/System/Library/CoreServices/'Menu Extras'/User.menu/Contents/Resources/CGSession -switchToUserID `id -ur " & username & "`"
        end if
    end repeat
end tell

on findReplace(findText, replaceText, sourceText)
    set ASTID to AppleScript's text item delimiters
    set AppleScript's text item delimiters to findText
    set sourceText to text items of sourceText
    set AppleScript's text item delimiters to replaceText
    set sourceText to sourceText as text
    set AppleScript's text item delimiters to ASTID
    return sourceText
end findReplace

To just invoke the Login Screen I have another script. You can find the gist here

do shell script "'/System/Library/CoreServices/Menu Extras/User.menu/Contents/Resources/CGSession' -suspend"

Both scripts are in my quicksilver catalog. Switching between user accounts is a matter of a few seconds.

Share:
23,449

Related videos on Youtube

Brian Willis
Author by

Brian Willis

I live over at brianwillis.com Come say hi.

Updated on September 17, 2022

Comments

  • Brian Willis
    Brian Willis over 1 year

    How do you create a keyboard shortcut that activates fast user switching (i.e. brings up the login window) in Mac OS X Snow Leopard?

    I'm trying to emulate the Windows Start+L keyboard combination, which I miss from my time using Windows.

  • notnoop
    notnoop over 14 years
    @Benjamin, It's Command+Shift+Q