capture-screen
Programmatic screenshot capture on macOS. Find window IDs with Swift CGWindowListCopyWindowInfo, control application windows via AppleScript (zoom, scroll, select), and capture with screencapture. Use when automating screenshots, capturing application windows for documentation, or building multi-shot visual workflows.
1How It Works & Mechanism
A three-step workflow built entirely on native macOS tools: Swift + CoreGraphics enumerates windows to get the WID → AppleScript/osascript controls the view → screencapture -l <WID> performs precise screen capture.
1. Find Window → Swift CGWindowListCopyWindowInfo → get numeric Window ID 2. Control View → AppleScript (osascript) → zoom, scroll, select 3. Capture → screencapture -l <WID> → PNG/JPEG output
Window discovery uses the Swift script get_window_id.swift invoking CoreGraphics CGWindowListCopyWindowInfo(.optionOnScreenOnly), filtering by app name/title keyword to output WID=… | App=… | Title=….
CGWindowListCopyWindowInfo( .optionOnScreenOnly, kCGNullWindowID ) as? [[String: Any]] else {
View control layer: Excel has full AppleScript dictionary support (activate, zoom, scroll row/column, select range, switch sheets, open files); arbitrary apps rely on basic activate / System Events AXRaise; restricted apps fall back to keyboard simulation.
osascript -e 'tell application "Microsoft Excel" set scroll row of active window to 45 end tell'
Capture layer: screencapture -l <WID> precisely captures the specified window (-x for mute, -t jpg, -T delay, -R region), and Retina 2x outputs are scaled back to 1x using sips.
# Silent capture (no camera shutter sound) screencapture -x -l <WID> output.png
Triple-check permission troubleshooting template: Screen Recording permission is mandatory; helper binaries (swift/python/terminal) and packaged .app bundles have different TCC authorization identities; the script provides --permission-hint screen/microphone to print troubleshooting guidance.
`swift scripts/get_window_id.swift` reads on-screen windows via CoreGraphics, so it needs Screen Recording permission on macOS.
Documented failed paths (DO NOT USE): System Events `id of window` (-1728), Python PyObjC Quartz (ModuleNotFoundError), and osascript `window id` (incorrect format)—preventing execution models from repeating mistakes.
| `System Events` → `id of window` | Error -1728 | System Events cannot access window IDs in the format screencapture needs |
2Core Capabilities
3External Dependencies
| Kind | Dependency / Endpoint |
|---|---|
| cli | swift(运行 get_window_id.swift 或 -e 内联执行 CoreGraphics 枚举) |
| cli | screencapture(macOS 系统自带,窗口/区域截图) |
| cli | osascript / System Events(AppleScript 控制 app 窗口) |
| cli | sips / ls / file / timeout(可选缩放与校验、超时保护) |
4Recommended Governance & Evidence
- scripts/: Execute local scripts or commands: swift scripts/get_window_id.swift (or inline window enumeration with swift -e)
- 本地工作目录: File read/write and artifact output: screenshot output files (output.png/output.jpg, etc., with paths specified by caller)
5Second-Pass Verification
- [ok] Swift 脚本只做窗口枚举(无隐藏行为) — get_window_id.swift 逻辑 = 参数解析 + CGWindowListCopyWindowInfo(.optionOnScreenOnly) + 过滤打印 + 权限提示文本;无文件写/网络/进程执行调用。
- [ok] 三步工作流与 description 声明一致 — Find Window/Control View/Capture 三步概览代码块、Step1-3 正文、Quick Start 命令逐字对应;Excel 控制命令含 zoom/scroll/select/sheet。
- [ok] screencapture -l 兼容 CGWindowID 的说法有失败表佐证 — Failed Approaches 表记录 System Events id of window 报 -1728 等三个失败路径,反向支持『Swift 枚举 + screencapture -l』为主路径。
- [ok] 无网络/无凭证(安全结论基础) — 全目录 token 扫描零网络/凭证命中;依赖全部是 macOS 系统自带 CLI。
- [ok] TCC/Screen Recording 依赖表述准确 — SKILL.md Permission Troubleshooting 与脚本 --permission-hint 均明示 Screen Recording 为前置条件,并处理 helper binary 身份问题。
- [ok] 元数据(commit/license/stars) — 本地 HEAD == pin d5c4678cb5d4fd6acc9c922690df035dbd33d247;GitHub API:MIT、stars 1385(2026-09-09 实采)、pushed_at 2026-09-09T12:33:29Z。
6Verdict & Scenarios
fb587255b81bdc81…