I'm trying to figure out how to launch a GUI application as another user that's logged in interactively, in that user's graphical session.
For example, say I have two users, foo and bar. Both are logged in, but the current interactive user is foo. I'd like to launch Calculator.app as user "bar", so that when I fast user switch to bar, I find the Calculator window is open in bar's session.
Here's what I've tried that doesn't work:
sudo -u bar /Applications/Calculator.app/Contents/MacOS/Calculator
This launches Calculator.app as bar, but the window opens in foo's graphical session.
sudo -u bar osascript -e "tell application \"Calculator\" to activate"
sudo -u bar open "/Applications/Calculator.app"
Launches Calculator as foo, not bar.
launchctl asuser [uid of bar] [any of the above commands]
Is there any way to accomplish this? I'm willing to entertain all manner of possible solutions, including bash scripting, AppleScript, writing a Core Foundation or Cocoa program, and so on. In my situation, any program or script could execute as any user, including root.
Note: I'm aware that it's possible using remote Apple Events, but I can't use that since in the situation I'm trying to do this I have no guarantee that "Remote Apple Events" will be enabled in Sharing preferences.
Any help would be greatly appreciated!