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

Mac OS下如何通过编程在Finder中实现多文件选中?

解决Mac上编程实现Finder多选文件的问题

我之前也踩过open -R的坑——它确实只能定位并选中最后一个传入的文件,没法实现你要的Ctrl点击多选效果。这里给你两个实用的解决方案:

方案1:用AppleScript(最直接,系统原生支持)

AppleScript是控制Finder的原生方式,能轻松实现多选。你可以直接写脚本执行,或者通过命令行调用:

命令行直接执行

把下面的命令里的文件路径换成你自己的,用osascript运行:

osascript -e 'tell application "Finder" to activate' \
          -e 'tell application "Finder" to select {POSIX file "/Users/yourname/Documents/file1.pdf", POSIX file "/Users/yourname/Downloads/file2.png"}'
  • activate是确保Finder处于前台
  • POSIX file用来把Unix风格的路径转换成Finder能识别的格式

写成独立脚本文件

如果需要重复使用,可以把脚本存成select_files.scpt:

tell application "Finder"
    activate
    -- 替换成你的文件路径列表
    set targetFiles to {POSIX file "/path/to/file1", POSIX file "/path/to/file2"}
    select targetFiles
end tell

然后用命令运行:

osascript select_files.scpt

方案2:用Python + PyObjC(适合Python项目集成)

如果你的项目是用Python写的,可以通过PyObjC调用系统的Cocoa API来实现:

首先安装依赖:

pip install pyobjc

然后写代码:

from AppKit import NSWorkspace, NSURL
import os

def select_in_finder(file_paths):
    # 确保路径是绝对路径
    absolute_paths = [os.path.abspath(path) for path in file_paths]
    workspace = NSWorkspace.sharedWorkspace()
    # 把路径转换成NSURL对象
    file_urls = [NSURL.fileURLWithPath_(path) for path in absolute_paths]
    # 让Finder选中这些文件
    workspace.selectURLs_inFileViewerRootedAtURL_(file_urls, None)

# 调用示例
select_in_finder(["/Users/yourname/Desktop/note.txt", "/Users/yourname/Pictures/photo.jpg"])

注意事项

  • 所有文件路径必须是绝对路径,相对路径可能会导致Finder找不到文件
  • 如果传入的文件不存在,Finder会忽略该文件,AppleScript版本可能会弹出错误提示
  • 两种方案都会自动激活Finder并将选中的文件所在文件夹前置显示

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.15 07:43:29