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

16

4

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.

Brian Willis

Posted 2009-09-21T07:42:42.470

Reputation: 672

while smalltown doesn't fix his answer, you may want to check this: http://www.macosxhints.com/article.php?story=20050706194219822 or that: http://artofgeek.com/2009/09/08/lock-your-macs-screen-like-in-windows-snow-leopard-edition

– cregox – 2010-04-15T02:02:20.363

Answers

7

If you are at the CLI, you could have the following alias set to switch quickly:

alias switch='/System/Library/CoreServices/Menu\ Extras/User.menu/Contents/Resources/CGSession -suspend'

user31752

Posted 2009-09-21T07:42:42.470

Reputation:

This and fastscripts and automator make fast work of needing a CLI handy.

– bmike – 2011-08-15T22:20:21.217

7

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.

Arjan

Posted 2009-09-21T07:42:42.470

Reputation: 29 084

4

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

Levi Figueira

Posted 2009-09-21T07:42:42.470

Reputation: 142

4

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

Benjamin Schollnick

Posted 2009-09-21T07:42:42.470

Reputation: 4 313

@Benjamin, It's Command+Shift+Q – notnoop – 2009-11-17T15:01:36.310

2

According to Király in Apple Support Discussions, he says this combination works:

...This can be done with Automator and Spark:

Open Automator, select Run Shell Script, and paste in this (all on one line):

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

File -> Save As... -> select "Save as application" and save the app in a convenient spot. Then use Spark to assign a keyboard shortcut to open that app.

I use Automator and Spark to switch to the login window and then put my Mac to sleep when I press control-F13...

About Spark 3.0b9 software:

Spark is a powerful, and easy Shortcuts manager. With Spark you can create Hot Keys to launch applications and documents, execute AppleScript, command iTunes, and more... You can also export and import your Hot Keys library, or save it in HTML format to print it. Spark is free, so use it without moderation!

in a small town

Posted 2009-09-21T07:42:42.470

Reputation:

PS: You could also see what's up with other suggested scripts already out there: • AppleScript: Account Switcher Applet http://www.macosxautomation.com/applescript/accountswitcher/index.html

– None – 2010-02-09T08:21:51.597

This answer is way too big and quite confusing to read, and you don't really need Sparkat least for Snow Leopard. Just create that on Automator as a Service and add a shortcut to it on System Preferences. I'll add an answer myself if you don't fix yours, smalltown-dude. ;) – cregox – 2010-04-13T19:30:22.383

1

A slightly different way of doing this, with added security benefits (or annoyances depending on your view-point), is to

  1. use the screen saver to lock the computer (allowing user switching from the lock screen)
  2. use "hot-mouse-corners" to enable fast locking/screen-saver activation

To do this, you need to adjust the following settings in your System Preferences application:

  • Under Exposé & Spaces, in the Exposé tab, set one of your Active Screen Corners to Start Screen Saver.
  • Under Security in the General tab, check the option of Require password ... after sleep or screen saver begins (you may want to choose immediately from the drop-down).

You'll have to do the latter on each account (unfortunately there's no global setting for this).

Then you can move your mouse to the appropriate corner of the screen to lock the computer (by starting the screen saver). When someone presses a key/moves the mouse they'll be prompted to log in and can switch users from there.

drfrogsplat

Posted 2009-09-21T07:42:42.470

Reputation: 357

1

If the desire is to lock (like Window-L in Windows), then there's a very simple solution .. set your screensaver to lock the screen (require logon) and use ctrl-shift-eject (bottom left two keys + top most right key). This locks your screen.

user51303

Posted 2009-09-21T07:42:42.470

Reputation: 36

1

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.

oschrenk

Posted 2009-09-21T07:42:42.470

Reputation: 111

1

nik

Posted 2009-09-21T07:42:42.470

Reputation: 50 788