通过使用 NSMetadataQuery 构造 NSPredicate 来查找具有聚光灯的不可见文件夹

发布于 2024-08-04 15:12:02 字数 1536 浏览 2 评论 0原文

我正在构建一个 NSmetaDataQuery 来查找不可见文件夹(如“.myInvisibleFolder”)。

不幸的是,聚光灯似乎没有找到以“.”开头的文件夹,即使特别包含在谓词中也是如此。

什么有效,什么无效

搜索任何不可见的文件名都有效。

搜索内容有效(kMDItemTextContent)。

没有以“.”开头的文件曾经被发现过。始终返回 0 个结果。

作为测试,在 Finder 中搜索不可见内容是有效的。

我做错了什么?还有其他方法可以找到不可见的文件夹吗?

代码:

- (void)searchForMyInvisableFolders{
    self.query = [[[NSMetadataQuery alloc] init] autorelease];

    // To watch results send by the query, add an observer to the NSNotificationCenter
    NSNotificationCenter *nf = [NSNotificationCenter defaultCenter];
    [nf addObserver:self selector:@selector(queryNote:) name:nil object:self.query];

    // Sort results by file name
    [self.query setSortDescriptors:[NSArray arrayWithObject:[[[NSSortDescriptor alloc] initWithKey:(id)kMDItemFSName ascending:YES] autorelease]]];

    [self.query setDelegate:self];

    //Create a predicate to search for file name
    NSPredicate* predicate = [NSPredicate predicateWithFormat:@" (kMDItemFSName == '.myInvisibleFolder')"];

    //Create a predicate to search for invisible files
    NSPredicate* invisablePredicate = [NSPredicate predicateWithFormat:@"kMDItemFSInvisible == YES"];

    //Compound predicate
    NSPredicate* compoundPredicate = [NSCompoundPredicate andPredicateWithSubpredicates:[NSArray arrayWithObjects:predicate, invisiblePredicate, nil]]; 

    // Set it to the query.
    [self.query setPredicate:compoundPredicate];           

    // Start it.
    [self.query startQuery]; 

}   

I am constructing an NSmetaDataQuery to find invisible folders (Like ".myInvisibleFolder").

Unfortunately, spotlight does not seem to be locating folders beginning with ".", even when specifically included in the predicate.

What works and doesn't work

Searching for any non-invisable filename works.

Searching content works (kMDItemTextContent).

No file beginning with a "." is ever found. Always returns 0 results.

As a test, searching for invisible content within the Finder works.

What Am I doing wrong? Is there another way to find invisible folders?

Code:

- (void)searchForMyInvisableFolders{
    self.query = [[[NSMetadataQuery alloc] init] autorelease];

    // To watch results send by the query, add an observer to the NSNotificationCenter
    NSNotificationCenter *nf = [NSNotificationCenter defaultCenter];
    [nf addObserver:self selector:@selector(queryNote:) name:nil object:self.query];

    // Sort results by file name
    [self.query setSortDescriptors:[NSArray arrayWithObject:[[[NSSortDescriptor alloc] initWithKey:(id)kMDItemFSName ascending:YES] autorelease]]];

    [self.query setDelegate:self];

    //Create a predicate to search for file name
    NSPredicate* predicate = [NSPredicate predicateWithFormat:@" (kMDItemFSName == '.myInvisibleFolder')"];

    //Create a predicate to search for invisible files
    NSPredicate* invisablePredicate = [NSPredicate predicateWithFormat:@"kMDItemFSInvisible == YES"];

    //Compound predicate
    NSPredicate* compoundPredicate = [NSCompoundPredicate andPredicateWithSubpredicates:[NSArray arrayWithObjects:predicate, invisiblePredicate, nil]]; 

    // Set it to the query.
    [self.query setPredicate:compoundPredicate];           

    // Start it.
    [self.query startQuery]; 

}   

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

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

发布评论

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

评论(1

一场信仰旅途 2024-08-11 15:12:02

如果我将第一个谓词更改为:

[NSPredicate predicateWithFormat:@" (kMDItemFSName == '.DS_Store')"];

您的隐形文件夹是否真的名为“.myInvisableFolder”(请注意您的无形文件夹的拼写错误),那么您的代码对我来说非常适合。

Your code works perfectly for me if I change the first predicate to:

[NSPredicate predicateWithFormat:@" (kMDItemFSName == '.DS_Store')"];

Is your invisible folder really called ".myInvisableFolder" (note your misspelling of invisible)?

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