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!

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

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:

3 Likes

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.

I tried this, but visual studio code, and calendar, note(mac os native app) did not work

and some other problem, If I open TextEdit, and write something, command + w,then prompt a close window"Do you want to keep", If I click "delete", the textEdit still alive

@Caliguvara Is there any way to fix this?

First of all, thanks everybody for this amazing input!

But I ran into a couple problems (with Monterey 12.6).
With a couple Apps in various situations I got not one window as a result, but two.
I was only seeing one though :wink:
In a couple Apps like Maps, Notes etc. it did not work at all.

I came up with a solution I want to share for everybody who wants to achieve this behavior.
For the time being it works incredibly well!

Cheers

tell application "System Events"
    set activeProcesses to first process where it is frontmost
	
    repeat with theProcess in activeProcesses
        if not background only of theProcess then
		
            tell theProcess
                set processName to name
                set theWindows to windows
            end tell
			
            set windowsCount to count of theWindows
        end if
    end repeat
end tell

if windowsCount is less than or equal to 1 then
	tell application (get path to frontmost application as text)
		quit
	end tell
else
	tell application "System Events"
		keystroke "w" using command down
	end tell
end if

Only time it does not work is, when the App is not the frontmost and I click the red button.

Is it possible to get the process of the app, where I clicked the red button, instead of going for the frontmost window?

When use Chrome or vscode, I want to quit only after all tabs closed,

hi, how can I exclude some apps in this apple script?

That should be done easy by using a conditional activation group in BTT.
The (+)-Button on the bottom left.

Then you edit the conditions and add a filter with 'App name' is not 'Google Chrome'

1 Like

Hi, when I'm using TextEdit.app, a native in macOS, if write sth in TextEdit,press command + w , there will be a prompt for the file name, then the prompt will lag and block due to this script, how to resolve this? @jnb

Hi @hu_hao

I don't know your script anymore. Because i changed it quite a lot till know.
But I don't have this problem. So just update with the latest preset I shared here:

and you should be fine.

I also extended the script to work better with different kind of Settings-Windows of different apps, which did not close as long as BTT was running with this preset.

But keep in mind, that you kind of backup the list of apps.

Hope that helps.

Cheers!

Ps. It even works for apps, that are not front-most :wink:

1 Like

Using this method led to the following bug (Reported here). What is your advice?

Describe the bug
Red Window Button does not properly after defining Automations & Named & Other Triggers:

  1. Red Window Button does not work at all after quitting BetterTouchTool (e.g., BTT crashes and closed automatically)
  2. Clicking on Red Window Button in an app, randomly closes other apps' windows!
  3. Clicking on Red Window Button in web browsers' private/incognito windows closes tabs instead windows!
  4. Clicking on Red Window Button in Microsoft Edge shows a menu instead of closing the window!

Screenshots
Clicking on Red Window Button in the Activity Monitor app closes Firefox! (Note: Activity Monitor is the active window):

Clicking on Red Window Button in Microsoft Edge shows a menu instead of closing the window:
image

Device information:

  • Type of Mac: MacBook Air M1
  • macOS version: 12.6.3 (21G419)
  • BetterTouchTool version: 4.023 (2240)

Additional information:
The following Apple Script is defined to run when clicking on Red Window Button based on this topic:

tell application "System Events"
    set activeProcesses to first process where it is frontmost
	
    repeat with theProcess in activeProcesses
        if not background only of theProcess then
		
            tell theProcess
                set processName to name
                set theWindows to windows
            end tell
			
            set windowsCount to count of theWindows
        end if
    end repeat
end tell

if windowsCount is less than or equal to 1 then
	tell application (get path to frontmost application as text)
		quit
	end tell
else
	tell application "System Events"
		keystroke "w" using command down
	end tell
end if

1 Like

Hi Meave, as far as I can tell from your screenshots you did not install the script as I described in the first place.

The script does not work when you just put the Applescript on the Leftclick-Red-Button-Action.

My LeftclickBA looks like this:

So first thing the script needs is, that it will be activated. This makes sure that this window is the frontmost window. Otherwise it won't work and closes the other apps, that are actually active :wink: