Skip to content

fix(macos): detect the frontmost window via Quartz instead of NSWorkspace - #20

Merged
ayushh0110 merged 2 commits into
ayushh0110:mainfrom
Venopacman:fix/macos-frontmost-window
Oct 14, 2026
Merged

ayushh0110 merged 2 commits into
ayushh0110:mainfrom
Venopacman:fix/macos-frontmost-window

Conversation

@Venopacman

@Venopacman Venopacman commented Oct 6, 2026 •

Copy link
Copy Markdown
Contributor

Problem

On macOS every capture gets the same app: the one in front when ScreenMind started (usually Terminal). The window title is also wrong. It is always the app name, never the real title.

NSWorkspace.frontmostApplication() updates through notifications on the main NSRunLoop. ScreenMind's capture loop has no run loop, so the value never changes after the first read.

Fix

  • Read the live on-screen window list with CGWindowListCopyWindowInfo. It is ordered front to back, so the first layer-0 window is the active one.
  • Take the app name, PID and window title from that window. The title (kCGWindowName) needs Screen Recording permission. Without it, we fall back to the app name.
  • Skip tiny windows (under 50x50 px), which are usually overlay helpers.
  • Same bug in ScreenCapture._window_center_macos, which used the stale PID to choose the monitor when capture_active_monitor is on. It now asks the platform layer: new PlatformAdapter.get_active_window_bounds() returns None by default, so Windows and Linux don't change. MacOSAdapter implements it from the same _front_window(), so monitor selection and app detection always agree on which window is in front.

A side benefit: accessibility text extraction now gets the right PID too.

Testing

  • New tests/test_macos_frontmost.py fakes the Quartz module, so it runs on the Windows CI. It covers front-to-back order, skipping the menu bar and tiny windows, title fallback, changes between calls, window bounds, and ScreenCapture using the adapter.
  • Manual test on macOS 26 (Apple M5): switched between Terminal, Claude, Chrome and Telegram. Before the fix every capture was Terminal. After it, each capture had the right app and the real window title.

🤖 Generated with Claude Code

…pace

NSWorkspace.frontmostApplication() is updated through notifications on the
main NSRunLoop. ScreenMind's capture loop has no run loop, so the value is
read once and never changes: every capture was labeled with whatever app
was in front at launch (usually Terminal).

Read the live on-screen window list from Quartz instead. It is ordered
front to back, so the first layer-0 window is the active one. This also
gives the real window title (kCGWindowName, needs Screen Recording
permission) instead of the app name, and the correct PID for the
accessibility text extraction.

Same fix for ScreenCapture._window_center_macos, which used the stale PID
to choose the monitor when capture_active_monitor is on.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Move the frontmost-window bounds lookup out of ScreenCapture and into
the platform layer, following the platform_support pattern:

- PlatformAdapter.get_active_window_bounds() returns None by default
  (Windows/Linux unchanged).
- MacOSAdapter implements it from the same _front_window() used for the
  app name and title, so monitor selection and app detection agree on
  which window is in front (same layer-0 and min-size rules).
- ScreenCapture._window_center_macos() now just asks the adapter.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@ayushh0110

Copy link
Copy Markdown
Owner

my comments:

  1. _appkit_available is now dead code. After this PR it's set in __init__ but never read — all three detection methods use _front_window() / Quartz now. _ax_available is still used by extract_a11y_text(), so that one stays. Could clean up the AppKit init + flag.

  2. Quartz goes from optional to required. The old code had NSWorkspace + osascript as fallbacks when Quartz wasn't available. After this PR, if import Quartz fails, all three methods (get_active_window_title, get_active_app_name, get_foreground_window_handle) return None. pyobjc-framework-Quartz isn't declared in pyproject.toml (pre-existing gap), but now that it's the sole detection path on macOS, might be worth adding it as a platform-specific dependency.

  3. _front_window() is called 3× per capture cycle — each public method calls it independently, so that's 3 separate Quartz snapshots. Not a regression (old code had 3× NSWorkspace calls), but since you consolidated into one function, caching the result briefly would be a nice-to-have.

  4. Minor cleanup: import subprocess (line 8) is now unused since the osascript fallback was removed. Also the module docstring still says "Uses AppKit/NSWorkspace for window detection" — could update to reflect Quartz.

  5. Minor: The 50×50 minimum size filter is reasonable for skipping overlay helpers, but worth a brief comment explaining the threshold.

@ayushh0110
ayushh0110 merged commit 74ff138 into ayushh0110:main Oct 14, 2026
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.

2 participants