You can make a difference in the Apple Support Community!

When you sign up with your Apple Account, you can provide valuable feedback to other community members by upvoting helpful replies and User Tips.

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

How make bash script using osascript to close app from command line

When executed in Terminal, a command like the following will close the named, open, app:


osascript -e 'quit app "BBedit"'


I created the following bash script, in file closeapp.sh and made it executable (chmod a+x closeapp.sh).


#! /bin/bash
# gracefully close app 
osascript -e 'quit app "$1"'


If I try it...


closeapp.sh BBedit


... then nothing happens.


What's wrong?

Posted on Jul 29, 2020 8:47 AM

Reply
Question marked as Top-ranking reply

Posted on Jul 29, 2020 9:22 AM

osascript is very complicated with shell variables because AppleScript uses double quotes and single quotes.

You have to escape the quotes:

#!/bin/bash
# gracefully close many apps 
for a in "$@"
do
	osascript -e 'quit app "'"$a"'"'
done

See here for more info and how to prevent command line injection: https://stackoverflow.com/questions/23923017/osascript-using-bash-variable-with-a-space/23923167#23923167

7 replies
Question marked as Top-ranking reply

Jul 29, 2020 9:22 AM in response to murrayE

osascript is very complicated with shell variables because AppleScript uses double quotes and single quotes.

You have to escape the quotes:

#!/bin/bash
# gracefully close many apps 
for a in "$@"
do
	osascript -e 'quit app "'"$a"'"'
done

See here for more info and how to prevent command line injection: https://stackoverflow.com/questions/23923017/osascript-using-bash-variable-with-a-space/23923167#23923167

Jul 29, 2020 10:00 AM in response to murrayE

The pkill command is a compiled binary that directly sends a SIGTERM (-15) signal to a process name that you provide to the script. Can be done from the command-line without the need of a script.


With osascript, you are asking it to interpret, and execute the AppleScript command syntax that tells the operating system to do the same thing as pkill. Far more overhead using osascript.


Your choice of which you use.

How make bash script using osascript to close app from command line

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