如何使用 PyObjC 绑定和 NSMetadataQuery 编写与 mdfind 等效的 python 脚本?

发布于 2024-09-28 12:47:36 字数 225 浏览 0 评论 0原文

我想写一个相当于 mdfind 的 python 程序。我想使用 .Spotlight-V100 元数据,但找不到所使用的元数据数据库格式的描述,但 NSMetadataQuery 似乎正是我所需要的。我想使用内置的 Obj-C 绑定在 python 中执行此操作,但无法找出正确的咒语以使其工作。不确定问题是否在于调用的异步性质,或者我只是错误地将事物连接在一起。

一个相当于“mdfind”的简单例子就可以作为开始。

I want to write the python equivalent of mdfind. I want to use the .Spotlight-V100 metadata and I cannot find a description for the metadata db format used, but NSMetadataQuery seems to be what I need. I'd like to do this in python using the built in Obj-C bindings, but have not been able to figure out the correct incantation to get it to work. Not sure if the problem is the asynchronous nature of the call or I'm just wiring things together incorrectly.

A simple example giving the equivalent of of "mdfind " would be fine for a start.

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

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

发布评论

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

评论(1

变身佩奇 2024-10-05 12:47:36

我得到了一个非常简单的版本。我的谓词不太正确,因为等效的 mdfind 调用具有其他结果。此外,它需要两个参数,第一个是要使用的基本路径名,第二个是搜索词。

这是代码:

from Cocoa import *

import sys

query = NSMetadataQuery.alloc().init()
query.setPredicate_(NSPredicate.predicateWithFormat_("(kMDItemTextContent = \"" + sys.argv[2] + "\")"))
query.setSearchScopes_(NSArray.arrayWithObject_(sys.argv[1]))
query.startQuery()
NSRunLoop.currentRunLoop().runUntilDate_(NSDate.dateWithTimeIntervalSinceNow_(5))
query.stopQuery()
print "count: ", len(query.results())
for item in query.results():
    print "item: ", item.valueForAttribute_("kMDItemPath")

查询调用是异步的,因此为了更完整,我应该注册一个回调并让运行循环连续运行。事实上,我搜索了 5 秒,所以如果我们有一个需要更长时间的查询,我们将只得到部分结果。

I got a very simple version working. I don't quite have the predicate correct, as the equivalent mdfind call has additional results. Also, it requires two args, the first is the base pathname to work from with the second being the search term.

Here is the code:

from Cocoa import *

import sys

query = NSMetadataQuery.alloc().init()
query.setPredicate_(NSPredicate.predicateWithFormat_("(kMDItemTextContent = \"" + sys.argv[2] + "\")"))
query.setSearchScopes_(NSArray.arrayWithObject_(sys.argv[1]))
query.startQuery()
NSRunLoop.currentRunLoop().runUntilDate_(NSDate.dateWithTimeIntervalSinceNow_(5))
query.stopQuery()
print "count: ", len(query.results())
for item in query.results():
    print "item: ", item.valueForAttribute_("kMDItemPath")

The query call is asynchronous, so to be more complete, I should register a callback and have the run loop go continuously. As it is, I do a search for 5 seconds, so if we have a query that would take longer, we will get only partial results.

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