Apple Event: May 7th at 7 am PT

Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Automatically closing a scripted app

I have an application which needs to be started with options to enable some additional functionality and I wanted to be able to start it from Launchpad, or the Dock or Spotlight in the same way as the app without the options. Thought the easiest way to do this would be to write a script and save it as an app which I've done. Actually it's just a single command

do shell script "binary -option"

where "binary" is the executable in the MacOS folder of the target app.


This does the job of starting the target app but it leaves my scripted app open and unresponsive until I close the target app. This seems a bit untidy and must also be using resource. What is the best way to get the scripted app to close automatically as soon as it has opened the target app?

iMac 27″, macOS 10.14

Posted on Oct 7, 2020 4:35 PM

Reply
Question marked as Best reply

Posted on Oct 8, 2020 1:24 PM

This should work - to explain the process, ordinarily do shell script executes the command and waits for the response/output from that command - that's useful if you want to capture the output of the shell command in your script flow.


Technically, do shell script waits for the stdout stream to close as an indicator that the command has finished.


Appending & to the end of the command tells the shell to execute in the background, and to return control to the caller. This is useful if you want the command to execute in parallel and you don't care about what (if any) data the command returns.


The second form, ' > /dev/null 2>&1 &' goes one step further.

Shell commands actually return two separate outputs known as standard output (stdout) and standard error (stderr). AppleScript will normally wait for both of these to close as an indicator that the command has finished. This somewhat arcane syntax redirects both stdout and stderr to /dev/null and returns control back to the caller immediately.

It's impossible in this form to capture any output from the command, but you don't need it here - control will return to the AppleScript which can promptly quit leaving the process running.

Similar questions

4 replies
Question marked as Best reply

Oct 8, 2020 1:24 PM in response to Marco Klobas

This should work - to explain the process, ordinarily do shell script executes the command and waits for the response/output from that command - that's useful if you want to capture the output of the shell command in your script flow.


Technically, do shell script waits for the stdout stream to close as an indicator that the command has finished.


Appending & to the end of the command tells the shell to execute in the background, and to return control to the caller. This is useful if you want the command to execute in parallel and you don't care about what (if any) data the command returns.


The second form, ' > /dev/null 2>&1 &' goes one step further.

Shell commands actually return two separate outputs known as standard output (stdout) and standard error (stderr). AppleScript will normally wait for both of these to close as an indicator that the command has finished. This somewhat arcane syntax redirects both stdout and stderr to /dev/null and returns control back to the caller immediately.

It's impossible in this form to capture any output from the command, but you don't need it here - control will return to the AppleScript which can promptly quit leaving the process running.

Automatically closing a scripted app

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.