Left Click Red Window Button Triggers on Windows from Other Applications

Describe the bug
I have filed this as a bug, although it may be more of a feature request/general question.

Currently, the family of triggers involving clicking on the red/yellow/green window buttons are not tied to the window/application that is clicked, but rather to the application that is frontmost.

For example, if TextEdit is open & frontmost, but you click the red window button on a background Finder window without first activating Finder, BTT runs the action associated with TextEdit, not Finder.

I would argue that this is a little counter-intuitive, since I imagine that most actions that people use with these triggers relates to the window/application that is clicked. BTT works around most of these issues by having actions that act on the window/application under the cursor, but for the edge cases it would probably be useful to have the triggers determine which application was targeted.

Affected input device (e.g. MacBook Trackpad, Magic Mouse/Trackpad, Touch Bar, etc.):
Other Triggers - Click Red/Yellow/Green Window Button


Device information:

  • Type of Mac: Multiple Macs
  • macOS version: 12.3.1
  • BetterTouchTool version: 3.779 & Earlier

Additional information (e.g. StackTraces, related issues, screenshots, workarounds, etc.):
In particular, I use the 'Left Click Red Window Button' trigger on certain applications, such as Reminders, that quit when their last window is closed, in order to hide the last window instead of quitting the application. I do this with a little command-line tool, as AppleScript is too slow (and simply remapping command-H doesn't work properly if the application can have multiple windows).

If you think this snippet is useful, please feel free to include it as an action (such as Hide Remaining Application Window)!

import AppKit

// Get the frontmost application using the accessibility framework.
let frontmostApplication = NSWorkspace.shared.frontmostApplication!
let app = AXUIElementCreateApplication(frontmostApplication.processIdentifier)

// Get all windows for the application count.
var windows: CFTypeRef?
var err = AXUIElementCopyAttributeValue(app, kAXWindowsAttribute as NSString, &windows)
guard err == .success else { exit(EXIT_FAILURE) }

// Get the window count.
let windowCount = (windows as! [AXUIElement]).count
if windowCount < 2 { // Single window. Hide the application.
	// Hide the application.
} else { // Multiple windows. Close the frontmost window.
	// Get the focused window.
	var frontWindow: CFTypeRef?
	err = AXUIElementCopyAttributeValue(app, kAXFocusedWindowAttribute as NSString, &frontWindow)
	guard err == .success else { exit(EXIT_FAILURE) }
	// Get its close button.
	var closeButton: CFTypeRef?
	err = AXUIElementCopyAttributeValue(frontWindow as! AXUIElement, kAXCloseButtonAttribute as NSString, &closeButton)
	guard err == .success else { exit(EXIT_FAILURE) }
	// Press its close button.
	AXUIElementPerformAction(closeButton as! AXUIElement, kAXPressAction as NSString)