SPB Git

Repository Public

window-shot.swift 1.4 KB 16 lines swift

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]   (pid: only windows of that process — the Debug build, never the installed app)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([.optionOnScreenOnly, .excludeDesktopElements], kCGNullWindowID) as? [[String: Any]] ?? []10let pid = args.count == 4 ? Int(args[3]) : nil11let wins = list.filter { ($0["kCGWindowOwnerName"] as? String) == args[1] && (pid == nil || ($0["kCGWindowOwnerPID"] as? Int) == pid) && [0, 3, 8].contains(($0["kCGWindowLayer"] as? Int) ?? -1) && ((($0["kCGWindowBounds"] as? [String: Any])?["Height"] as? Double) ?? 0) > 80 }12guard let win = wins.first, let id = win["kCGWindowNumber"] as? Int else { print("no window for \(args[1])"); exit(1) }13print("window \(id) \(win["kCGWindowBounds"] ?? "") title=\(win["kCGWindowName"] ?? "")")14let p = Process(); p.executableURL = URL(fileURLWithPath: "/usr/sbin/screencapture"); p.arguments = ["-l", String(id), "-o", "-x", args[2]]15try! p.run(); p.waitUntilExit(); print("saved \(args[2])")16
Files main
Menu

Theme