An AppleScript to ease fast user switching

Apr 08, '08 07:30:00AM

Contributed by: wackazong

With kudos here and there, I now present my working version of an easy Fast User Switching AppleScript that switches passwords with a double-click, and even types the password for you.

  1. Enable support for Assistive Devices in the Universal Access System Preferences panel.
  2. Change the password string to the password of the user account you want to switch to.
  3. Name the script like the short username of the user you wish to switch to.
  4. Save the script as an executable, to allow easy double-click operation.
I use two users (with the same password) on my computer for different usage types (one for general stuff, the other for music production). I keep two versions of the script in the Applications folder, each assigned to one user. In this way, I just have to type the user name I want to switch to in Spotlight, press Return, and voila.

The script:
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
For your convenience, I've also created an AppleScript application bundle version (32KB download), complete with icon. Just open like a normal Script in Script Editor (in 10.5 at least), put in your password, rename to the short user name, and enjoy the icon.

[robg adds: I haven't tested this one, and note that storing account passwords in a plain text file on your drive is at least something of a security risk. You could mitigate that issue somewhat by keeping your scripts on an encrypted disk image, though this will make using the scripts a bit less convenient.]

Comments (7)


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