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...
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
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.
@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:
Go to Named & Other Triggers
Create a New Trigger that activates if a specific Conditional Activation Group is activated.
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
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.
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
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
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
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
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
Clicking on Red Window Button in Microsoft Edge shows a menu instead of closing the window:
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
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