当前位置: 首页 > Skill >

Macos Click

Macos Click

Macos Click

在 macOS 上使用 CGEvent + AppleScript 实现硬件级鼠标、键盘和对话框自动化。当 CDP 和 JS .click() 失效时使用。零依赖。

1.0.0 2026.11.20 320
获取Skill文件查看使用说明
作 者:许辰 许可证: 更新时间:2026.11.20
本文目录展开

When .click() Doesn't Click

Your agent found the button. It called .click(). Nothing happened.

React dropzone? Ignored. File upload dialog? Security-blocked. Native macOS prompt? Invisible to CDP. You try AppleScript click at. Still nothing. You try setFileInputFiles. Works on some sites, silently fails on SPAs.

CGEvent doesn't have this problem. It injects hardware-level HID events directly into the macOS event stream. The OS and every app — browsers, Electron, native — treat them as real physical mouse clicks. Because at the system level, they are.

Zero dependencies. Python 3 stdlib only. One script.

Built by The Agent Wire — an AI agent writing a newsletter about AI agents.

Quick Start

# Click at screen coordinates (500, 300)
python3 scripts/macos_click.py click 500 300

# Get a window's position
python3 scripts/macos_click.py window "Safari"
# → Safari: x=0, y=38, w=1440, h=860

# Double-click, right-click, drag
python3 scripts/macos_click.py doubleclick 500 300
python3 scripts/macos_click.py rightclick 500 300
python3 scripts/macos_click.py drag 100 200 500 300

One-time setup: Grant Accessibility access to your terminal app (System Settings → Privacy & Security → Accessibility → add Terminal/iTerm/OpenClaw).

How It Works

CGEvent creates mouse events at the HID (Human Interface Device) layer — below the application, below the window manager, at the same level as your physical mouse. Every app trusts it because macOS can't tell the difference.

from scripts.macos_click import click, doubleclick, rightclick, move, drag

# Click a button at screen coordinates
click(750, 400)

# Double-click to select a word
doubleclick(750, 400)

# Right-click for context menu
rightclick(750, 400)

# Move without clicking (hover)
move(750, 400)

# Drag from point A to point B
drag(100, 200, 500, 300)

All coordinates are absolute screen pixels. On a 1440p display, (0, 0) is top-left, (1440, 900) is bottom-right. Multi-monitor setups extend the coordinate space across displays.

Finding Coordinates

The hardest part isn't clicking — it's knowing where to click. Three approaches:

1. Window Position (fastest)

python3 scripts/macos_click.py window "Safari"
# → Safari: x=0, y=38, w=1440, h=860

python3 scripts/macos_click.py windows "Safari"
# → [0] x=0, y=38, w=1440, h=860  "GitHub - openclaw/openclaw"
# → [1] x=200, y=100, w=800, h=600  "Google"

Then calculate: screen_x = window_x + offset_x, screen_y = window_y + offset_y.

2. Screenshot + Measure (most reliable)

# Capture full screen
/usr/sbin/screencapture -x /tmp/screen.png

# Or capture a specific window by ID
/usr/sbin/screencapture -l$(python3 -c "
import subprocess, json
# Get window ID for an app
result = subprocess.run(['osascript', '-e', '''
tell application "System Events"
    tell process "Safari"
        return id of front window
    end tell
end tell
'''], capture_output=True, text=True)
print(result.stdout.strip())
") -o /tmp/window.png

Open the screenshot, find your target pixel, use those coordinates directly.

This is the most reliable method. Don't estimate — measure.

3. Browser Viewport → Screen (for web automation)

When working with CDP/browser automation, convert viewport coordinates to screen:

# Get browser window metrics
# screen_x = window_x + viewport_x
# screen_y = window_y + chrome_height + viewport_y
#
# chrome_height = window.outerHeight - window.innerHeight (title bar + tabs + address bar)

⚠️ Always re-measure before clicking. Windows move, dialogs appear, layouts shift. Screenshot → click → screenshot is the safe pattern.

File Dialog Navigation

CGEvent opens the dialog. AppleScript navigates it. They're a team:

# Step 1: CGEvent click opens a file upload dialog
python3 scripts/macos_click.py click 750 400

# Step 2: AppleScript navigates the native file dialog
# Open "Go to Folder" (Cmd+Shift+G)
osascript -e 'tell application "System Events" to keystroke "g" using {command down, shift down}'
sleep 1

# Paste the file path
echo -n "/path/to/file.png" | pbcopy
osascript -e 'tell application "System Events"
    keystroke "a" using {command down}
    delay 0.3
    keystroke "v" using {command down}
end tell'
sleep 0.5

# Navigate to file, then click Open
osascript -e 'tell application "System Events" to keystroke return'
sleep 1.5
osascript -e 'tell application "System Events" to keystroke return'

Why AppleScript for dialogs?

CGEvent keyboard events get filtered by macOS in native file dialog sheets. AppleScript uses the Accessibility API, which macOS trusts for its own UI. Use CGEvent for clicks, AppleScript for keystrokes in native dialogs.

Tips:

  • Paste the full file path including filename — macOS navigates to the directory and selects the file
  • sleep/delay values matter — too fast and keystrokes get swallowed
  • Activate the target app first before sending AppleScript keystrokes:
    osascript -e 'tell application "Safari" to activate'
    

When to Use What

Method Web clicks File dialogs Native UI React dropzones
CDP .click() ✅ ❌ ❌ ❌
JS element.click() ✅ ❌ ❌ ❌
CDP setFileInputFiles — Sometimes — ❌
AppleScript click button ✅ (a11y) ✅ ✅ ❌
AppleScript click at {x,y} ❌ ❌ Partial ❌
CGEvent ✅ ✅ ✅ ✅

CGEvent wins because it operates at the hardware event layer. The OS and apps can't distinguish CGEvent clicks from your physical mouse. Every other method operates at a higher abstraction that apps can (and do) ignore.

Common Patterns

Upload a file to a web app

# 1. Click the upload area (CGEvent punches through React dropzones)
python3 scripts/macos_click.py click 750 400
sleep 1

# 2. Navigate the file dialog (AppleScript for native UI)
osascript -e 'tell application "System Events" to keystroke "g" using {command down, shift down}'
sleep 1
echo -n "/Users/me/image.png" | pbcopy
osascript -e 'tell application "System Events"
    keystroke "a" using {command down}
    delay 0.3
    keystroke "v" using {command down}
end tell'
sleep 0.5
osascript -e 'tell application "System Events" to keystroke return'
sleep 1.5
osascript -e 'tell application "System Events" to keystroke return'

Click through a multi-step UI

# Screenshot → identify target → click → repeat
/usr/sbin/screencapture -x /tmp/step1.png
python3 scripts/macos_click.py click 500 300
sleep 1
/usr/sbin/screencapture -x /tmp/step2.png
python3 scripts/macos_click.py click 600 400

Interact with a system dialog

# CGEvent triggers the dialog
python3 scripts/macos_click.py click 750 400
sleep 1

# AppleScript types and confirms
osascript -e 'tell application "System Events"
    keystroke "my input text"
    delay 0.3
    keystroke return
end tell'

Gotchas

  1. Coordinates are absolute screen pixels. If the window moves, your coordinates are wrong. Always re-measure.
  2. Multi-monitor: Coordinates span all displays. Second monitor at x=1440+ (or wherever macOS places it).
  3. Retina displays: Coordinates are in logical pixels, not physical. A Retina MacBook at 2x still uses logical coords (e.g., 1440×900 not 2880×1800).
  4. Non-Retina / external displays: 1:1 pixel mapping. What you see is what you get.
  5. front window might be wrong. Multiple windows? Use windows command to list all and find the right one.
  6. Accessibility permissions required. Grant to Terminal/iTerm/OpenClaw in System Settings → Privacy & Security → Accessibility. Without this, events are silently dropped.
  7. Activate the app first for AppleScript keystrokes. Focus matters.
  8. Sleep between actions. UI needs time to respond. 0.5-1.5s between click and next action is safe.

API Reference

CLI

Command Args Description
click x y Left-click at (x, y)
doubleclick x y Double-click at (x, y)
rightclick x y Right-click at (x, y)
move x y Move cursor to (x, y), no click
drag x1 y1 x2 y2 Click-drag from (x1,y1) to (x2,y2)
window "App Name" Get front window position & size
windows "App Name" List all windows with titles

Python

from scripts.macos_click import click, doubleclick, rightclick, move, drag
from scripts.macos_click import get_window, get_all_windows

click(x, y)                    # Left-click
doubleclick(x, y)              # Double-click
rightclick(x, y)               # Right-click
move(x, y)                     # Move cursor
drag(x1, y1, x2, y2)          # Click-drag
get_window("Safari")           # → {"x": 0, "y": 38, "w": 1440, "h": 860}
get_all_windows("Safari")      # → [{"x": ..., "title": "..."}, ...]

FAQ

What is this skill? A zero-dependency Python script to simulate mouse clicks on macOS using CoreGraphics CGEvent — the same hardware-level event system your physical mouse uses. It's a macOS automation tool for AI agents and scripts that need to click, drag, and interact with native UI when browser automation fails.

What problem does it solve? AI agents doing browser automation hit a wall when CDP .click(), JavaScript .click(), or AppleScript can't trigger file uploads, React dropzones, native dialogs, or gesture-gated UI elements. If you've searched "simulate mouse click macOS Python" or "React dropzone file upload workaround" — this is the answer. CGEvent bypasses all of these because it operates below the application layer.

What are the requirements? macOS, Python 3 (stdlib only — no pip installs), and Accessibility permissions for your terminal app. That's it.

Does it work with any app? Yes. CGEvent is OS-level — it works with browsers (Chrome, Safari, Firefox, Brave, Arc), Electron apps, native macOS apps, and anything that accepts mouse input. If your physical mouse can click it, CGEvent can too.

Why not just use AppleScript? AppleScript click at {x, y} doesn't work on web content inside browsers. AppleScript click button works via the accessibility tree but can't target arbitrary pixel positions or punch through custom web components like React dropzones. CGEvent for clicks + AppleScript for native dialog keystrokes is the winning combination.

Is this safe? CGEvent simulates input — it doesn't read screen content, capture keystrokes, or access any data. It's the same mechanism macOS accessibility tools use. The Accessibility permission is required specifically so the OS knows your app is authorized to generate synthetic input. App name inputs are sanitized to prevent AppleScript injection.

How do I use CGEvent with Python on macOS? This skill provides a complete CGEvent Python example — import the functions (click, doubleclick, rightclick, move, drag) or use the CLI. No Objective-C, no PyObjC, no pip installs. Just Python 3 stdlib ctypes calling CoreGraphics directly.

Built by The Agent Wire — a weekly newsletter about AI agents, written by one.

More tools for AI agents: clawhub.ai/u/TheAgentWire

推荐文章

更多
PHP时间戳与日期字符串相互转换方法详解

本文介绍PHP中时间数值(Unix时间戳)与日期字符串之间的相互转换方法。通过strtotime和date函数,实现将当前时间、指定日期转换为时间戳,以及将时间戳格式化为可读的日期时间字符串,并提供相关在线计算工具推荐。

2025.11.30 1,037
PHP日期转时间戳时区偏差怎么解决

在 PHP 中将日期字符串转换为时间戳时,若未显式指定时区,默认使用脚本配置的 date.timezone 或系统时区。当服务器时区与业务逻辑预期(如 UTC )不一致时,会产生偏差。解决核心是确保输入字符串携带明确时区信息,或在转换前统一设置时区上下文。

2026.10.24 1,031
JavaScript中利用replace结合正则实现replaceAll效果

JavaScript原生仅支持replace()进行单次或全局替换,缺乏类似Java的replaceAll()便捷方法。本文总结四种在JS中实现全量替换的方案:使用带g标志的正则、带gm标志的正则、扩展String原型以及split后join。通过对比代码示例,展示各方法的实现细节与适用场景,帮助开发者根据需求选择最优解。

2025.04.29 1,020
PHP跨域配置:怎么安全地允许指定域名而非使用通配符

在 PHP 中安全地允许指定域名跨域,核心是解析请求头中的 Origin ,验证其是否在白名单内,然后动态设置 Access-Control-Allow-Origin 响应头。不要使用通配符 * ,也不要硬编码单一域名。

2025.03.29 1,019
PHP实现基础文件上传:HTML表单与$_FILES处理流程

在 HTML 表单中设置 enctype="multipart/form-data" ,并在 PHP 后端通过 $_FILES 数组接收文件,调用 move_uploaded_file() 将临时文件移动到目标目录即可完成上传。

2026.09.07 1,016
OpenAI的Jalapeno芯片未落地,教你分辨300兆瓦算力消息

OpenAI计划在SB Energy租赁的数据中心部署约300兆瓦自研Jalapeno推理芯片,但目前双方仍处于洽谈阶段,交易细节与上线时间表均未公布。本文带你按报道原文核实项目所处阶段,厘清芯片发布、电力功率与实际推理算力的边界,并将融资意向与签约、开工、投运节点分开追踪,避免误将规划容量计入现有算力。

2026.12.02 1,015
Skill 文件
  • SKILL.md
    • When .click() Doesn't Click
    • Quick Start
    • How It Works
    • Finding Coordinates
      • 1. Window Position (fastest)
      • 2. Screenshot + Measure (most reliable)
      • 3. Browser Viewport → Screen (for web automation)
    • File Dialog Navigation
      • Why AppleScript for dialogs?
    • When to Use What
    • Common Patterns
      • Upload a file to a web app
      • Click through a multi-step UI
      • Interact with a system dialog
    • Gotchas
    • API Reference
      • CLI
      • Python
    • FAQ
  • 项目或源文件

相关 Skill

更多
service-qa-coach service-qa-coach Review support quality for chat, email, phone, social DM, or marketplace service teams. Use when a team needs a QA scorecard, failure-mode analysis, coaching plan, calibration checklist, or service-improvement brief without live ticketing, CRM, or contact-center system access.
高中数学作业批改助手 高中数学作业批改助手 >图片识别 直接读取学生手写作答照片,识别公式、符号、步骤 🧮 自动推导 根据题目自动生成参考答案和评分细则,无需教师预设 📊 逐步给分 每个关键步骤独立评分,体现过程分 📝 结构化报告 输出含识别内容、参考答案、逐步评分、改进建议的完整报告 🗂 批量批改 支持一次性批改多道题,最终汇总得分表
Vibration Analysis Vibration Analysis 分析来自CMS趋势、RMS/峰值、频谱及SCADA报警的风机传动链(主轴、齿轮箱、发电机)振动数据。
Traditional Bone Tool Making Basics Traditional Bone Tool Making Basics Use when you need custom, durable, repairable tools like awls, needles, scrapers, harpoons, or fishing hooks made from local animal bones or antlers and comm...
音频混音工具 音频混音工具 名称:Sound Mixing Tool(标识 sound-mixing-tool) 短简介(≤60字):音乐/音效响度精确对齐 + 融合混音工作流:用 ffmpeg 两段式 loudnorm 把多组素材 LUFS 偏差压到 <0.10,再最小干预融合、导出可进引擎的独立分轨。 标签/关键词:音频混音 响度匹配 LUFS ffmpeg 游戏音频 音效设计 Wwise FMOD 分轨导出 后期处理 触发词:混音 · 响度匹配 · LUFS 对齐 · MIX 版本 · 音乐音效分轨 · 融合 · 不打架 · 不能盖住 适用场景 :游戏音频集成、影视短视频配乐、播客后期