启动 Finder 窗口并选择特定文件

发布于 2024-12-08 07:24:36 字数 191 浏览 0 评论 0原文

我正在尝试以编程方式从 Xcode 项目启动 OS X Finder 窗口。我需要打开一个特定文件夹的窗口,并自动选择该文件夹中的特定文件。

这类似于 Xcode 和相关应用程序中使用的“在 Finder 中显示”功能。

有谁知道如何在 Objective-C、Swift、AppleScript 或 Finder 命令行参数中执行此操作?

I'm trying to programmatically launch an OS X Finder window from an Xcode project. I need the window to open to a specific folder and have specific files within that folder automatically selected.

This is similar to the "Show in Finder" functionality used in Xcode and related apps.

Does anyone know how to do this in either Objective-C, Swift, AppleScript, or Finder command-line parameters?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

假扮的天使 2024-12-15 07:24:37

Objective-C版本:

NSArray *fileURLs = [NSArray arrayWithObjects:fileURL1, /* ... */ nil];
[[NSWorkspace sharedWorkspace] activateFileViewerSelectingURLs:fileURLs];

Objective-C version:

NSArray *fileURLs = [NSArray arrayWithObjects:fileURL1, /* ... */ nil];
[[NSWorkspace sharedWorkspace] activateFileViewerSelectingURLs:fileURLs];
2024-12-15 07:24:37
$ open -R <path-to-reveal>
$ open -R <path-to-reveal>
吲‖鸣 2024-12-15 07:24:37

另一种 AppleScript 风格 - Finder 的显示命令将打开包含文件夹的窗口并选择项目。如果有多个包含文件夹,则会打开多个 Finder 窗口。

tell application "Finder" 
   to reveal {someAlias, "path/to/POSIXfile" as POSIX file, etc}

Another AppleScript flavor - the Finder's reveal command will both open a window to the containing folder and select the item(s). If there are multiple containing folders, multiple Finder windows will be opened.

tell application "Finder" 
   to reveal {someAlias, "path/to/POSIXfile" as POSIX file, etc}
时光无声 2024-12-15 07:24:37

斯威夫特版本:

let paths = ["/Users/peter/foo/bar.json"]
let fileURLs = paths.map{ NSURL(fileURLWithPath: $0)}
NSWorkspace.sharedWorkspace().activateFileViewerSelectingURLs(fileURLs)

Swift version:

let paths = ["/Users/peter/foo/bar.json"]
let fileURLs = paths.map{ NSURL(fileURLWithPath: $0)}
NSWorkspace.sharedWorkspace().activateFileViewerSelectingURLs(fileURLs)
负佳期 2024-12-15 07:24:37

在 Finder 中显示多个文件

open -R 仅适用于单个文件。
我们可以使用 Apple Script 来代替。

从 user866649 的 回答 中,我们可以将其移植到 shell 脚本,如下所示:

osascript -e 'tell application "Finder" to reveal {"path/to/file1" as POSIX file, "path/to/file2" as POSIX file} activate'

刚刚创建了一个实用程序脚本:

finder .sh

#!/usr/bin/env bash

join() {
  local d=$1 s=$2
  shift 2 && printf %s "$s${@/#/$d}"
}

lst=()
for f in "$@"; do
  lst+=("\"$f\" as POSIX file")
done
files=$(join , "${lst[@]}")

osascript -e "tell application \"Finder\" to reveal {$files} activate"

然后尝试一下:

chmod +x finder.sh
./finder.sh ~/Downloads ~/Desktop

它应该打开 Finder 并选择“下载”和“桌面”文件夹。

Reveal multiple files in Finder

As open -R <path-to-reveal> only works for single file.
We can use Apple Script instead.

From user866649's answer, we can port it to a shell script as the following:

osascript -e 'tell application "Finder" to reveal {"path/to/file1" as POSIX file, "path/to/file2" as POSIX file} activate'

Just created a utility script:

finder.sh

#!/usr/bin/env bash

join() {
  local d=$1 s=$2
  shift 2 && printf %s "$s${@/#/$d}"
}

lst=()
for f in "$@"; do
  lst+=("\"$f\" as POSIX file")
done
files=$(join , "${lst[@]}")

osascript -e "tell application \"Finder\" to reveal {$files} activate"

Then try it:

chmod +x finder.sh
./finder.sh ~/Downloads ~/Desktop

It should open Finder and selects both Downloads and Desktop folder.

乖乖兔^ω^ 2024-12-15 07:24:37

我发现 activateFileViewerSelectingURLs 在 Yosemite 上不起作用(至少在与 Finder 不同的空间中时)。它会导致切换到 Finder 空间,但似乎不会选择 URL。使用:

- (BOOL)selectFile:(NSString *)fullPath inFileViewerRootedAtPath:(NSString *)rootFullPath

将从全屏应用程序切换空间并选择路径。

I'm finding that activateFileViewerSelectingURLs is not working on Yosemite (at least when in separate space from Finder). It will cause a switch to the Finder's space but won't seem to select the URL. Using:

- (BOOL)selectFile:(NSString *)fullPath inFileViewerRootedAtPath:(NSString *)rootFullPath

will switch spaces from full screen app and select path.

世俗缘 2024-12-15 07:24:37

path 打开文件时:

NSString* path = @"/Users/user/Downloads/my file"
NSArray *fileURLs = [NSArray arrayWithObjects:[NSURL fileURLWithPath:path], nil];
[[NSWorkspace sharedWorkspace] activateFileViewerSelectingURLs:fileURLs];

When opening a file at path:

NSString* path = @"/Users/user/Downloads/my file"
NSArray *fileURLs = [NSArray arrayWithObjects:[NSURL fileURLWithPath:path], nil];
[[NSWorkspace sharedWorkspace] activateFileViewerSelectingURLs:fileURLs];
游魂 2024-12-15 07:24:37

斯威夫特 3.2/4.0 版本:

NSWorkspace.shared.activateFileViewerSelecting([outputFileURL])

Swift 3.2/4.0 Version:

NSWorkspace.shared.activateFileViewerSelecting([outputFileURL])
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文