Skip to content

Instantly share code, notes, and snippets.

@rtrouton
Created March 25, 2016 17:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rtrouton/a4e819a85585406de9f8 to your computer and use it in GitHub Desktop.
Save rtrouton/a4e819a85585406de9f8 to your computer and use it in GitHub Desktop.
For OS X 10.9.x and earlier - Script to launch Safari as the logged-in user from a script run with root privileges
#!/bin/bash
# Identify the username of the logged-in user
logged_in_user=`python -c 'from SystemConfiguration import SCDynamicStoreCopyConsoleUser; import sys; username = (SCDynamicStoreCopyConsoleUser(None, None, None) or [None])[0]; username = [username,""][username in [u"loginwindow", None, u""]]; sys.stdout.write(username + "\n");'`
# Identify the loginwindow PID of the logged-in user
logged_in_user_loginwindow_pid=$(ps auxww | grep "/System/Library/CoreServices/loginwindow.app/Contents/MacOS/loginwindow" | grep "$logged_in_user" | grep -v "grep" | awk '{print $2}')
/bin/launchctl bsexec "$logged_in_user_loginwindow_pid" /usr/bin/open "/Applications/Safari.app"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment