Yes, I tested it, and yes, it works.
If you had actually used the code that I had provided, it would have worked fine.
Instead, you just used half of it and complained that it didnāt work.
Since you apparently didnāt understand what I wrote, I will explain it piece by piece.
APP="DEVONthink 3"
Step one assigns a variable to the name of the app. This is only necessary to avoid re-typing the name of the app. It could also be replaced with APP="$1"
if you wanted to generalize the script to work with any other app that respects AppleScript.
&&
tells it to continue only if the previous command is successful.
pgrep -xq "$APP"
Step two checks to see if $APP
(aka āDEVONthink 3ā) is running.
&&
tells it to continue only if the previous command is successful. If pgrep
does not return an exit code of zero, it will stop here.
osascript -e "tell application \"$APP\" to quit"
Step three tells $APP
to quit, using AppleScript.
Also, if you are trying to run AppleScript as root
but are not logged in and running DEVONthink 3 as root
then it will not work. The shell command and AppleScript have to be run by the same user that is running DEVONthink 3.
You can watch a screencast of this code working. The first time it is run, it quits DEVONthink 3. The second time, it does nothing, because it is not running.
(If the embed does not work, you can find it at https://vimeo.com/613682526: