On a mac, how can I force-close a window of an app (NOT the app itself)

keyboardshortcut

I'm looking for a keyboard shortcut to close a window of an app, skipping the "Would you like to save" pester dialog, while leaving the app, itself, and other windows, still open.

If there's no way to do that, is there a keyboard shortcut for hitting a certain button in a window/dialog? In Windows, this would typically be ALT+[first letter of the button text].

Best Answer

To press a button in a dialog using a keyboard shortcut, press Command, followed by the first letter of the action you wish to trigger. The exception to this rule is if the action you wish to activate will delete a file or changes thereto, in which case the shortcut is Command-delete.

To force-close a window, you'd need to automate the correct series of key presses using AppleScript or something similar. The first key combination would likely be Command-W (though not even this is entirely universal). However, the dialog shortcuts vary by application—older versions of Microsoft Word don't comply with the Command-delete behavior (they use Command-D), while Automator's save sheets can often only be closed through mouse input. You would have to tailor this behavior to the specific applications in which you're interested. Such an AppleScript might look something like this (though be warned, triggering Command-delete without knowing the context is an easy way to accidentally delete files and large swathes of text):

tell application "System Events"
    keystroke "w" using {command down} -- Command-W
    delay 0.5 -- Wait for the dialog to appear
    key code 51 using {command down} -- Command-delete
end tell

To tie an AppleScript to a keyboard shortcut:

  • Open Automator (Applications > Utilities > Automator) and create a new Service (optionally, configure it just to run in the applications you want).
  • Add a "Run AppleScript" block in which you write your AppleScript.
  • Open System Preferences > Keyboard > Shortcuts.
  • Create a new shortcut, where the "Menu Title" is the title you gave your Service (again, you can tie this shortcut just to the applications to which your Service applies). The keyboard shortcut you enter here is the one you can use to trigger your "force close" behavior.

You could create multiple services, each one with the same keyboard shortcut but in different applications, corresponding to the unique behavior of each.