Skip to content

Instantly share code, notes, and snippets.

@davedelong
Last active August 10, 2016 13:03
Show Gist options
  • Save davedelong/56a973b142785c434335eddb99cc2b54 to your computer and use it in GitHub Desktop.
Save davedelong/56a973b142785c434335eddb99cc2b54 to your computer and use it in GitHub Desktop.
Programmatically switch to another user
#!/bin/bash
# A script to automatically switch to another user in macOS
# Invoke like this: `autologin.sh {user} {password}`
uid=`id -u $1`
/System/Library/CoreServices/Menu\ Extras/User.menu/Contents/Resources/CGSession -switchToUserID "$uid"
osascript <<EOD
-- wait for the system UI to come up
delay 2
tell application "System Events"
-- type the password
keystroke "$2"
delay 1
-- press enter
key code 36
end tell
EOD
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment