通过open命令向打包为.app的AppleScript传递参数报错如何解决
问题根源
你遇到的传参异常是因为:AppleScript 打包为 .app 应用后,open --args 传入的参数不会直接传递给 run 处理函数的 argv 参数,只有直接执行 .scpt 脚本时 argv 才会生效。你之前直接读取 argv 实际拿到的是应用当前上下文对象,所以输出为 current application。
修复方案
你需要引入 Foundation 框架,通过系统进程接口读取启动参数,完整脚本如下:
use framework "Foundation" on run -- 读取所有启动参数 set allArgs to (current application's NSProcessInfo's processInfo()'s arguments) as list -- 前两个参数是系统自带的(第一个是app本体路径,第二个是进程标识参数),从第三个开始是自定义传入的参数 if (count of allArgs) ≥ 3 then set inputArg to item 3 of allArgs do shell script "echo " & quoted form of inputArg & " >> ~/Desktop/test.txt" end if end run
验证方法
- 将上述脚本保存为
.app格式,保存时无需勾选「运行处理程序后保持打开」选项 - 调用命令保持不变:
open -a test.app --args arg1 - 检查
~/Desktop/test.txt即可看到正确写入的arg1参数
额外说明
如果你需要搭配 with administrator privileges 使用,直接在 do shell script 后追加参数即可,参数读取逻辑不需要改动:
do shell script "echo " & quoted form of inputArg & " >> ~/Desktop/test.txt" with administrator privileges
该方案完全兼容 Big Sur 和 Monterey 系统,也支持你自定义应用图标、权限提示文本的需求。
内容的提问来源于stack exchange,提问作者Nyque
相关产品推荐
相关产品推荐

