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

如何用AppleScript正确关闭指定已打开的文件窗口

错误原因

原脚本无法生效的核心问题有两个:

  • quit是AppleScript中用于直接退出应用的指令,不支持接收文件路径作为参数,执行后只会直接退出Finder,和关闭指定文件的需求无关
  • .rtf格式文件默认由「文本编辑(TextEdit)」应用打开,打开后的文档窗口归属TextEdit管理,不属于Finder窗口范畴,向Finder发送操作指令自然无法生效
正确实现方案

关闭TextEdit中打开的指定rtf文件

匹配目标文件的绝对路径执行关闭操作,代码如下:

set targetFile to POSIX file "/Users/xyz/Desktop/foo.rtf" as alias
tell application "TextEdit"
	close (every document whose path is (targetFile as text)) saving no
end tell

注:脚本中saving no表示关闭文件时不保存未提交的修改,如果需要关闭前自动保存改动,将参数改为saving yes即可。

关闭Finder中打开的指定目录窗口

如果需求是关闭Finder中打开的对应文件夹窗口,可使用以下脚本:

set targetFolder to POSIX file "/Users/xyz/Desktop/目标文件夹路径" as alias
tell application "Finder"
	close (every Finder window whose target is targetFolder)
end tell

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.30 09:15:40