Captures programmatic screenshots on macOS by finding window IDs via Swift/CoreGraphics, controlling app windows with AppleScript, and using screencapture. Useful for automating screenshots, documenting UIs, or building visual workflows.
Ships in daymade-macos. Installing the plugin adds it to Claude Code.
What the skill costs in Claude's context, stage by stage
Name and description: Claude knows it exists.
SKILL.md joins the conversation and stays.
Read when SKILL.md points to them; scripts run unread.
Programmatic screenshot capture on macOS: find windows, control views, capture images.
For an existing view, enumerate and capture the exact window without activating it. Skip Step 2 when no view change is needed. A window image proves visible content in that capture, not keyboard focus, input delivery or a successful business action.
Before opening/raising windows, scrolling, selecting, changing zoom or sending keys, the executing agent checks user activity and other agents' ownership. Use a concrete task handoff or an existing explicit idle-desktop authorization; under the latter, coordinate exclusive access and proceed without asking again. Idle input alone is not ownership. Busy/unknown occupancy or unexpected input stops foreground actions; continue independent reads and do not restore old focus after human takeover.
Inspect the chosen tool's activation semantics. An app-targeted call or a shell job running in the background does not prove it preserves focus. A failed capture does not authorize raising the window, permission changes or switching tools around a host refusal/GUI lock. Only request a handoff when existing authorization is absent.
Use this route when the user requests a whole-screen view, or when permitted window observation cannot identify the reported obstruction. First establish that the host permits this capture and its scope. An app-specific control refusal does not prove screen capture is unavailable; it also does not authorize using a different tool to read content the host protects. Stop if the refusal covers the proposed capture or a GUI lock prevents it.
Capture without activation, scrolling or input:
capture_dir="$(mktemp -d)"
screencapture -x "$capture_dir/desktop.png"
Inspect the resulting image with the host's permitted image viewer. On a multi-display desktop, inspect each produced display image before claiming to have checked the whole desktop. File creation alone does not prove the requested dialog was captured. Once identified, prefer the exact window for further observations and keep the user's reported interaction failure in scope.
Follow the macOS permission owner's macos-permissions entry for classification
and recovery.
# Find Excel window ID
swift scripts/get_window_id.swift Excel
# Capture that window (replace 12345 with actual WID)
screencapture -x -l 12345 output.png
Use the bundled Swift/CoreGraphics route when the host permits window enumeration. Prefer the host's native window inventory when it supplies the required IDs.
swift -e '
import CoreGraphics
let keyword = "Excel"
let list = CGWindowListCopyWindowInfo(.optionOnScreenOnly, kCGNullWindowID) as? [[String: Any]] ?? []
for w in list {
let owner = w[kCGWindowOwnerName as String] as? String ?? ""
let name = w[kCGWindowName as String] as? String ?? ""
let wid = w[kCGWindowNumber as String] as? Int ?? 0
if owner.localizedCaseInsensitiveContains(keyword) || name.localizedCaseInsensitiveContains(keyword) {
print("WID=\(wid) | App=\(owner) | Title=\(name)")
}
}
'
swift scripts/get_window_id.swift Excel
swift scripts/get_window_id.swift Chrome
swift scripts/get_window_id.swift # List all windows
Output format: WID=12345 | App=Microsoft Excel | Title=workbook.xlsx
Parse the WID number for use with screencapture -l.
Use these commands only inside the authorized foreground interval above. Bind the
exact app/window/document before using any active window or active workbook
example; never infer the target from whichever window happens to be active.
# Activate (bring to front)
osascript -e 'tell application "Microsoft Excel" to activate'
# Set zoom level (percentage)
osascript -e 'tell application "Microsoft Excel"
set zoom of active window to 120
end tell'
# Scroll to specific row
osascript -e 'tell application "Microsoft Excel"
set scroll row of active window to 45
end tell'
# Scroll to specific column
osascript -e 'tell application "Microsoft Excel"
set scroll column of active window to 3
end tell'
# Select a cell range
osascript -e 'tell application "Microsoft Excel"
select range "A1" of active sheet
end tell'
# Select a specific sheet
osascript -e 'tell application "Microsoft Excel"
activate object sheet "DCF" of active workbook
end tell'
# Open a file
osascript -e 'tell application "Microsoft Excel"
open POSIX file "/path/to/file.xlsx"
end tell'
Avoid
System Eventsand unapprovedosascriptfor read-only checks. Queryingtell application "System Events"just to check whether an app is running or to enumerate windows/tabs triggers a separate macOS Automation TCC consent dialog on hosts without prior approval, can hang or fail with-609(Connection is invalid), and leaves a foreground system modal that obscures target windows. Directtell application "<App>"can trigger the same blocking Automation consent modal (and hangosascriptwithouttimeout) when run from an unapproved parent host such as a remote-access terminal multiplexer (e.g.,uuyc-mux). Usepgrep, SwiftCGWindowListCopyWindowInfo, andAXUIElementfor read-only inspection; use the target app's AppleScript dictionary orSystem Eventsonly on hosts where Automation permission is already verified, and always wraposascriptwithtimeout.
# Activate any app
osascript -e 'tell application "Google Chrome" to activate'
# Bring specific window to front (by index; requires Automation permission for System Events)
osascript -e 'tell application "System Events"
tell process "Google Chrome"
perform action "AXRaise" of window 1
end tell
end tell'
Always add sleep 1 after AppleScript commands before capturing, to allow UI rendering to complete.
IMPORTANT: osascript hangs indefinitely if the target application is not running or not responding. Always wrap with timeout:
timeout 5 osascript -e 'tell application "Microsoft Excel" to activate'
# Capture specific window by ID
screencapture -l <WID> output.png
# Silent capture (no camera shutter sound)
screencapture -x -l <WID> output.png
# Capture as JPEG
screencapture -l <WID> -t jpg output.jpg
# Capture with delay (seconds)
screencapture -l <WID> -T 2 output.png
# Capture a screen region (interactive)
screencapture -R x,y,width,height output.png
On Retina Macs, screencapture outputs 2x resolution by default (e.g., a 2032x1238 window produces a 4064x2476 PNG). This is normal. To get 1x resolution, resize after capture:
sips --resampleWidth 2032 output.png --out output_1x.png
# Check file was created and has content
ls -la output.png
file output.png # Should show "PNG image data, ..."
Foreground example: capture multiple sections of an Excel workbook. Establish the authorization and exclusive ownership above first. If a user takes over, stop the remaining sequence rather than replaying it or restoring your previous focus.
# 1. Open file and activate Excel
osascript -e 'tell application "Microsoft Excel"
open POSIX file "/path/to/model.xlsx"
activate
end tell'
sleep 2
# 2. Set up view
osascript -e 'tell application "Microsoft Excel"
set zoom of active window to 130
activate object sheet "Summary" of active workbook
end tell'
sleep 1
# 3. Get window ID
# IMPORTANT: Always re-fetch before capturing. CGWindowID is invalidated
# when an app restarts or a window is closed and reopened.
WID=$(swift -e '
import CoreGraphics
let list = CGWindowListCopyWindowInfo(.optionOnScreenOnly, kCGNullWindowID) as? [[String: Any]] ?? []
for w in list {
let owner = w[kCGWindowOwnerName as String] as? String ?? ""
let wid = w[kCGWindowNumber as String] as? Int ?? 0
if owner == "Microsoft Excel" { print(wid); break }
}
')
echo "Window ID: $WID"
# 4. Capture Section A (top of sheet)
osascript -e 'tell application "Microsoft Excel"
set scroll row of active window to 1
end tell'
sleep 1
screencapture -x -l $WID section_a.png
# 5. Capture Section B (further down)
osascript -e 'tell application "Microsoft Excel"
set scroll row of active window to 45
end tell'
sleep 1
screencapture -x -l $WID section_b.png
# 6. Switch sheet and capture
osascript -e 'tell application "Microsoft Excel"
activate object sheet "DCF" of active workbook
set scroll row of active window to 1
end tell'
sleep 1
screencapture -x -l $WID dcf_overview.png
These methods were tested and confirmed to fail on macOS:
| Method | Error | Why It Fails |
|---|---|---|
System Events → id of window | Error -1728 | System Events cannot access window IDs in the format screencapture needs |
Python import Quartz (PyObjC) | ModuleNotFoundError | PyObjC not installed in system Python; don't attempt to install it — use Swift instead |
osascript window id | Wrong format | Returns AppleScript window index, not CGWindowID needed by screencapture -l |
System Events or unapproved tell application "<App>" for read-only process/window checks | TCC prompt / hang / Error -609 | Both System Events and direct tell application "<App>" trigger blocking Automation consent dialogs on unapproved or remote-hosted terminals (e.g., remote-access mux shells), hang without timeout, and leave a modal covering the target window; use pgrep, Swift CGWindowListCopyWindowInfo, and AXUIElement for read-only inspection, and wrap required osascript calls with timeout |
If get_window_id.swift cannot enumerate windows or a required field is unavailable,
read the actual result before attributing the failure to Screen Recording. Request
the helper's diagnostic guidance without enumerating windows:
swift scripts/get_window_id.swift --permission-hint screen
swift scripts/get_window_id.swift --permission-hint microphone
Interpret that output with the helper diagnostic procedure.
The hint prints guidance and execution context; it does not check grant state or
identify which process should receive a grant. Keep general TCC attribution,
permission changes and request recovery with macos-permissions.
| Application | Window ID | AppleScript Control | Notes |
|---|---|---|---|
| Microsoft Excel | Swift | Full (zoom, scroll, select, activate sheet) | Best supported |
| Google Chrome | Swift | Basic (activate, window management) | No scroll/zoom via AppleScript |
| Other macOS apps | Swift or permitted host inventory | App-dependent | Capture requires host permission and a capturable surface |
AppleScript control depth varies by application. Excel has the richest AppleScript dictionary. For apps with limited AppleScript, keyboard simulation via System Events is a foreground alternative only after the entry gate passes, not an automatic recovery from a failed background action.
What SKILL.md sets; muted values are Claude Code's defaults
Three short reads, in order
6 skills in this plugin
/developing-ios-appsBuilds and ships iOS/macOS apps with XcodeGen, SwiftUI and SPM: Xcode build fixes, device deployment, Apple Developer signing/notarization, CI/CD pipelines. Use for XcodeGen project.yml setup, "Library not loaded @rpath" crashes, code-signing errors (Error -25294, adhoc fallback), or Electron notarization failures. Not for TCC permission dialogs (use macos-permissions) or disk cleanup (use macos-cleaner)./macos-cleanerDiagnoses and safely reclaims macOS disk space: caches, logs, app remnants, large/duplicate files, Docker/OrbStack, Chromium code-sign clones, developer caches. Use when a Mac is low on storage, shows “Caching needs more space”, when consolidating old task evidence, or before deleting anything to avoid data loss. Not for Windows/Linux or permission issues (use macos-permissions)./macos-load-doctorDiagnoses macOS system-level slowness, heat and fleet-wide timeouts: high load average, leaked per-session child processes (MCP servers, helpers), fork storms, busy loops. Use when the Mac feels slow or hot, fans spin up, or everything times out at once (电脑发烫/卡顿/负载高/全部超时). Not for single-app bugs, network slowness (use tunnel-doctor), launchd watchdog design (use macos-watchdog), or disk-full (use macos-cleaner)./macos-permissionsDiagnoses TCC denials, Local Network access and stuck Keychain dialogs: Full Disk Access, Screen Recording, Microphone, Camera, Accessibility, Automation. Use for repeated prompts, background-only LaunchAgent/uv failures, broken grants or FDA automation (权限弹窗 / 授权了没用 / 完全磁盘访问 / 本地网络权限). Checks requester/grants first. Not launchd design (macos-watchdog) or app permission UX (developing-ios-apps)./macos-watchdogDesigns and audits macOS launchd watchdogs: LaunchAgent/LaunchDaemon setup, plist edits, scheduled self-healing, repeated notifications, apps reopening after quit, crash/repair loops, Script Editor alerts, stop/disable/restart, cooldown/backoff, notification throttling, alert decisions, schedule readback, WatchPaths/FSEvents event triggers. Supports periodic observers. launchd 守护进程、常驻任务、开机自启、后台监控、定时自愈、通知来源排查、事件驱动监控。/daymade-macos:capture-screenfrom daymade-macos
npx claudepluginhub daymade/claude-code-skills --plugin daymade-macosOther plugins' skills for the same job