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?
Asked
Active
Viewed 6,021 times
2 Answers
9
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

RobC
- 22,977
- 20
- 73
- 80
-
1Is osascript better or open better? – Mar 12 '19 at 09:34
-
2Aaron, in terms of _"better"_ there's really little difference between the two given your requirement - both achieve the desired result on macOS. If I had to make a choice, I would probably choose to use `osascript -e 'activate app "Safari"'` and `osascript -e 'quit app "Safari"'`, simply because of the similarity of syntax for both opening and closing - and perhaps because of its marginal improved readability. That being said, `open` doesn't have to call _AppleScript_ which inevitably results in a _very minor_ performance gain (probably microseconds faster)... Toss a coin ;) – RobC Mar 12 '19 at 10:50
-1
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

DL chyi
- 11
- 1
- 1
-
Sorry, but this opens a music file, which just opens the default music app(iTunes in this case) – Mar 12 '19 at 09:34