如何编写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
相关产品推荐
相关产品推荐

