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

如何编写AppleScript将文件复制到Finder当前活动窗口的文件夹?

将文件复制到Finder当前活动窗口的文件夹(AppleScript)

你已经拿到了关键步骤——获取Finder当前窗口的目标文件夹,接下来直接把这个对象作为复制命令的目标即可,不需要额外转换路径字符串,Finder可以直接识别这个对象引用。

基础实现代码

-- 替换为你要复制的文件的实际路径(HFS格式:用冒号分隔)
set sourceFile to file "HDD:Users:YourName:Desktop:example.txt"

tell application "Finder"
    -- 获取当前活动窗口的目标文件夹
    set destinationFolder to target of front window
    -- 执行复制操作
    copy sourceFile to destinationFolder
end tell

进阶:处理无窗口的情况

如果当前没有打开的Finder窗口,上面的脚本会报错,你可以加个判断逻辑避免异常:

set sourceFile to file "HDD:Users:YourName:Desktop:example.txt"

tell application "Finder"
    if exists front window then
        set destinationFolder to target of front window
        copy sourceFile to destinationFolder
    else
        display alert "提示" message "请先打开一个Finder文件夹窗口再执行脚本。"
    end if
end tell

额外实用提示

  • 如果你习惯用POSIX路径(斜杠分隔,比如/Users/YourName/Desktop/example.txt),可以转成Finder能识别的对象:
    set sourcePOSIXPath to "/Users/YourName/Desktop/example.txt"
    set sourceFile to POSIX file sourcePOSIXPath
    
  • 要复制多个文件的话,把sourceFile改成文件列表即可:
    set sourceFiles to {file "HDD:File1.txt", file "HDD:File2.jpg"}
    tell application "Finder"
        copy sourceFiles to target of front window
    end tell
    

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.31 01:45:45