Mac Finder中创建新文件并自动打开的AppleScript问题求助
解决Finder脚本创建文件后无法打开的问题
你的脚本能成功创建文件但打开时报错,核心原因是Finder的open命令不支持POSIX路径——你拼接的currentPath是POSIX格式(如/Users/xxx/Documents/untitled),但Finder只能识别HFS路径(冒号分隔格式,如Macintosh HD:Users:xxx:Documents:untitled)或直接的文件对象。
下面提供两种修复方案:
方案1:直接操作创建的文件对象(推荐)
跳过路径拼接,直接使用make new file返回的文件对象调用open,完全避免路径格式问题:
set file_name to display dialog "File Name" default answer "untitled" with icon note buttons {"Cancel", "Continue"} default button "Continue" set file_name to (text returned of file_name) tell application "Finder" set theWin to window 1 set targetDir to target of theWin as alias -- 创建文件时保存返回的文件对象 set newFile to make new file at targetDir with properties {name:file_name} -- 直接打开该对象 open newFile end tell
方案2:转换POSIX路径为Finder可识别格式
如果需要保留路径相关逻辑,可将POSIX路径转换为Finder支持的文件引用:
set file_name to display dialog "File Name" default answer "untitled" with icon note buttons {"Cancel", "Continue"} default button "Continue" set file_name to (text returned of file_name) tell application "Finder" set theWin to window 1 set currentDir to POSIX path of (target of theWin as alias) set currentPath to currentDir & file_name make new file at (target of the front window) as alias with properties {name:file_name} -- 将POSIX路径转为Finder可识别的引用 open POSIX file currentPath end tell
内容的提问来源于stack exchange,提问作者Alec
相关产品推荐
相关产品推荐

