Skip to content

Instantly share code, notes, and snippets.

@oschrenk
Created June 26, 2012 13:18
Show Gist options
  • Save oschrenk/2995746 to your computer and use it in GitHub Desktop.
Save oschrenk/2995746 to your computer and use it in GitHub Desktop.
Keyboard Shortcut for Fast User Switching
--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>", where "<user>" is the other user's name.the field "Kind" must be "User Login" (without quotes).
--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
@jaspreetsidhu
Copy link

Renamed your script as Switch to Test.scpt

Event log from AppleScript

@oschrenk
Copy link
Author

Why did you change

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

to

set username to word -1 of my findReplace(".scpt", "/Users/Jaspreet/Documents/AppleScripts", (path to me as text))

?

The function findReplace states that the second parameter is replaceText the text that will be inserted in place of findText. Don't do it. I overlooked that,because I assumed you didn't make changes to the script.

Please try again with the correct version of the script.

@jaspreetsidhu
Copy link

I knew I was doing something stupid... again, new to AppleScript... I thought that I was supposed to... works like a charm right now... thanks for all your help and assistance!

@oschrenk
Copy link
Author

No worries.

@jaspreetsidhu
Copy link

The script is working in the sense that it switches the user, but occasionally, the system "hangs" and refuses any additional keyboard input... this only happens when I invoke the script.

I have the output with a few errors here:

tell current application
path to current application as text
--> "OSX:Library:Scripts:System:Switch to Test.scpt"
do shell script "/System/Library/CoreServices/'Menu Extras'/User.menu/Contents/Resources/CGSession -switchToUserID id -ur Test"
--> ""
end tell
tell application "System Events"
do shell script "stat -f %Su /dev/console"
--> error number -1708
«event ascrgdut»
--> error number -1708
do shell script "stat -f %Su /dev/console"
--> error number -10004
end tell
tell current application
do shell script "stat -f %Su /dev/console"
--> "Jaspreet"
end tell
tell application "System Events"
do shell script "security find-generic-password -g -s "Test" -D "User Login" 2>&1 1>/dev/null | sed -e 's/password: "//' -e 's/"//'"
--> error number -10004
end tell
tell current application
do shell script "security find-generic-password -g -s "Test" -D "User Login" 2>&1 1>/dev/null | sed -e 's/password: "//' -e 's/"//'"
--> "password"
end tell
tell application "System Events"
exists window 1 of application process "SecurityAgent"
--> true
set value of text field 1 of window 1 of process "SecurityAgent" to "password"
key code 36
end tell

@jaspreetsidhu
Copy link

Also, how do I enable markdowns in as in your replies?

@oschrenk
Copy link
Author

How do you invoke the script?

Sometimes the script will behave strange. This is not a problem with the script though in my opinion. I run it by invoking it via Quicksilver and sometimes it will look like it was invoked multiple times opening the SecurityAgent window multiple times. This occurs maybe 1% of the time so I can live with it.

RIght above the comment box there is a link to GitHub Flavored Markdown which will help you understand how Github deals with Markdown.

@jaspreetsidhu
Copy link

either using fast script or alfred (not both)

What are the following errors being caused by?

--> error number -1708
«event ascrgdut»
--> error number -1708
do shell script "stat -f %Su /dev/console"
--> error number -10004

*and yes, realized that as soon as I had pasted it... luckily, a test account, literally, but even though I updated the comment, is the previous revision still available?

@oschrenk
Copy link
Author

I don't know what these errors are, but a quick google search offers this and many others.

I have no time to investigate more. As the script works for me, I'll have to leave it to you.

@strangeluck
Copy link

Running 10.10.2, I had to change "-s" to "-l" in the line "set pswd to (do shell script..." for it to successfully find the key, but otherwise, I had no problems. This was great. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment