0

I'm trying to force quit my Terminal.app using Apple Script Editor

tell application "Terminal"
    quit
end tell

tell application "System Events"
    key code 53
end tell

It does not seem to work. How would one go about and did that ?

0

It looks like I found the answer to my owned question

tell application "Terminal"
    quit
    tell application "System Events"
        keystroke return
    end tell
end tell
0

Sending a quit command and a keystroke doesn't equate to a force-quit. I assume that the solution you settled upon appeared to work simply because, after being issued a quit command, I'm guessing a dialog box popped up asking whether you'd like to force-quit Terminal. Hitting Enter—either physically or programmatically—would then select the force-quit option presented to you.

The problem with sending a key press programmatically is that, if the focus unexpectedly changed to another window on your system, that key press would be sent to the wrong window.

One bonafide method of force-quitting an app from AppleScript is to obtain its PID, then issue a kill command via a shell script. It's actually just a single line of code. Here's what it would look like when used to deal with your situation (formatted over two lines for readability):

    tell application "System Events" to ¬
    do shell script "kill -9 " & unix id of process "Terminal"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.