You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.16 10:35:29