Finding files using NSMetadataQuery just does nothing

I am trying to use NSMetadataQuery and NSPredicate to search for files. After hours of trying and finding solutions (I'm new to fast) I have a small example. It compiles fine, but the results are zero. I tried different predicates, but at the end metadataQuery.resultCount is always 0. Does anyone have an idea what is going wrong?

class AppDelegate: NSObject, NSApplicationDelegate { var metadataQuery: NSMetadataQuery! var metadataQueryDidUpdateObserver: AnyObject? var metadataQueryDidFinishGatheringObserver: AnyObject? @IBOutlet weak var window: NSWindow! func applicationDidFinishLaunching(aNotification: NSNotification) { NSNotificationCenter.defaultCenter().addObserver(self, selector: "initalGatherComplete:", name: NSMetadataQueryDidFinishGatheringNotification, object: nil) metadataQuery = NSMetadataQuery() metadataQuery.searchScopes = [NSMetadataQueryIndexedLocalComputerScope] metadataQuery.predicate = NSPredicate(format: "%K LIKE '*'", NSMetadataItemFSNameKey) metadataQuery.startQuery() } func initalGatherComplete(notification: NSNotification) { metadataQuery.stopQuery() let resultCounter = metadataQuery.resultCount NSLog("%lu", resultCounter) NSNotificationCenter.defaultCenter().removeObserver(self, name: NSMetadataQueryDidFinishGatheringNotification, object: nil) } 

And last but not least, the current predicate should display all the files, but at the end the predicate should display only the list of applications. What is the best practice for creating such a predicate? I planned to filter the .app extension, but maybe there is a better way?

Thanks!

+5
source share
1 answer

Check the syntax of predicates . The LIKE statement is not available for NSMetadataQuery predicate NSMetadataQuery (in fact, NSPredicate uses a fairly similar, but actually different set of statements and behavior in the context of Spotlight metadata searches.)

NSMetadataQuery The glob search syntax simply uses an equal sign:

 NSPredicate(format: "%K ==[cd] '*'", NSMetadataItemFSNameKey) 
+4
source

Source: https://habr.com/ru/post/1212373/


All Articles