macos-automation
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
Chinesemacos-automation
macOS自动化
Purpose
用途
This skill automates tasks on macOS using tools like AppleScript, JXA, Shortcuts, Automator, osascript, System Events, and the accessibility API, enabling script-based control of apps and system functions.
本技能借助AppleScript、JXA、Shortcuts、Automator、osascript、System Events以及辅助功能API,实现macOS上的任务自动化,支持通过脚本控制应用程序和系统功能。
When to Use
适用场景
- Automate repetitive workflows, such as file management or app interactions.
- Integrate with macOS apps for custom behaviors, like triggering actions via keyboard shortcuts.
- Script complex sequences involving multiple apps, e.g., when Shortcuts alone isn't sufficient.
- Handle accessibility tasks, like UI element manipulation, when apps don't expose direct APIs.
- Use for rapid prototyping of automations in environments like shell scripts or IDEs.
- 自动化重复工作流,例如文件管理或应用交互操作。
- 与macOS应用集成以实现自定义行为,比如通过键盘快捷键触发操作。
- 编写涉及多个应用的复杂流程脚本,例如当仅使用Shortcuts无法满足需求时。
- 处理辅助功能相关任务,比如在应用未开放直接API时操作UI元素。
- 在Shell脚本或IDE等环境中快速原型化自动化流程。
Key Capabilities
核心功能
- Execute AppleScript via osascript for app control, e.g., manipulating Finder or Safari.
- Run JXA (JavaScript for Automation) to interact with macOS APIs, supporting modern JavaScript syntax.
- Create and invoke Shortcuts programmatically for quick actions, like sharing files.
- Use Automator workflows as scripts, convertible to applications or services.
- Leverage System Events for UI automation, including keyboard/mouse simulation via accessibility API.
- Access macOS-specific APIs like NSAppleScript for embedding in Objective-C/Swift code.
- 通过osascript执行AppleScript以控制应用,例如操作Finder或Safari。
- 运行JXA(JavaScript for Automation)与macOS API交互,支持现代JavaScript语法。
- 以编程方式创建和调用Shortcuts以完成快速操作,比如文件分享。
- 将Automator工作流用作脚本,可转换为应用程序或服务。
- 利用System Events实现UI自动化,包括通过辅助功能API模拟键盘/鼠标操作。
- 访问macOS专属API(如NSAppleScript),以便嵌入到Objective-C/Swift代码中。
Usage Patterns
使用模式
- Run scripts from the command line using osascript; for example, pipe AppleScript code directly.
- Embed JXA in Node.js environments by loading the JXA module and executing via .
Application('Finder').activate() - Chain Shortcuts with other tools by exporting as URLs and invoking via command.
open - Use Automator to build workflows, then save as .applescript files for osascript execution.
- For accessibility, ensure "Accessibility" is enabled in System Preferences > Security & Privacy, then use System Events to target UI elements like .
tell application "System Events" to click button "OK" of window "Main" - Always check for required permissions, such as Full Disk Access, before running scripts that access files.
- 使用osascript从命令行运行脚本;例如,直接传入AppleScript代码。
- 通过加载JXA模块并执行,在Node.js环境中嵌入JXA。
Application('Finder').activate() - 将Shortcuts导出为URL,再通过命令调用,实现与其他工具的联动。
open - 使用Automator构建工作流,然后保存为.applescript文件供osascript执行。
- 如需使用辅助功能,请先在「系统偏好设置」>「安全性与隐私」中启用「辅助功能」,然后使用System Events定位UI元素,例如。
tell application "System Events" to click button "OK" of window "Main" - 在运行访问文件的脚本前,务必检查是否具备所需权限,例如「完全磁盘访问权限」。
Common Commands/API
常用命令/API
- osascript CLI: Use to create a folder; handle errors by checking the exit code.
osascript -e 'tell application "Finder" to make new folder at desktop' - JXA Example: In a JavaScript file, use to open and navigate a URL.
const app = Application('Safari'); app.activate(); app.documents[0].url = 'https://example.com'; - Shortcuts Integration: Run a shortcut via ; pass inputs as JSON for parameterized execution.
shortcuts run "My Shortcut" -i '{"input": "text"}' - Automator API: Convert workflows to AppleScript with .
tell application "Automator" to run workflow "path/to/workflow" - System Events API: For UI automation, use to simulate Command+A.
osascript -e 'tell application "System Events" to keystroke "a" using {command down}' - Config Formats: Store scripts in plain text files (.applescript) and invoke with ; for JXA, use ES6 modules in .js files.
osascript path/to/script.applescript
- osascript命令行工具:使用创建文件夹;通过检查退出码处理错误。
osascript -e 'tell application "Finder" to make new folder at desktop' - JXA示例:在JavaScript文件中,使用打开并导航到指定URL。
const app = Application('Safari'); app.activate(); app.documents[0].url = 'https://example.com'; - Shortcuts集成:通过运行快捷指令;以JSON格式传入参数实现参数化执行。
shortcuts run "My Shortcut" -i '{"input": "text"}' - Automator API:使用将工作流转换为AppleScript。
tell application "Automator" to run workflow "path/to/workflow" - System Events API:如需UI自动化,使用模拟Command+A操作。
osascript -e 'tell application "System Events" to keystroke "a" using {command down}' - 配置格式:将脚本存储为纯文本文件(.applescript),通过调用;对于JXA,在.js文件中使用ES6模块。
osascript path/to/script.applescript
Integration Notes
集成说明
- Integrate with other tools by wrapping osascript calls in shell scripts; e.g., use and run via
$ export APPLESCRIPT_PATH='/path/to/script'.osascript $APPLESCRIPT_PATH - For JXA, require the OSA module in Node.js: and use
npm install osa.const OSA = require('osa'); OSA.runAppleScript('...') - If accessibility features are needed, set the env var after granting permissions in System Preferences.
$ACCESSIBILITY_ENABLED=1 - Auth/keys: For scripts accessing protected resources, use env vars like (though rare); more commonly, handle macOS permissions via
$osascript_API_KEYCLI, e.g.,tccutilto reset access.tccutil reset AppleEvents com.example.app - Embed in larger apps by compiling AppleScript into bundles or using JXA in Electron apps for cross-tool integration.
- 通过在Shell脚本中封装osascript调用,实现与其他工具的集成;例如,使用,再通过
$ export APPLESCRIPT_PATH='/path/to/script'运行。osascript $APPLESCRIPT_PATH - 对于JXA,在Node.js中引入OSA模块:,然后使用
npm install osa。const OSA = require('osa'); OSA.runAppleScript('...') - 如需使用辅助功能,在「系统偏好设置」中授予权限后,设置环境变量。
$ACCESSIBILITY_ENABLED=1 - 认证/密钥:对于访问受保护资源的脚本,可使用环境变量(如,这种情况较少见);更常见的是通过
$osascript_API_KEY命令行工具管理macOS权限,例如tccutil重置访问权限。tccutil reset AppleEvents com.example.app - 通过将AppleScript编译为包,或在Electron应用中使用JXA,将自动化功能嵌入到大型应用中,实现跨工具集成。
Error Handling
错误处理
- Check osascript exit codes: If a script fails, capture with and parse stderr for messages like "Execution error: ...".
osascript -e 'script' 2>&1 - In JXA, wrap code in try-catch: to handle app not found errors.
try { Application('Finder').activate(); } catch (e) { console.error(e.message); } - For Shortcuts, verify with and check output for failures.
shortcuts run "Name" --wait-for-result - Use Automator's built-in logging by enabling in workflow settings, then review logs via Console app.
- General pattern: Always run scripts with elevated privileges if needed, e.g., via , and handle permission errors by prompting users to adjust settings.
sudo osascript -e '...'
- 检查osascript退出码:如果脚本执行失败,使用捕获错误,并解析stderr中的消息(如"Execution error: ...")。
osascript -e 'script' 2>&1 - 在JXA中,使用try-catch包裹代码:,处理应用未找到等错误。
try { Application('Finder').activate(); } catch (e) { console.error(e.message); } - 对于Shortcuts,使用执行并检查输出是否失败。
shortcuts run "Name" --wait-for-result - 在Automator工作流设置中启用内置日志,然后通过「控制台」应用查看日志。
- 通用模式:如有需要,始终使用提升权限运行脚本(例如),并通过提示用户调整设置来处理权限错误。
sudo osascript -e '...'
Concrete Usage Examples
具体使用示例
- Automate App Launch and Action: Use osascript to open Safari and load a page: . This is useful for daily workflows; extend by adding error checks.
osascript -e 'tell application "Safari" to open location "https://example.com"' followed by 'tell application "Safari" to activate' - UI Automation with Shortcuts: Create a Shortcut to resize windows, then invoke via CLI: . Combine with JXA for dynamic inputs, e.g., in a script:
shortcuts run "Resize Window" -i '{"app": "Finder", "width": 800}'.Application('Shortcuts').run('Resize Window', {input: JSON.stringify({app: 'Finder'})})
- 自动化应用启动与操作:使用osascript打开Safari并加载页面:,接着执行
osascript -e 'tell application "Safari" to open location "https://example.com"'。这适用于日常工作流;可通过添加错误检查进行扩展。tell application "Safari" to activate - 结合Shortcuts实现UI自动化:创建一个用于调整窗口大小的Shortcut,然后通过命令行调用:。结合JXA实现动态输入,例如在脚本中执行:
shortcuts run "Resize Window" -i '{"app": "Finder", "width": 800}'。Application('Shortcuts').run('Resize Window', {input: JSON.stringify({app: 'Finder'})})
Graph Relationships
关联关系
- Related to: macos cluster (direct parent), applescript skill (sub-skill for scripting), shortcuts skill (integrated tool), automation cluster (broader category).
- Dependencies: Requires macos-os (base system), interacts with accessibility-api skill for UI tasks.
- 相关技能:macos集群(直接父类)、applescript技能(脚本子技能)、shortcuts技能(集成工具)、automation集群(更广泛分类)。
- 依赖项:需要macos-os(基础系统),与accessibility-api技能交互以完成UI任务。