Skip to content

Add a Terminal option to apply an action to every new window - #1945

Merged
rxhanson merged 2 commits into
rxhanson:mainfrom
AkiyamaKunka:feat/new-window-action
Nov 3, 2026
Merged

rxhanson merged 2 commits into
rxhanson:mainfrom
AkiyamaKunka:feat/new-window-action

Conversation

@AkiyamaKunka

Copy link
Copy Markdown
Contributor

Closes #1894

Picks up the feature request from @devxbasit, where you wrote that you "see no reason to turn down a good pull request that adds this in." I claimed it on the issue on October 15 and went ahead with the design posted there, settling the two open questions below.

What it does

A hidden preference names one action, using the same names as the rectangle://execute-action URLs:

defaults write com.knollsoft.Rectangle newWindowAction -string maximize

From the next launch on, Rectangle runs that action on each ordinary window an application opens. defaults delete com.knollsoft.Rectangle newWindowAction turns it off. Documented in TerminalCommands.md.

How it works

NewWindowActionManager (new file) does nothing unless the preference names a valid action, so there is no new Accessibility traffic for anyone who has not opted in. When it is set:

  • Every regular application (activationPolicy == .regular, so apps with a Dock icon; menu bar utilities are not watched) gets an AXObserver for kAXWindowCreatedNotification, attached on a worker queue the way TitleBarManager attaches its observer, with retries over about 12 s because a launching application's accessibility server is not up immediately. If attaching still fails, that is logged and tried again the next time the app is activated, and the windows it has by then are left alone as the user's; an app that is still launching when Rectangle starts is left to its launch notification. An app the user opens is usually activated before it finishes launching, so an observation that activation started is upgraded when the launch notification arrives; the windows the app restored are placed as soon as the observer is attached (or at once, if it already is).
  • Applications that launch later are picked up through NSWorkspace, and the windows they have when their launch notification arrives (the ones they restored or opened while starting) count as new. Windows that exist when Rectangle starts are left alone.
  • The action runs 150 ms after the window appears so the application can finish its own initial placement, and only if the window is still there: a window that went away is skipped rather than beeped at. The checks are Accessibility reads on the main thread against an app that may be busy, so they run with a 0.1 s cap (the one read the cap does not reach is the full-screen check's subrole read on the zoom button, which goes through a fresh element) and a window that does not answer is left alone, with a line in View Logging; the placement itself runs with the default timeout, like a shortcut.
  • Only standard windows qualify (AXStandardWindow). Dialogs, sheets, palettes, minimized and full-screen windows, apps on Rectangle's ignore list and the Todo app while Todo mode is on are skipped, as is a window that appears while the mouse button is held, such as a tab torn off into its own window. Each window id is handled once.
  • Only single-window actions are accepted. The names that arrange several windows (tile-all, cascade-all, cascade-active-app and friends), the Todo moves and restore are rejected, since none of them places the window that just opened and some would raise or rearrange other apps' windows. The display moves (next-display, previous-display, display-one and so on) are rejected too: with one display, or a window that already opens on the target display, they would answer every new window with a beep. If moving new windows to a chosen display is wanted, that can come as a follow-up with its own no-op check.
  • Placement goes through the normal ShortcutManager and WindowManager path with preservesFocus set, so gaps, Stage Manager and display rules apply unchanged and a background app is not brought forward. A new window's first placement is never treated as a repeated command, so the "cycle through displays" option cannot send a window that opened at full size to another display.

ExecutionSource gains a .newWindow case (plus the one exhaustive switch over it, which excludes cooperative resize, and the repeat-cycling condition in ShortcutManager); AccessibilityElement gains isStandardWindow. Nothing else in WindowManager changes. #1905 touches the same ExecutionSource line, so whichever lands second has a one-line rebase.

The two questions from the issue

  1. Do windows an app restores at launch count as new? Yes. That is when most windows appear, and it keeps the rule simple: windows present before Rectangle started observing are left alone, everything that appears afterwards is new.
  2. Where does the control go? Terminal command only for now, in line with your note on Move thirds and fourths to the adjacent display on repeat #1937 about putting new behavior behind a Defaults flag first. A picker under Behavior can follow if you would like one; the manager already reloads the preference on config import.

Tests

27 new cases: NewWindowActionPolicyTests cover the name parsing (URL form, shortcut name, aliases, unset, unknown, multi-window and display values) and every skip rule; NewWindowActionManagerTests drive the manager with an injected observer and fake windows (nothing observed without a value, regular apps only, startup windows left alone, the action's parameters, one placement per window, skipped window kinds, ignore list, launch-time restores, terminated apps, config import, a failed observer re-attached on activation, activating an app already observed, apps still launching at startup, an app activated before it finished launching, a launch notification after the observer is ready, a stale failure callback, a window torn off during a drag); ShortcutManagerSessionTests pins that a .newWindow placement never cycles displays; DefaultsExportTests checks the new key is exported.

Full RectangleTests suite: 680 tests, 0 failures, on macOS 27.0.1 / Xcode 27.

AkiyamaKunka and others added 2 commits October 29, 2026 01:08
Setting the hidden newWindowAction default to an action name, using the
same names as the rectangle://execute-action URLs, makes Rectangle run
that action on each ordinary window an application opens (rxhanson#1894).
Nothing is observed while the default is unset.

NewWindowActionManager attaches a window-creation observer to every
regular application, picks up applications that launch later through
NSWorkspace, treats the windows a launching application restores as new
and leaves the windows that already exist at startup alone. Dialogs,
sheets, minimized and full-screen windows, apps on the ignore list and
windows that have gone away are skipped, and each window is handled once.
Placement goes through the usual ShortcutManager and WindowManager path
with a new .newWindow execution source.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
@rxhanson
rxhanson merged commit ec9e29d into rxhanson:main Nov 3, 2026
1 check passed
@rxhanson

rxhanson commented Nov 3, 2026

Copy link
Copy Markdown
Owner

Nice! At first cut, I like having this as a Terminal command since it is easier to effectively communicate that it is not automatically applied to some windows. I'm open to having this in the UI later as well.

AkiyamaKunka added a commit to AkiyamaKunka/Rectangle that referenced this pull request Nov 3, 2026
Keeps this branch's two test classes appended after the Todo reflow
tests that rxhanson#1947 added at the end of RectangleTests.swift.

Also restores the two closing braces at the end of
NewWindowActionManagerTests that the merge of rxhanson#1945 into main dropped;
without them the RectangleTests target does not compile on main.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
rxhanson pushed a commit that referenced this pull request Nov 3, 2026
Merging main into the new-window-action branch for #1945 dropped the
closing braces of testAStaleFailureDoesNotDropAReplacedObservation and
NewWindowActionManagerTests, so EdgeActionsAcrossDisplaysTests from #1937
ended up nested inside the test method and the RectangleTests target no
longer compiles. The app target still builds, which is why CI passes.

Puts the two braces back; the test block now matches #1945's original
commit exactly.

Co-authored-by: Claude Opus 5.5 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Feature request]: Let users set a default action for newly opened windows

2 participants