user5153901
user5153901

Reputation:

How to open and close apps using bash in MacOS

I want to open an app(.app) from a bash shell(.sh using #!/usr/bin/bash) which is located in a folder in /Applications. How do I open it?
If I can open it, can I close it? If yes, how?

Upvotes: 3

Views: 9614

Answers (2)

DL chyi
DL chyi

Reputation: 11

of course you can do that, please use the terminal command "open".

#!/bin/bash 
echo This is a play music script

# play Music from command line in the background
open ~/Downloads/AlanWalker-Faded.m4a & 

# wait some seconds 
sleep 60 

# force quit iTunes 
killall iTunes

Upvotes: -1

RobC
RobC

Reputation: 25042

Setting your shebang to #!/usr/bin/env bash (which is the preferred way as it's portable), consider the following examples:

Open app:

  • Utilize open command with the -a option. For instance:

    open -a "Safari"
    
  • Or, using osascript to execute an AppleScript. For instance:

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

    Or a terse equivalent:

    osascript -e 'activate app "Safari"'
    

Close app:

  • Utilize osascript to execute an AppleScript. For instance:

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

    Or a terse equivalent:

    osascript -e 'quit app "Safari"'
    

Note: If bash actually resides in /usr/bin/ on macOS as per your question the above examples will work successfully with the shebang: #!/usr/bin/bash

Upvotes: 9

Related Questions