Repository Public
1// Author: Simon-Pierre Boucher — contact@spboucher.ai2// File: Scripts/window-shot.swift3// Description: Captures the frontmost window of a named app to a PNG (evidence for visual checks).4// Usage: swift Scripts/window-shot.swift <AppName> <output.png> [pid] — with a pid, only the windows of that process (never the installed app's)5import CoreGraphics6import Foundation7let args = CommandLine.arguments8guard args.count == 3 || args.count == 4 else { print("usage: window-shot <AppName> <out.png>"); exit(2) }9let list = CGWindowListCopyWindowInfo([.optionAll, .excludeDesktopElements], kCGNullWindowID) as? [[String: Any]] ?? []10let pid = args.count == 4 ? Int(args[3]) : nil11let anyLayer = ProcessInfo.processInfo.environment["SHOT_ANY_LAYER"] != nil12let wins = list.filter { ($0["kCGWindowOwnerName"] as? String) == args[1] && (anyLayer || ($0["kCGWindowLayer"] as? Int) == 0) && (pid == nil || ($0["kCGWindowOwnerPID"] as? Int) == pid) }13func area(_ w: [String: Any]) -> Double { let b = w["kCGWindowBounds"] as? [String: Any] ?? [:]; return ((b["Width"] as? Double) ?? 0) * ((b["Height"] as? Double) ?? 0) }14guard let win = wins.filter({ ($0["kCGWindowIsOnscreen"] as? Bool) == true }).max(by: { area($0) < area($1) }) ?? wins.max(by: { area($0) < area($1) }), let id = win["kCGWindowNumber"] as? Int else { print("no window for \(args[1])"); exit(1) }15print("window \(id) \(win["kCGWindowBounds"] ?? "") title=\(win["kCGWindowName"] ?? "")")16let p = Process(); p.executableURL = URL(fileURLWithPath: "/usr/sbin/screencapture"); p.arguments = ["-l", String(id), "-o", "-x", args[2]]17try! p.run(); p.waitUntilExit(); print("saved \(args[2])")18