🚀 使用 AppleScript 與 macOS 應用交互指南
本項目聚焦於展示如何藉助 AppleScript 實現與 macOS 系統及其應用程序的交互。文檔涵蓋項目使用方法、實現細節以及開發流程等多方面內容,助您快速掌握相關技能。
🚀 快速開始
本項目旨在通過 AppleScript 實現與 macOS 系統及應用的交互,您可參考後續內容瞭解具體功能及開發方法。
✨ 主要特性
基礎功能
日曆操作
tell application "Calendar"
add event "Event Name" at date (current date)
end tell
tell application "Calendar"
get every event of calendar "工作"
end tell
系統偏好設置
tell application "System Preferences"
set the brightness of display to 0.8
end tell
tell application "Network"
connect using interface "Wi-Fi" with name "MyNetwork" and password "password123"
end tell
文件操作
tell application "Finder"
duplicate the item "/Users/Shared/test.txt" to folder "/Users/user/Desktop"
end tell
tell application "Finder"
make new folder at "/Users/user/Documents" named "新建文件夾"
end tell
高級功能
跨應用程序交互
tell application "Safari"
open location "https://modelcontextprotocol.io"
end tell
tell application "Finder" to get the selection
tell application "TextEdit" to open the result
💻 使用示例
基礎用法
以下是日曆操作、系統偏好設置和文件操作的基礎示例代碼:
tell application "Calendar"
add event "Event Name" at date (current date)
end tell
tell application "System Preferences"
set the brightness of display to 0.8
end tell
tell application "Finder"
duplicate the item "/Users/Shared/test.txt" to folder "/Users/user/Desktop"
end tell
高級用法
以下是跨應用程序交互的高級示例代碼:
tell application "Safari"
open location "https://modelcontextprotocol.io"
end tell
tell application "Finder" to get the selection
tell application "TextEdit" to open the result
🔧 技術細節
技術架構
- 腳本引擎:基於 AppleScript 的語言處理。
- 執行環境:使用
osascript
命令在終端中運行腳本。
- 通信協議:遵循 Model Context Protocol (MCP) 協議。
執行流程
- 接收 MCP 請求
- 解析請求參數
- 生成對應的 AppleScript 代碼
- 使用
osascript
執行腳本
- 處理並返回執行結果
📚 詳細文檔
開發指南
環境配置
npm install
npm run build
npx @modelcontextprotocol/inspector node 指定路徑/index.js 參數...
新功能開發
創建新分類文件
在 src/categories
目錄下新建一個 TypeScript 文件:
import { ScriptCategory } from "../types/index.js";
export const 新分類: ScriptCategory = {
name: "新分類名稱",
scripts: [
{
name: "腳本1",
description: "腳本功能描述",
execute: (params) => {
return result;
}
}
]
};
添加具體實現
在 scripts
數組中添加具體的腳本定義,每個腳本包含名稱、描述和執行函數。
高級主題
動態腳本生成
可以根據輸入參數動態生成 AppleScript 代碼。例如:
generateScript(params: any): string {
let script = '';
if (params.action === 'openUrl') {
script = `tell application "Safari" to open location "${params.url}"`;
}
return script;
}
複雜數據處理
在腳本執行前後進行復雜的數據處理,例如:
processData(data: any): any {
const cleanedData = data.map(item => ({
id: item.id,
name: item.name.toUpperCase()
}));
return cleanedData;
}
調試技巧
使用 MCP 檢查工具
通過 modelcontextprotocol/inspector
工具監控和調試腳本執行過程。
日誌設置
在代碼中添加日誌輸出:
import { Logger } from '../utils/logger';
const logger = new Logger('script-executor');
logger.info('開始執行腳本');
try {
} catch (error) {
logger.error('腳本執行失敗', error);
}
資源鏈接
貢獻指南
如何貢獻
- Fork 項目倉庫
- 創建功能分支
- 提交代碼更改
- 開啟 Pull Request
代碼規範
- 遵循項目編碼風格
- 提供詳細的提交信息
- 確保單元測試通過
📄 許可證
本項目遵循 MIT 許可證,具體內容如下:
MIT License
版權 © 年份 作者姓名。
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
通過以上指南,您可以深入瞭解如何使用 AppleScript 與 macOS 應用程序進行交互,並根據需要開發和擴展相關功能。