Execute AppleScript / JXA scripts via osascript to control macOS applications and the system.
Use Cases
- Need to control native macOS applications (Finder, Safari, Mail, etc.)
- Need to show dialog boxes to prompt the user
- Need to operate system settings (volume, brightness, Wi-Fi, etc.)
- Called internally by other Skills
Common AppleScript Commands
Application Control
# 打开应用
osascript -e 'tell application "Safari" to activate'
Quit Application
osascript -e 'tell application "Safari" to quit'
Get the current frontmost applicationosascript -e 'tell application "System Events" to get name of first application process whose frontmost is true'
### 对话框
```bash
# 显示消息
osascript -e 'display dialog "操作完成!" with title "小搭子" buttons {"好的"} default button "好的"'
# 选择列表
osascript -e 'choose from list {"选项A", "选项B", "选项C"} with title "请选择" with prompt "你想要哪个?"'
# 选择文件
osascript -e 'choose file with prompt "选择一个文件"'
# 选择文件夹
osascript -e 'choose folder with prompt "选择一个文件夹"'
System Information
# 获取当前 Wi-Fi 名称
osascript -e 'do shell script "networksetup -getairportnetwork en0 | cut -d: -f2 | xargs"'
# 获取系统音量
osascript -e 'output volume of (get volume settings)'
# 设置音量
osascript -e 'set volume output volume 50'
# 获取电池百分比
osascript -e 'do shell script "pmset -g batt | grep -o \"[0-9]\\+%\""'
# 获取屏幕亮度
osascript -e 'tell application "System Events" to get value of value indicator 1 of slider 1 of group 1 of window 1 of application process "System Preferences"'
Finder Operations
# 获取当前 Finder 窗口路径
osascript -e 'tell application "Finder" to get POSIX path of (target of front Finder window as alias)'
# 在 Finder 中显示文件
osascript -e 'tell application "Finder" to reveal POSIX file "/path/to/file"'
# 获取选中的文件
osascript -e 'tell application "Finder" to get POSIX path of (selection as alias)'
JXA(JavaScript for Automation)
# JXA 替代 AppleScript
osascript -l JavaScript -e '
const app = Application.currentApplication();
app.includeStandardAdditions = true;
app.displayDialog("Hello from JXA!");
'
# JXA 获取 Safari 标签页
osascript -l JavaScript -e '
const safari = Application("Safari");
const tabs = safari.windows[0].tabs();
tabs.map(t => t.url()).join("\n");
'
Safety Rules
- Do not silently perform destructive operations: Deleting files, sending emails, etc. must first be confirmed via HITL
- Do not operate on passwords and keys: Do not use AppleScript to operate the keychain
- When permissions are insufficient, open Settings directly: If an operation fails due to permissions, call the
open_system_preferencestool to open the corresponding settings panel (such as accessibility), do not describe the steps in text