zhuyuan-ice-gl-zy001/.tmp_axprobe/capwin.swift
2026-08-20 00:01:53 +08:00

22 lines
1 KiB
Swift
Executable file

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")