Created
August 17, 2019 11:59
-
-
Save Heilum/2ec98991d16e7ce6a63b3207764e618b to your computer and use it in GitHub Desktop.
How to use NSMetadataQuery in command line
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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