Launch an app after installation

Sonic84
Contributor III

Hello, How do I have an app launched after it is installed by Casper remote without user interaction?

Running "open /path/to/app.app" or "sudo -u [shortname] open /path/to/app.app" in a pkg postflight.sh didn't work.

Running "sudo -u [shortname] open /path/to/app.app" from the advanced tab in Casper remote works sometimes, however running this command from a script does not.

Any help is appreciated.

Thank you!

1 ACCEPTED SOLUTION

acidprime
New Contributor III

Try using the bsexec command with launchctl, typically this behavior is due to how the every* (i.e. every15) policies run (via launchd) and so you need to get out of that context. Either way this may help with Casper Remote as well.

#!/bin/bash
declare -x UserName="$(/usr/bin/who | /usr/bin/awk '/console/{print $1;exit}')"
echo "Derived Console username: $UserName"
declare -x LoginWindowPID="$(/bin/ps -axww | /usr/bin/grep loginwindo[w] | /usr/bin/awk '/console/{print $1;exit}')"
echo "Found PID: $LoginWindowPID"
/bin/launchctl bsexec "${LoginWindowPID:?}" /usr/bin/sudo -u "$UserName" /usr/bin/open /Applications/Calculator.app

https://gist.github.com/2210258

Just a note on @jarednichols comment, that should typically work as well, however be aware that is running the process as the root user if you omit the "open" and would block the procedural execution of your script until the app is complete. Leaving the open should not be much different then /Applications/Calculator.app , as in a way both basically execute the same process.

View solution in original post

8 REPLIES 8

jarednichols
Honored Contributor

Errors thrown?

Sometimes you need to go lower than the app bundle. e.g. open /Applications/Firefox.app/Contents/MacOS/firefox

acidprime
New Contributor III

Try using the bsexec command with launchctl, typically this behavior is due to how the every* (i.e. every15) policies run (via launchd) and so you need to get out of that context. Either way this may help with Casper Remote as well.

#!/bin/bash
declare -x UserName="$(/usr/bin/who | /usr/bin/awk '/console/{print $1;exit}')"
echo "Derived Console username: $UserName"
declare -x LoginWindowPID="$(/bin/ps -axww | /usr/bin/grep loginwindo[w] | /usr/bin/awk '/console/{print $1;exit}')"
echo "Found PID: $LoginWindowPID"
/bin/launchctl bsexec "${LoginWindowPID:?}" /usr/bin/sudo -u "$UserName" /usr/bin/open /Applications/Calculator.app

https://gist.github.com/2210258

Just a note on @jarednichols comment, that should typically work as well, however be aware that is running the process as the root user if you omit the "open" and would block the procedural execution of your script until the app is complete. Leaving the open should not be much different then /Applications/Calculator.app , as in a way both basically execute the same process.

talkingmoose
Moderator
Moderator

You want:

open -a /path/to/app.app

acidprime
New Contributor III

@talkingmoose
I have never used -a, like this, and I'm not sure it does anything different.
Typically its for the scenario when you want to open a specific file with a specific app
open /etc/resolv.conf -a /Applications/TextEdit.app

Both:
open /Applications/TextEdit.app
open -a /Applications/TextEdit.app

Do the same thing on my system, have you seen them act differently?

Sonic84
Contributor III

Thanks acidprime! That code did the trick.

tlarkin
Honored Contributor

Hey everyone,

I hope your weekends went well. As always it was too short. There are many ways to open apps via the command line. The open command itself has lots of switches because you can tell it to open a file in a specific application, which is why the -a switch exists.

Another option is a one liner apple secript:

osascript -e 'tell application "Safari" to activate'

Thanks,
Tom

mm2270
Legendary Contributor III

I can't be sure, but there may be an issue with doing this via Casper Remote. I tried using all of the above methods (open /path/to/application, open -a /path/to/application and osascript -e 'tell application "App Name" to activate') and they all fail with "An error of type -10810 has occurred. (-10810)"
Has anyone else seen this? This is when plugging these into the Advanced tab.

Oddly enough, SSH'd in, direct on the Mac in Terminal and via a policy (sudo jamf policy -trigger) all of the above work fine and launch the app. Strange! What is Casper Remote doing that is causing it to balk I wonder?

talkingmoose
Moderator
Moderator

Casper Remote is running under a local admin account on the remote machine. If the Finder is running under that local admin account then GUI applications should launch under the local admin account's login session.

However, sending a command using the remote machine's local admin account won't launch an application for "jcool" under his login session.