Please allow application to quit when last window is closed

I think MANY Mac users would love if there was a feature to quit an app after they close the LAST window of that application. For example, I really don't like how when I open a picture with preview and close the window with the red x button, the preview icon still stays in my dock and clutters it up.

With BTT, I know I can assign an application to quit once I press the red x button, but then it quits all windows of that app, which I usually don't want.

I would hate this. I have quite a lot apps that are supposed to run in the background once a window is closed.

Go to the Triggers section, make a new action with "left click on red button"
As an associated action, add "Run Apple Script (blocking), and paste this script:

delay 0.2
try
	tell application (path to frontmost application as text)
		set WindowCount to get (count of windows)
	end tell
	
	
	if WindowCount is 0 then
		tell application (path to frontmost application as text)
			quit
		end tell
	end if
end try

It should do what you want.

Edit: You'll have to add a part to the script that tells the app to close the window (I didn't know BTT replaced the function of the red button). So here we go:

tell application "System Events"
	keystroke "w" using command down
end tell

delay 0.5

try
	tell application (path to frontmost application as text)
		set WindowCount to get (count of windows)
	end tell
end try

if WindowCount is 0 then
	tell application (path to frontmost application as text)
		quit
	end tell
end if

I would also add it to a keyboard shortcuts section, for ⌘w.

You can create an applescript widget in Global with the code above, it'll run in any app, maybe you need to add if - end if block to exclude some apps, such as BTT...

This is one of the typical examples of an app that should run w/o any open window xD Totally forgot about that one, you're right!

I appreciate the help from both of you so much!

I'm new at this, so I'm a bit confused. I only want specific apps to quit once their last window is closed. Such as Preview, Powerpoint, Excel, etc. So I don't believe I would want to put anything in the "All Apps" section.

So if I go to the sidebar in BTT, and choose a specific app, and under triggers do the "Leftclick red window button", then for the action use "Run apple script (blocking)", which part of Caliguvara's apple script would I use? The entire apple script you posted in your first response including the added portion? Or would I only need the part of it that Noon-Chen quoted?

Hi DrewVV,

For simplicity, I'll give a complete walkthru :slight_smile:

  1. Don't choose a specific app, instead, using conditional group so that you can specify the apps you want:
    image
  2. Specify the apps like the following:
  3. Create an applescript widget with no assigned actions, no names, no icons, so that it will be invisible on touch bar but still running if the specified apps are at the top, then paste the code:
tell application (get path to frontmost application as text)
    if (count windows) = 0 then quit
end tell
  1. Finished

Please note: if you've set to hide BTT touch bar for this app, or you want to use applications' origin touch bar layout, this will not work, because the code above doesn't even run. But there is a workaround, which is using launchd.

1 Like

@Noon_Chen did a quite good job here, but it presumes that you have a MacBook Pro with Touch Bar (I think? Or does BTT display Touch Bar settings even on Devices without?). Here is how to achieve it without using the Touch Bar. Stepp 1 and two are the same, then:

  1. Go to Named & Other Triggers

  2. Create a New Trigger that activates if a specific Conditional Activation Group is activated.


    Don't forget to chose the CAG!

  3. Add an action "Run Apple Script (async in background), and paste the script

     tell application (get path to frontmost application as text)
     	if (count windows) = 0 then quit
     end tell
     repeat until application (get path to frontmost application as text) is not "ABC" or "BCA" or "CBA"
     end repeat
    

Replace ABC, BCA and CBA with your apps, and it should work - didn't give it a field test though :wink:

Final Trigger should look like this:

1 Like

Ha! Never noticed a trigger can be activated by CAG's status, you're right~

1 Like

Hey thank you both for the complete walkthroughs. That's super kind of you both to help me out that much. I do have a MacBook Pro with a Touch Bar, yet I mostly use it in clamshell mode and attach it to my monitor. All I really want are certain apps to quit when I close the last window, and I'm not looking to change the Touch Bar at all, I essentially want it to look exactly the same as if I was not running these scripts at all, so whatever the application would normally display on the stock Touch Bar. In that case, would I use Caliguvara's method?

Yupp. CAGs are System wide, not just Touch Bar related :wink:

Another option, depending on how many apps you want to "treat" that way: make an App specific section (button left corner), and make ⌘W and the red button inside that application make run this script:

tell application "System Events"
	keystroke "w" using command down
end tell

delay 0.5

try
	tell application (get path to frontmost application as text)
		set WindowCount to get (count of windows)
	end tell
end try

if WindowCount is 0 then
	tell application (get path to frontmost application as text)
		quit
	end tell
end if

Here is how it should look like in BTT preferences:

Thank you, @Caliguvara. I tried various methods, including most suggestions on this discussion thread. The last one you suggested worked!

Some background for others: I need to close Safari when its last window closes, otherwise Safari is in a confused state with regard to the private browsing mode. Since it's only Safari, the second part of the script can be simplified to:

tell application "Safari"
	if (count windows) = 0 then quit
end tell

Let me hijack this thread as I want to achieve something similar:

For any application:
(1) Click on red x window button: Closes the window, and quits the app if it’s the last window. Ideally shows a “do you want to save/are you sure you want to close” dialogue before.

(2) Upon pressing cmd+F4 doing the same thing.

I am currently mapping cmd+F4 to the close window command using Karabiner, but it keeps the app running even after the last window was closed.

Imprint | Privacy Policy