Repository navigation
Add a Terminal option to apply an action to every new window - #1945
Merged
Merged
Conversation
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]>
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]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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-actionURLs:From the next launch on, Rectangle runs that action on each ordinary window an application opens.
defaults delete com.knollsoft.Rectangle newWindowActionturns 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:activationPolicy == .regular, so apps with a Dock icon; menu bar utilities are not watched) gets anAXObserverforkAXWindowCreatedNotification, attached on a worker queue the wayTitleBarManagerattaches 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).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.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.tile-all,cascade-all,cascade-active-appand friends), the Todo moves andrestoreare 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-oneand 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.ShortcutManagerandWindowManagerpath withpreservesFocusset, 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.ExecutionSourcegains a.newWindowcase (plus the one exhaustive switch over it, which excludes cooperative resize, and the repeat-cycling condition inShortcutManager);AccessibilityElementgainsisStandardWindow. Nothing else inWindowManagerchanges. #1905 touches the sameExecutionSourceline, so whichever lands second has a one-line rebase.The two questions from the issue
Tests
27 new cases:
NewWindowActionPolicyTestscover the name parsing (URL form, shortcut name, aliases, unset, unknown, multi-window and display values) and every skip rule;NewWindowActionManagerTestsdrive 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);ShortcutManagerSessionTestspins that a.newWindowplacement never cycles displays;DefaultsExportTestschecks the new key is exported.Full
RectangleTestssuite: 680 tests, 0 failures, on macOS 27.0.1 / Xcode 27.