获取 Finder 前窗口的 POSIX 路径

发布于 2024-07-26 07:59:25 字数 608 浏览 4 评论 0原文

如何获取当前位于窗口列表顶部的 Finder 窗口的 POSIX 路径? 最好使用某种 Cocoa 框架,但我对任何事情都持开放态度。

背景: 我需要这个,因为我想选择与正则表达式模式匹配的文件,从该路径递归开始。 这个想法是使用

[[NSWorkspace sharedWorkspace] subpathsAtPath:thePath]

方法来获取此路径的所有后代,在 NSTask 中使用“grep”(以避免打包正则表达式支持框架)并

[[NSWorkspace sharedWorkspace] selectFile:aFile inFileViewerRootedAtPath:thePath]

在循环中使用由 grep 返回的条目组成的 NSArray。

到目前为止,我已经查看了 NSWorkspace、NSFileManager 和 NSApplication 以及 Xcode 文档中的其他关键字搜索。

感谢您查看我的问题!

Andre

PS:我不确定 grep 部分,也许我只是将 RegexKit Lite 放在那里......

How can I get the POSIX path of the Finder window that is currently at the top of its window list?
Preferably with some kind of Cocoa framework but I am open for anything.

Background:
I would need this because I want to make a selection of files matching a regex pattern, starting recursively from this path. The idea is to use

[[NSWorkspace sharedWorkspace] subpathsAtPath:thePath]

method to get all the descendants of this path, use "grep" in an NSTask (to avoid packaging a regex support framework) and use

[[NSWorkspace sharedWorkspace] selectFile:aFile inFileViewerRootedAtPath:thePath]

in a loop looping through an NSArray made from the entries returned by grep.

So far, I have looked at NSWorkspace, NSFileManager and NSApplication plus other keyword searches within the Xcode Documentation.

Thanks for checking out my question!

Andre

PS: I am not sure about the grep part, maybe I'll just slap RegexKit Lite in there...

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

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

发布评论

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

评论(4

自此以后,行同陌路 2024-08-02 07:59:26

我正在开发一个商业应用程序,它完全按照您所描述的方式进行,一年多来我一直在尝试不同的方法。 我是一名新手开发人员,所以我完全接受这样的想法:可能有比我的方式更好的方法,但它似乎很稳定并且有效。

我使用 Apple 脚本

我每 3 秒获取一次活动文档的路径,但如果活动应用程序是 Finder,我会获取活动窗口的路径。 我按照接受的答案所述使用 Apple 脚本来执行此操作。

使用 Carbon 获取窗口列表

要获取所有进程的窗口列表以获取窗口 ID(Apple 脚本无法执行此操作),我使用 CGWindowListCopyWindowInfo,如本问题中所述:

获取另一个应用程序窗口的唯一 ID

这向我展示了一个包含所有进程的所有窗口的数组,按最前面的顺序排列。 所以我需要做的就是从数组中取出第一个条目。 如果这对您有帮助,这也可以用于获取前窗的屏幕截图,如 Son of Grab 示例应用程序所示,它作为一个工作示例对我来说非常宝贵。

从 Cocoa 发送 Apple 脚本

当谈到 Apple 脚本时,我尝试了 Jim 建议的所有 3 个脚本,我的结论是,每个脚本都有其问题,无论是在稳定性还是灵活性方面。

  1. Apple 活动管理器依赖于您
    发送原始 Apple 事件。 为我,
    这太像努力工作了
    级别太低。

  2. 使用 NSAppleScript 我发现
    缓慢且容易出错。 这也是
    当你想发送一个时非常笨拙
    各种 Apple 脚本
    像我一样发消息。

  3. 在阅读有关 Scripting Bridge 的内容时,
    我很兴奋,只想尝试一下
    出来并感到非常失望。 为了
    我的windows的属性
    试图抓住,但它甚至没有
    承认它们是存在的。 看起来
    对我来说是越野车和奇怪的。 我预计
    如果您发送的命令是
    有SB支持,那就最好了
    选项。

第四种选择

我现在依赖于 Apple Script 的一个很棒的 Objective C 包装器,名为 AppScript 。 它已经存在很多很多年了,它很稳定,开发人员很棒,最重要的是它可以工作。

它具有坚如磐石的方法,允许您发送 Apple 脚本,它甚至会整齐地为您返回错误,而无需检查大量潜在错误和混乱的代码。

我已经使用它一年了,没有任何问题。 如果您对如何执行此操作还有任何其他疑问,请随时发表评论,我会尽力回答。

I'm developing an commercial application that does exactly what you describe and I've been messing with different ways of doing this for over a year now. I'm a newbie developer, so I'm totally open to the idea that there may be a much better way of doing it than my way, but it seems to be stable and work.

I use Apple Script

I get the path of the active document every 3 seconds, but if the active application is the Finder, I get the path of the active window. I do this using Apple Script as described by the accepted answer.

Getting Window List using Carbon

To get the window list of all processes to get the window ID (something Apple Script can't do), I use CGWindowListCopyWindowInfo as detailed in this question:

Getting a unique ID for a window of another application

This presents me an array with all the windows of all processes ordered by which is frontmost. So all I need to do is pluck the first entry from the array. This can also be used to get a screengrab of the front window, if that's helpful to you, as shown in the Son of Grab sample application, which has been invaluable to me as a working example.

Sending Apple Script from Cocoa

When it comes to Apple Script, I've experimented with all 3 that Jim suggests and my conclusion is that each has it's issues, both in terms of stability and flexibility.

  1. Apple Event Manager relies on you
    sending raw Apple Events. For me,
    this was too much like hard work and
    too low level.

  2. Using NSAppleScript I found to be
    slow and error prone. It's also
    pretty clumsy when you want to send a
    whole variety of Apple Script
    messages as I do.

  3. When reading about Scripting Bridge,
    I got very excited, only to try it
    out and be very disappointed. For the
    attributes of the windows I was
    trying to grab, it didn't even
    recognise them as existing. It seemed
    to me to be buggy and weird. I expect
    if the commands you're sending are
    supported by SB, it would be the best
    option.

A Fourth Option

I now rely on a fabulous Objective C wrapper around Apple Script called AppScript. It's been around for many, many years, it's stable, the developer is great and most of all it just works.

It has rock solid methods which allow you to send Apple Script and it'll even return errors for you neatly without a whole load of potentially buggy and messy code needing to check for them.

I've been using it for a year now with no problems. If you have any other questions about how to do this, please don't hesitate to comment and I'll try my best to answer them.

黯淡〆 2024-08-02 07:59:26

获取由 Finder 中最前面的窗口表示的 POSIX 路径将涉及以一种或另一种方式将 Apple 事件发送到 Finder。

您的选择包括:

  • 直接使用 Apple Event Manager(或 NSAppleEventDescriptor 等)。
  • 使用 NSAppleScript 或 OSAScript 对象。
  • 使用 ScriptingBridge。

如果您正在构建 Cocoa 应用程序,OSAScript 可能是最自然的选择。

Getting the POSIX path represented by the frontmost window in the Finder is going to involve sending Apple Events to the Finder in one way or another.

Your choices include:

  • Using the Apple Event Manager (or NSAppleEventDescriptor and friends) directly.
  • Using NSAppleScript or OSAScript objects.
  • Using ScriptingBridge.

If you are building a Cocoa app, OSAScript is probably the most natural choice.

桃扇骨 2024-08-02 07:59:26

可以使用 ScriptingBridge< 获取 Finder 前窗口的 POSIX 路径/a> 与斯威夫特

import Foundation
import ScriptingBridge // imports: ScriptingBridge.SBApplication, .SBElementArray, .SBObject

// SBApplication? to SBApplication to FinderApplication
let finder = (SBApplication(bundleIdentifier: "com.apple.finder")!) as FinderApplication
    
let windowList: SBElementArray = finder.windows!()
guard windowList.count > 0,
    let window = windowList[0] as? FinderWindow,
    let windowProperties = window.properties,
    let windowTarget = windowProperties["target"] as? FinderFolder,
    let windowUrlOptionalStr = windowTarget.URL,
    let windowUrlSubStr = windowUrlOptionalStr
        .removingPercentEncoding?
        .dropFirst(7) // "file://" 7 characters
else {
    // … handle failure
}

let windowUrl = URL(
    fileURLWithPath: String(windowUrlSubStr),
    isDirectory: true,
    relativeTo: nil)

It is possible get the POSIX path of the Finder's front window by using the ScriptingBridge with Swift

import Foundation
import ScriptingBridge // imports: ScriptingBridge.SBApplication, .SBElementArray, .SBObject

// SBApplication? to SBApplication to FinderApplication
let finder = (SBApplication(bundleIdentifier: "com.apple.finder")!) as FinderApplication
    
let windowList: SBElementArray = finder.windows!()
guard windowList.count > 0,
    let window = windowList[0] as? FinderWindow,
    let windowProperties = window.properties,
    let windowTarget = windowProperties["target"] as? FinderFolder,
    let windowUrlOptionalStr = windowTarget.URL,
    let windowUrlSubStr = windowUrlOptionalStr
        .removingPercentEncoding?
        .dropFirst(7) // "file://" 7 characters
else {
    // … handle failure
}

let windowUrl = URL(
    fileURLWithPath: String(windowUrlSubStr),
    isDirectory: true,
    relativeTo: nil)
极度宠爱 2024-08-02 07:59:25

您可以通过 AppleScript 向 Finder 询问这一点。

这*一句话对我有用:

osascript -e 'tell application "Finder" to set myname to POSIX path of (target of window 1 as alias)'

*这个的修改版本。

You can probably ask the Finder this via an AppleScript.

This* one-liner works for me:

osascript -e 'tell application "Finder" to set myname to POSIX path of (target of window 1 as alias)'

*a modified version of this.

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