how to close an active document in Mac OS using shell script

317 Views Asked by At

I am closing active document using apple script as below

  tell application "Microsoft Word"
                activate
                try
                    if not (exists active document) then error number -128

                    close active document saving yes

                on error

                end try
            end tell

want to do similar action using shell script. I want to gracefully close it and don't want to use kill command . And I don't want to use osascript to call an apple script . I want a graceful way using native shell commands

1

There are 1 best solutions below

3
On

Hi you can use osascript in the shell code

#!/bin/sh
osascript <<EOF
tell application "$1"
  close (every window whose name is "$2")
end tell
EOF

Compile this code and make filename.sh

or you can use

#!/bin/sh
osascript <<EOF
tell application "Preview"
  close (every window whose name is "$1")
end tell
EOF

To use this from cmd type $./file_name Preview application name

Here I used the reference of https://ss64.com/osx/osascript.html