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

An AppleScript to ease fast user switching System
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: 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.]
    •    
  • Currently 2.56 / 5
  You rated: 4 / 5 (9 votes cast)
 
[18,672 views]  

An AppleScript to ease fast user switching | 7 comments | Create New Account
Click here to return to the 'An AppleScript to ease fast user switching' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
An AppleScript to ease fast user switching
Authored by: empaler on Apr 08, '08 10:26:48AM
Apple actually already has an example script that does this; it's even GUI based:
http://www.apple.com/applescript/accountswitcher/index.html

The submitted script is a bit more elegant though :)

[ Reply to This | # ]
An AppleScript to ease fast user switching
Authored by: maeks84 on Dec 29, '09 08:17:18PM

Link above now redirects to a page describing some basic Apple apps. I believe though that the script at http://www.macosxautomation.com/applescript/accountswitcher/agreement.html is the same as the one mentioned above.



[ Reply to This | # ]
Security
Authored by: MacTipper on Apr 08, '08 10:30:54AM

If you need a low-security way of keeping it secure, you could always just save it as run only. Granted, you couldn't reset the password, but, it adds a little safety.

MacTipper



[ Reply to This | # ]
Store the passwords in the Keychain
Authored by: PCheese on Apr 08, '08 11:03:56AM

That really doesn't add much security. Why not store these passwords in the Keychain and access them through Keychain scripting?



[ Reply to This | # ]
Store the passwords in the Keychain
Authored by: wackazong on Apr 08, '08 02:35:20PM

Said, done.

Here you go (just the script)

<code>
--get the name of the user to switch to from the file name
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 ""

--get the password out of the keychain
tell application "Keychain Scripting"
set myKeyChain to keychain "login.keychain"
unlock myKeyChain
set theKeyList to every key of myKeyChain
repeat with x from 1 to (length of theKeyList)
set theKey to item x of theKeyList
if the name of theKey is N and the description of theKey is "User Login" then
set thePassword to the password of theKey --grab the password
set theUserID to the account of theKey --grab the userid
exit repeat
end if
end repeat
end tell

--get the uid
set N to do shell script "/usr/bin/id -u " & N

--do the switch
do shell script "/System/Library/CoreServices/Menu\\ Extras/User.menu/Contents/Resources/CGSession -switchToUserID " & N
tell application "System Events"
--use universal access to enter the text and to click the button
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
</code>

1) Enable access for assistive devices
2) Store a key in the login keychain for each user you want to switch to. Choose "New Password", enter the username as the account, and the password as the password. Save, open again and change the description to "User Login" (you only need to do this once.
3) Enjoy secure Fast User Switching



[ Reply to This | # ]
Store the passwords in the Keychain
Authored by: wackazong on Apr 09, '08 06:48:52AM

I have now developed a variant where you can name the script either "Switch to username" or "Log out to username". If you choose "Log out to", then the current user is logged out (gracefully) after the switch (does not work of course if programs cancel the logout".

One question: To log out, I use a do shell script command to open another applescript which logs the user out after about 10 seconds (after the switch has occured). There must be a more elegant solution to this, at the moment I have to keep a separate aplication called "Log Out", which does the logout. Any ideas to contain this into one Script file?

The problem is that if you use e.g. osascript (even with &) then the logout is executed before the script can continue to the user switch section.

The Script

on run
try

--get the name of the user to switch to from the file name
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 ""
--get the username, depending on whether the prefix is "Log out to " or "Switch to "
if (characters 1 thru 3 of N) as text is "Log" then
set N to ((characters 12 thru (length of N)) of N) as string
set doLogOut to true
else
set N to ((characters 11 thru (length of N)) of N) as string
set doLogOut to false
end if

--get the password out of the keychain
set thePassword to ""
tell application "Keychain Scripting"
set myKeyChain to keychain "login.keychain"
set theKeyList to every key of myKeyChain
repeat with x from 1 to (length of theKeyList)
set theKey to item x of theKeyList
if the name of theKey is N and the description of theKey is "User Login" then
set thePassword to the password of theKey --grab the password
exit repeat
end if
end repeat
end tell

if thePassword is not "" then

DoSwitch(N, thePassword, doLogOut)

else

--get the password
repeat
display dialog "Enter the password for the targeted account:" default answer "" with icon note with title "User Password" with hidden answer
set the password_01 to the text returned of the result
display dialog "Enter the password for the targeted account again:" default answer "" with icon note with title "User Password" with hidden answer
set the password_02 to the text returned of the result
if password_01 is not password_02 then
display dialog "The two passwords do not match. Try again." with icon stop with title "User Password"
else
exit repeat
end if
end repeat

--make a new key
tell application "Keychain Scripting"
set myKeyChain to keychain "login.keychain"
make new generic key at myKeyChain with properties {name:N, description:"User Login", account:N, password:password_01}
end tell

DoSwitch(N, thePassword, doLogOut)

end if

on error error_message number error_number
display dialog error_message with icon 2 buttons {"Cancel"} default button 1
end try
end run

on DoSwitch(N, P, doLogOut)

--get the uid
set N to do shell script "/usr/bin/id -u " & N

--do the switch
if doLogOut then
do shell script "open '/Applications/Utilities/Switchers/Log Out.app'"
end if
tell application "System Events"
do shell script "/System/Library/CoreServices/Menu\ Extras/User.menu/Contents/Resources/CGSession -switchToUserID " & N
delay 0.2
repeat until exists process "SecurityAgent"
delay 0.2
end repeat
--use universal access to enter the text and to click the button
tell front window of process "SecurityAgent"
set value of text field 1 of group 1 to P
click button 2
end tell
end tell

end DoSwitch

The logout script

delay 10
tell application "System Events"
keystroke "q" using {command down, shift down, option down}
end tell



[ Reply to This | # ]
Yet another variant
Authored by: maeks84 on Dec 29, '09 08:21:46PM

Simplified determining the username and password. Just edit the script with whatever you need. Also, made it retry once if it failed to enter the password.

Begin script -
set thePassword to "password" --Set to "" if you wish to specify the password manually each time
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

if thePassword is not "" then --Password has been specified
repeat 2 times --Will only try twice before giving up
try
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
exit repeat
on error
delay 1
end try
end repeat
end if



[ Reply to This | # ]