Here are my files:
tell application ":path:to:applescript:apps:shell-script-launcher.app" to launch
do shell script "say starting script"
runcommand launches and runs an application hidden
activatecommand launches, runs, and activates the application (makes it the frontmost application)
launch, according to Apple, "Launches an application, if it is not already running, but does not send it a run command."
on runhandler is NOT invoked), but in practice that is not true up to 10.9 - see below.
A script can send commands to a script application just as it can to other applications. To launch a non-stay-open application and run its script, use a launch command followed by a run command, like this:
launch application "NonStayOpen"
run application "NonStayOpen"
The launch command launches the script application without sending it an implicit run command. When the run command is sent to the script application, it processes the command, sends back a reply if necessary, and quits.
launch by itself is enough to run the application and is indeed the only command that works with AppleScript-based applications.
Any attempt to execute
activate (whether in addition to
launch or not) runs the application - even twice when run from AppleScript editor(!; just once with
osascript) - but reports failure
<appName> got an error: Connection is invalid.
This strikes me as bug.
Not sure how OSX versions <= 10.7 behave.
Note: I've witnessed the non-executing behavior with
launch once, but every non-stay-open AppleScript-based test app I've created from scratch on OS X 10.9.2 and OS X 10.8.5 also executes the script with
launch - contradicting what the documentation says.
Please let me know if your system behaves differently and/or how older versions behave. On what OSX version was the app that doesn't execute with
On OSX 10.10, behavior is consistent with the documentation, with one thing worth noting:
run applicationis sufficient - no need for a separate
launch applicationcommand first.
do shell scriptwith the standard
openutility to bypass the problem - this should work, regardless of whether the application is AppleScript-based or not:
do shell script "open " & ¬ quoted form of POSIX path of ¬ alias ":path:to:applescript:apps:shell-script-launcher.app"
If you know the type of application you're invoking up front:
run script file, as @regulus6633 recommends - this has the added advantage that the invoked AppleScript-based application can return objects directly to the caller:
run script file ":path:to:applescript:apps:shell-script-launcher.app"
Note: There's also
load script file, which indeed lets you merely load script code without executing it right away.
activateto run the app hidden / frontmost:
run application ":path:to:applescript:apps:shell-script-launcher.app"
runeven with AppleScript-based applications and simply ignore errors with
try ... end try, as @atonus suggests - the downside is that you won't be able to detect actual failure to invoke the application.
You can mitigate this by selectively ignoring only the specific
Connection invalid error (which assumes this error would not legitimately occur) [no longer needed on OSX 10.10]:
try run application "Macintosh HD:Applications:_Sandbox-AppleScript0.app" on error number -609 # 'Connection is invalid' error that is spuriously reported # simply ignore end try
launchcommand (though that didn't work for the OP, possibly due to working on a <= 10.7 OSX version):
launch application ":path:to:applescript:apps:shell-script-launcher.app"
However, that is not advisable for two reasons:
launchbehavior to no longer execute also, so your code will break when run there.
launch, the documentation says that AppleScript "does not send it a run command" and "allows you to open an application without performing its usual startup procedures, such as opening a new window" - what that exactly means is not clear and different applications seem to handle this differently.