How to force close terminal window id using AppleScript?

332 Views Asked by At

I am starting a new terminal window from a terminal:

osascript -e 'tell app "Terminal" to do script "myProgram.sh"'

Since the new process is not ending(it is basically a server), I want to be able to stop it at some point using AppleScript.

If I do:

osascript -e 'tell app "Terminal" to close window id {windowId}'

I am getting the alert box "Do you want to terminate running process...?".

Is it possible to force close the window id? If not, how can I confirm this alert box using AppleScript?

2

There are 2 best solutions below

0
On

You could try this:

do shell script "killall -QUIT Terminal"

I hope this helps you solve your problem .

0
On

What you want to do is use osascript to use the dialog and press Terminate:

osascript -e 'tell application "Terminal" to close first window' \
          -e 'tell application "System Events" to tell process "Terminal"' \
            -e 'tell window 1' \
              -e 'tell sheet 1' \
                -e 'click button "Terminate"' \
              -e 'end tell' \
            -e 'end tell' \
          -e 'end tell'

Working for me, stolen from https://stackoverflow.com/a/37103658/9259703.