
作为一个经常需要跨境购物的用户我一直在寻找能够快速完成货币换算的方法。今天我要分享的「剪贴板金额换算器」——一个仅用55 行 AppleScript 代码实现的效率工具能够一键将剪贴板中的金额换算成人民币、台币和美元。整个过程只需要复制金额、运行脚本两步就能得到三种货币的换算结果。技术栈简介为什么选择 AppleScriptAppleScript 是 macOS 内置的脚本语言可以让用户自动化控制应用程序和操作系统。它的优势在于无需安装- macOS 系统自带无需额外安装任何软件剪贴板访问- 能够直接读取和操作系统剪贴板内容Shell 集成- 内置do shell script命令可以直接调用 curl、sed 等命令行工具网络请求- 通过 shell 命令可以轻松发起 HTTP 请求获取实时数据对于需要与系统深度集成的场景特别是涉及剪贴板操作和网络请求的任务AppleScript 是非常合适的选择。Exchange Rate APIExchange Rate API 是一个提供实时汇率数据的免费 API 服务。它的特点包括免费额度- 每月提供 1500 次免费请求对于个人使用完全足够实时更新- 汇率数据每小时更新一次保证准确性多币种支持- 支持全球 160 多种货币的汇率查询简单易用- 通过 URL 即可获取 JSON 格式的汇率数据无需复杂的认证流程我们使用https://api.exchangerate-api.com/v4/latest/CNY这个端点以人民币为基础货币获取最新的汇率数据。Protocol Launcher 的桥梁作用Protocol Launcher 是一个 JavaScript 库提供了类型安全的方式生成各种应用的深度链接Deep Link。通过它我们可以将 AppleScript 脚本嵌入到网页中用户点击按钮即可直接运行预设的脚本实现 Web 与本地应用的完美结合。核心功能详解这个「剪贴板金额换算器」虽然代码精简但功能却相当完善自动读取剪贴板- 无需手动粘贴脚本自动获取剪贴板中的内容智能货币识别- 自动识别美元 ($)、欧元 (€)、人民币 (¥) 等常见货币符号金额提取- 通过正则表达式从复杂文本中提取出数值部分实时汇率获取- 通过 Exchange Rate API 获取最新汇率数据多币种换算- 一次换算同时显示人民币、台币和美元三种货币友好错误提示- 当剪贴板为空、无法识别货币或网络请求失败时提供清晰的中文提示完整代码实现下面的代码展示了完整的实现逻辑import{addScript}fromprotocol-launcher/apple-scriptconsturladdScript({script:set selectedText to the clipboard as string if selectedText is then display dialog 剪贴板为空请先复制金额 buttons {OK} return end if set currency to if selectedText contains $ then set currency to USD else if selectedText contains € then set currency to EUR else if selectedText contains ¥ then set currency to CNY else display dialog 无法识别货币符号 buttons {OK} return end if set numText to do shell script echo quoted form of selectedText | sed s/[^0-9.]//g if numText is then display dialog 未识别到金额 return end if set amount to numText as number try set rateJSON to do shell script curl -s https://api.exchangerate-api.com/v4/latest/CNY on error display dialog 获取汇率失败 return end try set twdRate to do shell script echo quoted form of rateJSON | sed -n s/.*\TWD\:\\([0-9.]*\\).*/\\1/p set usdRate to do shell script echo quoted form of rateJSON | sed -n s/.*\USD\:\\([0-9.]*\\).*/\\1/p if twdRate is or usdRate is then display dialog 汇率解析失败 return end if if currency is CNY then set cnyValue to amount set twdValue to amount * (twdRate as number) set usdValue to amount * (usdRate as number) else if currency is USD then set cnyValue to amount / (usdRate as number) set twdValue to cnyValue * (twdRate as number) set usdValue to amount else if currency is EUR then set cnyValue to amount / ((do shell script echo quoted form of rateJSON | sed -n s/.*\EUR\:\\([0-9.]*\\).*/\\1/p) as number) set twdValue to cnyValue * (twdRate as number) set usdValue to cnyValue * (usdRate as number) end if display dialog 换算结果\n\n原始 selectedText \n\n人民币¥ (round cnyValue) \n台币NT$ (round twdValue) \n美元$ (round usdValue) buttons {OK},})代码解析让我们逐段解析这段 AppleScript 的实现逻辑第一步读取剪贴板set selectedText to the clipboard as string if selectedText is then display dialog 剪贴板为空请先复制金额 buttons {OK} return end if首先尝试读取剪贴板内容如果为空则显示提示对话框并退出。这里使用as string确保获取到的是文本格式。第二步识别货币类型set currency to if selectedText contains $ then set currency to USD else if selectedText contains € then set currency to EUR else if selectedText contains ¥ then set currency to CNY else display dialog 无法识别货币符号 buttons {OK} return end if通过检查货币符号来判断原始金额的币种。如果无法识别任何已知货币符号则提示用户。第三步提取金额数值set numText to do shell script echo quoted form of selectedText | sed s/[^0-9.]//g if numText is then display dialog 未识别到金额 return end if set amount to numText as number使用sed命令过滤掉所有非数字和小数点的字符从复杂的文本中提取出纯数值部分。如果提取结果为空说明文本中没有有效金额。第四步获取实时汇率try set rateJSON to do shell script curl -s https://api.exchangerate-api.com/v4/latest/CNY on error display dialog 获取汇率失败 return end try通过 curl 命令从 Exchange Rate API 获取以人民币为基础货币的汇率数据。使用 try-catch 捕获网络请求失败的情况。第五步解析汇率数据set twdRate to do shell script echo quoted form of rateJSON | sed -n s/.*\TWD\:\\([0-9.]*\\).*/\\1/p set usdRate to do shell script echo quoted form of rateJSON | sed -n s/.*\USD\:\\([0-9.]*\\).*/\\1/p if twdRate is or usdRate is then display dialog 汇率解析失败 return end if同样使用 sed 命令从 JSON 响应中提取出台币和美元的汇率。如果解析失败则提示用户。第六步执行货币换算if currency is CNY then set cnyValue to amount set twdValue to amount * (twdRate as number) set usdValue to amount * (usdRate as number) else if currency is USD then set cnyValue to amount / (usdRate as number) set twdValue to cnyValue * (twdRate as number) set usdValue to amount else if currency is EUR then set cnyValue to amount / ((do shell script echo quoted form of rateJSON | sed -n s/.*\EUR\:\\([0-9.]*\\).*/\\1/p) as number) set twdValue to cnyValue * (twdRate as number) set usdValue to cnyValue * (usdRate as number) end if根据不同的原始币种执行相应的换算计算。所有计算都以人民币为基础先将原始金额换算成人民币再分别乘以台币汇率和美元汇率得到最终结果。第七步显示换算结果display dialog 换算结果\n\n原始 selectedText \n\n人民币¥ (round cnyValue) \n台币NT$ (round twdValue) \n美元$ (round usdValue) buttons {OK}使用系统对话框展示换算结果。金额使用round函数四舍五入到整数使显示更加简洁直观。使用方式详解前提条件macOS 系统该脚本仅支持 macOS网络连接用于获取实时汇率操作步骤点击触发按钮- 在网页中点击「在 Apple Script Editor 中打开」按钮Script Editor 打开- 系统会自动打开 Script Editor 并显示脚本内容复制金额文本- 在任意应用或网页中复制包含金额的文本如 “$99.99”、“€49.99”、“¥666” 等运行脚本- 点击 Script Editor 中的运行按钮或按 CmdR查看结果- 脚本会自动读取剪贴板、获取汇率并显示换算结果支持的输入格式这个工具能够智能识别各种常见的金额格式$99.99- 美元格式€49.99 - 欧元格式¥666- 人民币格式$1,234.56- 带千位分隔符的美元Price: $99.99- 包含其他文字的混合文本进阶扩展思路如果你想进一步增强这个工具的功能可以考虑以下方向支持更多币种- 添加日元、韩元、英镑等更多常见货币的支持历史汇率记录- 将每次查询记录保存到文件方便回顾快捷键触发- 配合 Keyboard Maestro 等工具实现全局快捷键启动菜单栏应用- 将脚本封装成菜单栏小工具常驻后台汇率提醒功能- 当汇率达到用户设定的目标时发送通知总结这就是全部代码——不到 60 行不含 TypeScript 包装代码却实现了一个完整的货币换算工具。整个过程无需任何权限申请不需要安装额外软件只需要复制金额、运行脚本两步操作。这个项目展示了几个重要的编程思想最小可用产品——用最少的代码实现核心功能用户友好——支持各种输入格式提供清晰的中文提示容错处理——网络请求失败、解析错误等情况都有对应的处理逻辑。通过 AppleScript 与 Protocol Launcher 的结合我们可以轻松打造出连接 Web 与本地系统的效率工具。如果你对更多类似项目感兴趣欢迎访问 Protocol Launcher 官网 探索更多可能。相关链接Protocol Launcherhttps://protocol-launcher.huayi-data.com/Exchange Rate APIhttps://www.exchangerate-api.com/