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

AppleScript报错‘Finder got an error: Can’t make class file’如何解决?

解决AppleScript创建文本文件的「Can’t make class file」错误

错误原因

你的代码里的file type:text是过时的Mac OS 9时代属性,现代macOS已不再使用这种四字符文件类型代码,Finder无法识别该属性,导致创建文件失败。另外缺少文件名后缀、未处理Finder无前置窗口的情况也可能引发问题。

修正后的代码(Finder版本)

tell application "Finder"
    -- 检查是否有前置Finder窗口,无则用桌面作为目标路径
    set targetFolder to if exists front window then target of front window else desktop
    
    set filenameResult to display dialog "Enter Filename" default answer "newfile.txt"
    set newFilename to text returned of filenameResult
    
    -- 移除无效的file type属性,直接创建文件
    make new file at targetFolder with properties {name:newFilename}
end tell

更可靠的System Events版本(推荐)

如果需要确保创建的是标准纯文本文件,用System Events指定文件类型UTI更稳定:

tell application "System Events"
    set filenameResult to display dialog "Enter Filename" default answer "newfile.txt"
    set newFilename to text returned of filenameResult
    
    -- 获取目标文件夹
    tell application "Finder"
        set targetFolder to if exists front window then target of front window else desktop
    end tell
    
    -- 指定纯文本文件的UTI:public.plain-text
    make new file at targetFolder with properties {name:newFilename, type identifier:"public.plain-text"}
end tell

关键调整点

  • 移除了无效的file type:text属性,这是报错的核心原因。
  • 添加了Finder窗口检查,避免无窗口时脚本崩溃。
  • 默认文件名加上.txt后缀,确保系统识别为文本文件。
  • System Events版本通过type identifier明确指定文件类型,兼容性更好。

内容的提问来源于stack exchange,提问作者AgentStrawberry

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.19 16:51:36