macOS UI Automation Toolkit: Drive the Desktop with Python, Empower Agents to Execute

Preface

When performing repetitive UI operations on macOS, the common route is to hand-write AppleScript or stitch together osascript, pyautogui, and screenshot logic in Python. As scripts grow in complexity, it becomes harder to explain permission requirements, module boundaries, and error retries in a single conversation. If you are already writing code with agents in Cursor, Claude Code, or similar environments, you likely want a structured skill description that tells the model exactly how to open apps, perform clicks, match screenshots, and chain steps instead of improvising every time.

Below is a community skill on SkillHub: macOS UI Automation Toolkit – Control Your Mac with Python (identifier @user_2e438cd6/macos-desktop-controller-v1). It breaks desktop automation into four layers—application control, keyboard/mouse interaction, vision, and workflows—and ships with Python implementations plus a SKILL.md guide so that agents can generate runnable automation scripts in authorized environments.

What Is This

In one sentence: a Python desktop UI automation skill package for macOS 14+ (Sonoma) that uses PyAutoGUI, AppleScript, and OpenCV template matching to control the local interface. As an Agent Skill, it mainly solves the “model doesn’t know what modules, default configurations, and step formats exist in the project” problem.

  • Origin: A SkillHub community skill published by user_2e438cd6 (display name “戈壁老王”), categorized under dev-programming, current version 1.0.2, tag requires_api_key=false (no API key required).
  • Difference from ad-hoc scripts: After installation, the agent reads the skill directory’s SKILL.md and Python modules (app_control, click_control, vision_control, workflow, etc.) and composes code according to documented action names and configuration keys rather than piecing together scattered commands.

The SkillHub page description emphasizes: control your Mac with code, no need to learn AppleScript separately. Internally, it still calls osascript for certain window and application operations, but daily usage is primarily through Python APIs.

Core Functionality

The code in the skill is organized into modules. The following capabilities match the repository implementation (excerpted).

Application Control (AppControl)

  • Launch apps via open -a, optionally waiting for the process to be ready (psutil).
  • Gracefully or force-quit applications, query whether they’re running, and activate the frontmost window.
  • Use AppleScript for window moving, resizing, minimizing, and maximizing.

Keyboard & Mouse (ClickControl)

  • PyAutoGUI-based coordinate clicking, multi-clicks, dragging, scrolling, and key presses / hotkeys.
  • Configurable interval for text input; coordinates are validated to be within screen bounds.
  • “Click by element name” is documented but not implemented (logs a warning and fails); use template matching to find coordinates first and then click.

Vision (VisionControl)

  • Full-screen or region screenshots (Pillow / PyAutoGUI), optionally saved to files.
  • OpenCV template matching (find_template, wait_for_template, etc.) with screenshot caching and threshold configuration.

Workflow (WorkflowManager)

  • execute_steps accepts a list of steps; built-in actions include open_app, close_app, activate_window, click_at, click_template, write_text, press_key, drag_from_to, scroll, wait, wait_for_template, capture_screen, condition, loop, etc.
  • Per-step options for continue_on_failure and delay_after; results are written to workflow history.

Configuration

  • Loads defaults from config.json, the user directory ~/.macos_controller_config.json, or the environment variable MACOS_CONTROLLER_CONFIG.
  • Common options: default_timeout, retry_count, screenshot_dir, template_dir, vision_threshold, mouse_speed, etc.; environment variables like MACOS_CONTROLLER_LOG_LEVEL and MACOS_CONTROLLER_SCREENSHOT_DIR are supported as documented.

Installation & Activation

System & Environment

The SkillHub listing requires:

  • macOS 14+ (Sonoma)
  • Python 3.9+
  • Enable Accessibility and Screen Recording in System Settings; the skill docs also recommend granting Automation permissions (to control Safari / Finder, etc.).

The package’s SKILL.md mentions Python 3.8+. To match the host environment, it’s recommended to satisfy the SkillHub page requirement of 3.9+ before installing dependencies.

Installing the Skill (Agent)

The official install prompt:

Please install `@user_2e438cd6/macos-desktop-controller-v1` according to https://skillhub.cn/install/skillhub.md.

If the SkillHub CLI is installed, you can also run it in the agent’s skills directory (path depends on your client; see SkillHub install instructions):

skillhub install @user_2e438cd6/macos-desktop-controller-v1 --dir <your agent skills directory>

Common directories (from the same docs):

  • Cursor: ~/.cursor/skills/
  • Claude Code: ~/.claude/skills/
  • Codex: ~/.codex/skills/ or .agents/skills/ in the project

After installing, reload the skills directory in your agent (restart or refresh per client) so it picks up the new skill.

Installing Python Dependencies

On the Mac host, in the skill directory or your project environment:

pip install -r requirements.txt

Core dependencies in requirements.txt include pyautogui, psutil, opencv-python, numpy, Pillow; optional pyobjc-framework-Cocoa, pyobjc-framework-Quartz.

Typical Usage

The examples below match the class APIs in the skill (add the skill directory to PYTHONPATH, or import the same module names in your project). The import from macos_desktop_controller import app_control shown in SKILL.md is inconsistent with the current package structure; use class instances as shown here.

1. Launching, Activating, and Closing Apps

from app_control import AppControl

ctrl = AppControl()
ctrl.open_app("Safari")
ctrl.activate_window("Safari")
ctrl.close_app("Safari")

2. Clicking and Text Input

from click_control import ClickControl

mouse = ClickControl()
mouse.click_at(500, 300)
mouse.write_text("Hello World", interval=0.05)
mouse.drag_from_to(100, 100, 500, 500, duration=1.0)

3. Screenshot & Template Matching

from click_control import ClickControl
from vision_control import VisionControl

vision = VisionControl()
mouse = ClickControl()

vision.capture_screen(save_path="/tmp/screen.png")
position = vision.find_template("target.png", threshold=0.8)
if position:
    mouse.click_at(position[0], position[1])

4. Declarative Workflows

from workflow import WorkflowManager

steps = [
    {"action": "open_app", "params": {"app_name": "Safari"}},
    {"action": "wait", "params": {"duration": 2}},
    {"action": "click_template", "params": {"template_path": "search_bar.png"}},
    {"action": "write_text", "params": {"text": "macOS automation"}},
    {"action": "press_key", "params": {"key": "enter"}},
]

WorkflowManager().execute_steps(steps)

Note: The workflow step click_template uses the parameter name template_path (different from the template field in some documentation examples; follow the workflow.py implementation).

Use Cases & Cautions

Who It’s For

  • Developers on macOS who need repetitive GUI operations (test aids, internal tools, batch clicking).
  • Users of Cursor / Claude Code who want the agent to generate automation scripts from fixed modules instead of rewriting AppleScript snippets each time.

Limitations & Gotchas

  • Platform: macOS only; the control logic won’t run directly on Linux or Windows.
  • Permissions: Without granting Accessibility / Screen Recording, mouse/keyboard and screenshots will fail; confirm each item in “Privacy & Security” on first run.
  • Version Notes: The SkillHub package version is 1.0.2, but the package’s __init__.py marks __version__ = "2.0.0" and the documentation title says “v2”; for troubleshooting, refer to the actual installed directory and _meta.json.
  • Docs vs Code: Some import examples in SKILL.md don’t match module export names; clicking by element ID is not implemented—use coordinates or template matching.
  • Safety: Automation performs real actions on your local UI; review the step scope before handing scripts to an agent to avoid closing apps or triggering sensitive actions unintentionally.

Final Thoughts

This macOS UI automation skill consolidates application, keyboard/mouse, vision, and workflows into a set of Python modules and documents them in an agent-readable SKILL.md. It’s suitable for driving desktop scripts via conversation on Sonoma and newer, instead of wrestling with AppleScript alone. See SkillHub for installation and details:

  • Page: https://www.skillhub.cn/skills/user_2e438cd6/macos-desktop-controller-v1
  • API endpoint: https://api.skillhub.cn/user_2e438cd6/macos-desktop-controller-v1

(SkillHub listing data: rating 100000, downloads 979, source: community.)

羽毛球分组比赛记分
小程序二维码

欢迎使用《羽毛球分组比赛记分》微信小程序

Xiaoye