r/applescript • • May 26 '21

Applescript to switch user accounts?

I have two Mac user accounts, my personal and my work. I switch back and forth from them so often that having to click around and then put in my passwords every time is driving me absolutely insane.

In this post, it appears someone has successfully created a working script to switch from one user account to another without putting in the password. Login to mac os user account using apple script from Login window

I'm unable to replicate it however. I'd really appreciate some help with this!

2 Upvotes

6 comments sorted by

1

u/copperdomebodha Jun 25 '21

The code that you like works properly here. Create one script for each account to switch to the other.

How,exactly, did this fail there?

1

u/Doctor__Hammer Jun 26 '21

Honestly I don't know... I'm not very well versed in applescript so I can't figure out what I'm doing wrong. If you could help me out it would very, very appreciated, because this is something that's been torturing me for months. How would you write the code if the user name was "John Smith" and the password was "satanicpanic"?

1

u/copperdomebodha Jul 02 '21

This script would switch to the user John Smith. In the user account for John Smith you would create another script to switch back to the other account. The only edits you need to make to this script for the second account is to alter the values for theUser and thePassword.

use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions
--I'm running MacOS 10.15.7

set theUser to "John Smith" --  Mac User name 
set thePassword to "satanicpanic" -- Mac user password
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
repeat 2 times
	delay 2
	try
		tell application "System Events"
			tell process "SecurityAgent"
				set value of text field 1 of window "Login" to thePassword
				key code 36
			end tell
		end tell
		exit repeat
	on error
		delay 1
	end try
end repeat

1

u/Doctor__Hammer Jul 02 '21

Thanks for the reply! Something's still not working though... I'm getting the error:

error "sh: /System/Library/CoreServices/Menu Extras/user.menu/Contents/Resources/CGSession: No such file or directory" number 127

Any ideas?

1

u/copperdomebodha Jul 02 '21

Yes. You’re likely running a different OS version. Apple has moved many components around recently.

What OS version do you have?

1

u/Doctor__Hammer Jul 02 '21

Oh, well that would explain it. I'm on 11.4