import Cocoa let pid = pid_t(CommandLine.arguments[1])! let out = CommandLine.arguments[2] guard let wins = CGWindowListCopyWindowInfo([.optionOnScreenOnly], kCGNullWindowID) as? [[String: Any]] else { exit(1) } for w in wins { if (w[kCGWindowOwnerPID as String] as? Int) == pid, (w[kCGWindowLayer as String] as? Int) == 0, let wid = w[kCGWindowNumber as String] as? Int, let bd = w[kCGWindowBounds as String] as? [String: Any], (bd["Width"] as! Double) > 400 { if let img = CGWindowListCreateImage(.null, .optionIncludingWindow, CGWindowID(wid), [.boundsIgnoreFraming]) { let url = URL(fileURLWithPath: out) if let dest = CGImageDestinationCreateWithURL(url as CFURL, "public.png" as CFString, 1, nil) { CGImageDestinationAddImage(dest, img, nil) CGImageDestinationFinalize(dest) print("saved \(out) size=\(img.width)x\(img.height)") } } else { print("capture failed") } exit(0) } } print("no window found")