How to wait for an app to quit in a macOS bash script

Share this:

Shortcuts now runs on macOS since the Monterey release and there’s always been AppleScript as well. However, I just still prefer the simpler bash scripts for all the automation of tasks. You can pretty much do anything on the terminal, like when you need to perform more complex conditions, such as waiting for an app to quit before continuing with the rest of the script.

Wait for an app to quit in a macOS bash script

With Linux, you can use the jobs command to the get PIDs and then use wait with the PID. But on macOS, it’s a lot simpler to do get this done. Simply use the –wait-apps parameter with your open command. That’s it!

open --wait-apps /path-to/the-app

Here’s an example of how I’ve used it to in one of my automation steps for my kids to play multiplayer minecraft on a single Microsoft account.

networksetup -setairportpower en0 on
echo "### Launching Minecraft and waiting for it to quit"
open --wait-apps /Applications/

echo "### Minecraft app quit. Continuing with the rest of the script"
echo "### Changing user to ##########"
sed -i '' 's/ExistingProfileName/NewProfileName/g' ~/Library/Application\ Support/minecraft/launcher_accounts.json

networksetup -setairportpower en0 off

open /Applications/

In the script above, the open –wait-apps will launch the app, and you will see the script paused at that point until the app it had opened quits.

Wait for an app to quit in a macOS bash script

As you can see from the example above, this is a really simple yet useful way to automate task where you need to wait for an app to quit in a macOS bash script.

Hope this short little article has been helpful to you in your scripting on macOS!

If this post has been useful, support me by buying me a latte or two 🙂
Buy Me A Coffee
Share this:

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.