如何通过Spotlight文件元数据属性 / nsmetadataquery过滤文件URL的NSARRAY



NSMetadataQuery类似乎是Finder/Spotlight通过其元数据搜索文件的方式。

基础框架提供的

nsmetadataquery类。查询可以以两种模式运行:异步和带有实时更新的异步。第一个简单地对初始搜索时存在的文件进行搜索。后者继续搜索。将数据更新为满足或不再满足搜索参数的文件。

https://developer.apple.com/library/content/documentation/carbon/conepperual/spotlightquery/spotlightquery/concepts/introduction.htmll#/apple_redoc/doc/doc/doc/uid/tp40001843-bbbcfbbcag 是,它似乎围绕提供目录(searchScopes),然后异步返回这些目录中发现的结果(NSMetadataQueryDidFinishGathering)。

我已经有一个包含文件URL的NSARRAY。我想使用相同的元数据和查询语法构造这些NSURL的过滤器/搜索这些NSURL作为聚光灯搜索。但是我将提供快速文件列表的列表,而不是提供一个目录并接收异步结果。

// Something like this...
let imageFileTypePredicate = NSPredicate(fromMetadataQueryString: "(kMDItemGroupId = 13)")
let imageURLs = allURLs.filter{ imageFileTypePredicate.evaluate(with:$0) };

但是,这是使用标准的NSPredicate搜索而不是文件元数据过滤器,并且正在抛出错误:

此类不是密钥_kmditemgroupid的键值编码。

我感兴趣的过滤的聚光灯元数据属性在此处列出:
https://develveper.apple.com/library/content/documentation/coreservices/reference/reference/metadataattributesref/reference/commonattrs.httrs.httrs.html#/apple_reff/doc/doc/doc/doc/uid/tp40001694-sw1

如何通过Spotlight Metadata过滤文件URL?

为每个URL创建一个MDITEM,以获取文件的焦点属性。

mditem是一个符合CF的对象,代表文件和与文件关联的元数据。

https://developer.apple.com/reference/coreservices/1658213-mditem

MDItemRef item = MDItemCreateWithURL(kCFAllocatorDefault, url);
CFArrayRef attributes = MDItemCopyAttributeNames(item);
NSDictionary *attributeValues = CFBridgingRelease(MDItemCopyAttributes(item, attributes));

最新更新