I make Scry, a screenshot app for the Mac. One of its tools is scroll capture: you draw a box around part of a page, and Scry scrolls that page and stitches one long image.
For a while it had a bug that looked random. You drew the box over a web page, and Scry scrolled its own Library window instead. Sometimes it scrolled the Settings window. Never the page you asked for.
The cause was one line I had added on purpose.
The line that raised everything
The capture overlay is a borderless, full-screen, transparent window. To take key and receive keystrokes, the app has to be active, so the overlay called:
NSApp.activate(ignoringOtherApps: true)
That does make the overlay key. It also orders every ordinary window of the app above every other app. With a transparent overlay covering the whole screen, nobody sees it happen. You only see the result afterwards: the Library is now the top window under your box, and that is what gets scrolled.
Fix, part one: keep your own windows back
Before activating, I lower every visible ordinary window of the app by one level, remember the old level, and put it back when the overlay closes:
let below = NSWindow.Level(rawValue: NSWindow.Level.normal.rawValue - 1)
var saved: [(NSWindow, NSWindow.Level)] = []
for w in NSApp.windows where w.isVisible && w.level == .normal && w !== overlay {
saved.append((w, w.level))
w.level = below
}
NSApp.activate(ignoringOtherApps: true)
// on dismiss:
for (w, level) in saved { w.level = level }
Window level beats activation order, so the windows stay exactly where they were on screen.
Fix, part two: don’t ask who is frontmost
The second half was quieter. At hotkey time I asked NSWorkspace.shared.frontmostApplication which app to scroll. But Scry is often already frontmost at that moment, so the answer was Scry itself.
Now the target is the app that owns the window under the box: CGWindowListCopyWindowInfo, layer 0 windows only, with the point flipped into window-list coordinates. If nothing is found, it falls back to the frontmost app that is not Scry.
One more trap there: the rect from a selection overlay is in that screen’s own coordinates. Add screen.frame.origin before flipping, or the lookup can land on the wrong window.
Test both halves together
Both halves are needed, so Scry’s self-test checks them together. To trust it, I commented out the level change and watched the test fail with “Scry’s windows jumped in front”. A test you have never seen fail is not telling you much.
What I’d tell my past self
-
NSApp.activateis not “make this window key”. It is “bring the whole app forward”. - An invisible overlay hides window-order bugs completely. Check what is under it, not what is on it.
- “Frontmost app” is a bad question to ask from inside the app that just got the hotkey.
If you build Mac tools that act on other apps’ windows, I hope this saves you an afternoon.
I write about building Mac apps alone, and send a short email when one of my apps ships or gets a big update, twice a month at most: https://appsbymo.app/?ref=devto#signup-h
Top comments (0)