AppleScript如何接收文件名参数并正确打开指定文件
故障原因
脚本返回missing value核心是两个问题:
- 通过
osascript传入的相对路径不会被AppleScript自动解析为当前工作目录的绝对路径,直接读取字符串路径时系统找不到目标文件 - AppleScript的
open指令无法直接识别纯字符串格式的路径,需要转换为系统可识别的文件引用对象
可直接运行的修正脚本
on run argv set inputPath to item 1 of argv -- 相对路径转绝对路径 if inputPath does not start with "/" then set workDir to do shell script "pwd" set inputPath to workDir & "/" & inputPath end if -- 转换为AppleScript可识别的文件引用 set targetFile to POSIX file inputPath as alias tell application "MindNode" activate open targetFile end tell end run
使用说明
- 执行命令和原有写法完全兼容:
osascript test.scpt dysfunctions.mindnode,支持传入相对路径、绝对路径两种格式 - 脚本里的
activate作用是启动MindNode后自动切到前台,不需要可以直接删除 - 如果首次运行弹出权限报错,打开「系统设置>隐私与安全性>自动化」,给执行命令用的终端程序开启MindNode的访问权限即可。
内容的提问来源于stack exchange,提问作者Dr Rama Lakshmanan
相关产品推荐
相关产品推荐

