获取Finder的前窗口的POSIX路径 [英] Getting the POSIX path of the Finder's front window

查看:157
本文介绍了获取Finder的前窗口的POSIX路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何获取当前位于其窗口列表顶部的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 ...

解决方案

我正在开发一个商业应用程序,描述和我一直在搞乱不同的方式这样做一年多了。我是一个新手开发者,所以我完全开放的想法,可能有一个更好的方式做,而不是我的方式,但它似乎是稳定和工作。



我使用Apple脚本



我每3秒获取活动文档的路径,但如果活动应用程序是Finder,我得到活动窗口的路径。



使用Carbon获取窗口列表



为了得到所有进程的窗口列表以获得窗口ID(Apple Script不能做的东西),我使用CGWindowListCopyWindowInfo在这个问题中详细说明:



为其他应用程序的窗口获取唯一的ID



这给出了一个数组,其中所有进程的所有窗口排在最前面。所以我需要做的是从数组中删除第一个条目。这也可以用来获得前窗口的屏幕抓图,如果这对你有帮助,如示例应用程序Son中所示,这对我来说是非常宝贵的工作示例。



从Cocoa 发送Apple脚本



对于Apple Script,我已经尝试过Jim并且我的结论是,每个都有它的问题,在稳定性和灵活性方面。


  1. Apple Event Manager依赖于
    发送原始Apple事件。


  2. 使用NSAppleScript我发现是




    慢和容易出错。


  3. 当阅读脚本桥,
    我非常兴奋,只有尝试它
    出来,非常失望。对于窗口的
    属性,我是
    试图抢,它甚至没有
    认为他们是现有的。它似乎
    我是越野车和奇怪。我希望
    如果你发送的命令是
    支持SB,它将是最好的
    选项。


第四个选项



我现在依靠一个围绕Apple Script的神话般的Objective C封装href =http://appscript.sourceforge.net =nofollow noreferrer> AppScript 。它已经存在了许多,多年,它的稳定,开发人员是伟大的,最重要的是它的工作。



它有坚如磐石的方法,允许你发送Apple脚本,它甚至会整齐地返回错误,没有一个负载的潜在的buggy和凌乱的代码需要检查为他们。



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


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...

解决方案

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.

这篇关于获取Finder的前窗口的POSIX路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆