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

如何用Bash/AppleScript将文件复制到Finder剪贴板?

实现将文件复制到Finder剪贴板的脚本方案

AppleScript 实现方式

AppleScript可直接操作Finder的剪贴板对象,实现和Command+C完全一致的效果:

复制指定单个文件

# 替换为目标文件的实际路径
set targetFile to POSIX file "/Users/yourname/Documents/test.jpg" as alias
set the clipboard to targetFile

复制指定多个文件

# 替换为需要复制的多个文件路径
set fileList to {POSIX file "/Users/yourname/Documents/file1.png", POSIX file "/Users/yourname/Documents/file2.mp4"} as list of aliases
set the clipboard to fileList

复制Finder当前选中的文件

如果要复制Finder里用户已选中的文件,用这段脚本:

tell application "Finder"
    set selectedFiles to selection
    if selectedFiles is not {} then
        set the clipboard to selectedFiles
    end if
end tell

Bash 实现方式

Bash本身无法直接操作Finder的文件剪贴板,但可以通过osascript命令调用AppleScript来实现:

复制单个文件到剪贴板

osascript -e 'set the clipboard to POSIX file "/Users/yourname/Documents/test.jpg" as alias'

复制多个文件到剪贴板

可以写一个Bash脚本,批量处理传入的文件路径:

#!/bin/bash

# 使用方式:./copy-to-clipboard.sh 文件1路径 文件2路径 ...
if [ $# -eq 0 ]; then
    echo "请传入要复制的文件路径"
    exit 1
fi

# 处理路径中的特殊字符,避免AppleScript解析错误
escaped_paths=$(printf '"%s",' "$@" | sed 's/,$//')

osascript <<EOF
set fileList to {}
repeat with filePath in {$escaped_paths}
    set end of fileList to POSIX file filePath as alias
end repeat
set the clipboard to fileList
EOF

原理说明

Finder的剪贴板存储的是文件别名对象,而非单纯的文本路径。只有让剪贴板持有这些对象,其他应用(比如Finder、邮件、即时通讯工具)才能识别为可粘贴的文件,这也是手动按Command+C能实现跨场景粘贴的核心原因。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.19 10:43:15