Repository Public
1// Captures the frontmost window of a named app to a PNG (evidence for acceptance tests).2// Usage: swift Scripts/window-shot.swift <AppName> <output.png>3import CoreGraphics4import Foundation5let args = CommandLine.arguments6guard args.count == 3 else { print("usage: window-shot <AppName> <out.png>"); exit(2) }7let list = CGWindowListCopyWindowInfo([.optionOnScreenOnly, .excludeDesktopElements], kCGNullWindowID) as? [[String: Any]] ?? []8let wins = list.filter { ($0["kCGWindowOwnerName"] as? String) == args[1] && ($0["kCGWindowLayer"] as? Int) == 0 }9guard let win = wins.first, let id = win["kCGWindowNumber"] as? Int else { print("no window for \(args[1])"); exit(1) }10print("window \(id) \(win["kCGWindowBounds"] ?? "") title=\(win["kCGWindowName"] ?? "")")11let p = Process(); p.executableURL = URL(fileURLWithPath: "/usr/sbin/screencapture"); p.arguments = ["-l", String(id), "-o", "-x", args[2]]12try! p.run(); p.waitUntilExit(); print("saved \(args[2])")13