Applescript Mcp

Applescript Mcp

🚀 使用 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

高级功能

跨应用程序交互

  • 在 Safari 中打开链接
tell application "Safari"
open location "https://modelcontextprotocol.io"
end tell
  • 从 Finder 传输文件到其他应用
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

高级用法

以下是跨应用程序交互的高级示例代码:

-- 在 Safari 中打开链接
tell application "Safari"
open location "https://modelcontextprotocol.io"
end tell

-- 从 Finder 传输文件到其他应用
tell application "Finder" to get the selection
tell application "TextEdit" to open the result

🔧 技术细节

技术架构

  • 脚本引擎:基于 AppleScript 的语言处理。
  • 执行环境:使用 osascript 命令在终端中运行脚本。
  • 通信协议:遵循 Model Context Protocol (MCP) 协议。

执行流程

  1. 接收 MCP 请求
  2. 解析请求参数
  3. 生成对应的 AppleScript 代码
  4. 使用 osascript 执行脚本
  5. 处理并返回执行结果

📚 详细文档

开发指南

环境配置

# 安装依赖项
npm install

# 构建服务器
npm run build

# 启动 MCP 检查工具
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);
}

资源链接

贡献指南

如何贡献

  1. Fork 项目仓库
  2. 创建功能分支
  3. 提交代码更改
  4. 开启 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 应用程序进行交互,并根据需要开发和扩展相关功能。

  • 0 关注
  • 0 收藏,18 浏览
  • system 提出于 2025-09-18 15:15

相似服务问题

相关AI产品