Skip to content

Instantly share code, notes, and snippets.

@Heilum
Created August 17, 2019 11:59
Show Gist options
  • Save Heilum/2ec98991d16e7ce6a63b3207764e618b to your computer and use it in GitHub Desktop.
Save Heilum/2ec98991d16e7ce6a63b3207764e618b to your computer and use it in GitHub Desktop.
How to use NSMetadataQuery in command line
let query = NSMetadataQuery();
query.searchScopes = ["/work/books"]
query.predicate = NSPredicate(format: "kMDItemFSName like %@", "NSHipster - Cocoa & Swift.pdf")
NotificationCenter.default.addObserver(forName: NSNotification.Name.NSMetadataQueryDidFinishGathering, object: query, queue: nil) { (_) in
print("count = \(query.resultCount)")
if let items = query.results as? [NSMetadataItem]{
for item in items{
if let path = item.value(forKey: NSMetadataItemPathKey) as? String{
print(path)
}
}
}
}
query.start();
RunLoop.current.run();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment