Finding invisible folders with spotlight by constructing an NSPredicate with NSMetadataQuery [closed]

StackOverflow https://stackoverflow.com/questions/1441466

Question

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]; 

}   
Was it helpful?

Solution

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)?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top