Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

10.7: Keyboard Shortcut for Fast User Switching System 10.7
I have been using this hint until it broke in Lion. (See the last comment in that hint.)

After not being able to find a complete solution, I came up with this:

*Please note that this is not a secure solution. Your account password is stored in plain text, just as in the original hint.*

There is a way to collect the password from the keychain (see the above link and read through the comments), but I don't quite understand that just yet.

Also, this comes with the usual use at your own risk and I am not responsible if your computer explodes warnings.
  • Open Automator
  • Select Application
  • In the left column select Utilities
  • In the centre column drag Run AppleScript to the area on the right
  • Copy and Paste this code, changing USERNAME and PASSWORD accordingly:
      on run {input, parameters}
      set theUser to "USERNAME"
        set theUser to do shell script "/usr/bin/id -u " & theUser
        do shell script "/System/Library/CoreServices/Menu\\ Extras/User.menu/Contents/Resources/CGSession -switchToUserID " & theUser
        tell application "System Events"
          set token to "1"
          repeat while token = "1"
            if exists process "SecurityAgent" then
              delay 1
              keystroke "PASSWORD"
              keystroke return
              set token to "0"
            end if
          end repeat
        end tell
        return input
      end run
      
  • Go to the File menu and select Save...
  • Make sure the File Format is Application
  • Type in the name you select where you wish to save the App.
  • Assign a keyboard short cut in your favourite launcher App. In my case, I have assigned Control+Option+a with Quicksilver.

[crarko adds: This does work, but pay attention to the caveat about storing the plain text password. If you extend this to get the password from the Keychain please post your addition in the comments.]
    •    
  • Currently 3.25 / 5
  You rated: 4 / 5 (4 votes cast)
 
[15,902 views]  

10.7: Keyboard Shortcut for Fast User Switching | 9 comments | Create New Account
Click here to return to the '10.7: Keyboard Shortcut for Fast User Switching' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
10.7: Keyboard Shortcut for Fast User Switching
Authored by: mr. applescript on Aug 30, '11 09:15:47AM

Why save the Automator workflow as an application? Why not create an Automator service and assign a keyboard shortcut to the service?

For an overview of Automator and services, watch this short video: <http://www.macosxautomation.com/services/video/services-001HD.html>
at <http://www.macosxautomation.com/services/>

Also, you don't need the repeat.. while. Just use a standard repeat loop and then replace the line:
set token to "0"
with
exit repeat

Cheers!



[ Reply to This | # ]
10.7: Keyboard Shortcut for Fast User Switching
Authored by: MalEbenSo on Aug 30, '11 02:32:20PM

Or just use Spark for this and other keyboard shortcuts.

Free and works like a charme.



[ Reply to This | # ]
10.7: Keyboard Shortcut for Fast User Switching
Authored by: Sesquipedalian on Aug 30, '11 03:20:46PM
This script will do the job while keeping the other user's password stored safely in the keychain. Follow the instructions in the comments at the beginning to set it up properly.


--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 "User's Local Password", where "User" is the other user's name. 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 & "'s Local Password\" 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 group 1 of window 1 to pswd
			click button 2 of window 1 of application process "SecurityAgent"
			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
	#if application process "KeyboardViewerServer" exists then tell application "KeyboardViewerServer.app" to quit
	#if application process "CharPaletteServer" exists then tell application "CharPaletteServer.app" to quit
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


[ Reply to This | # ]
10.7: Keyboard Shortcut for Fast User Switching
Authored by: baltwo on Aug 30, '11 06:21:59PM

Note that Keychain Scripting isn't carried over within Scripting Additions when Lion's installed. You'll need to get it from your Snow Leopard installation if, in fact, it's called with this. Belongs in Lion's /System/Library/ScriptingAdditions/ directory.



[ Reply to This | # ]
10.7: Keyboard Shortcut for Fast User Switching
Authored by: Sesquipedalian on Aug 31, '11 09:17:21AM
The script uses the security command line tool, not Keychain Scripting. I see that I did forget to change that in the instructional commits, though.

[ Reply to This | # ]
10.7: Keyboard Shortcut for Fast User Switching
Authored by: baltwo on Aug 31, '11 04:28:16PM

That's why I brought it up.



[ Reply to This | # ]
10.7: Keyboard Shortcut for Fast User Switching
Authored by: oschrenk on Feb 17, '12 05:28:50AM
I'm getting this error (I'm on 10.7.3, fast user switch enabled, access for assistant device enabled):
error "„System Events“ hat einen Fehler erhalten: „group 1 of window 1 of process \"SecurityAgent\"“ kann nicht gelesen werden. Ungültiger Index." number -1719 from group 1 of window 1 of process "SecurityAgent"
Which can be translated into
error "„System Events“ has thrown an erroe: „group 1 of window 1 of process \"SecurityAgent\"“ cannnot be read. Invalid Index." number -1719 from group 1 of window 1 of process "SecurityAgent"
DOes anybody know how to fix this?

[ Reply to This | # ]
10.7: Keyboard Shortcut for Fast User Switching
Authored by: oschrenk on Feb 17, '12 07:05:59AM
I had some problems with the script using 10.7.3. a) find-generic-password had problems finding the keychain item b) apple script could paste the password into the security agent password field c) the security agent has no button anymore, so I had to send "return"
--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


[ Reply to This | # ]
10.7: Keyboard Shortcut for Fast User Switching
Authored by: oschrenk on Jun 26, '12 06:28:41AM

There were some users having trouble to set this up. The common mistake was to set the Description field to "User Login". It needs to be the "Kind" field. My error. As I can't seem to edit the answer I posted a public gist to https://gist.github.com/2995746.

If there are still problems discussion can be made there.



[ Reply to This | # ]