Skip to content

Instantly share code, notes, and snippets.

@markohlebar
Created October 20, 2016 17:59
Show Gist options
  • Save markohlebar/eea6bb9894bcb797198d34c1854ca43b to your computer and use it in GitHub Desktop.
Save markohlebar/eea6bb9894bcb797198d34c1854ca43b to your computer and use it in GitHub Desktop.
func perform(query: String) {
NotificationCenter.default.addObserver(self,
selector: #selector(didUpdate(with:)),
name: Notification.Name.NSMetadataQueryDidUpdate,
object: nil)
NotificationCenter.default.addObserver(self,
selector: #selector(didFinish(with:)),
name: Notification.Name.NSMetadataQueryDidFinishGathering,
object: nil)
metadataQuery = NSMetadataQuery()
metadataQuery.predicate = NSPredicate(format: "kMDItemTextContent like[c] %@", query)
//NSHomeDirectory() on sandboxed apps is the sandbox itself
//http://stackoverflow.com/questions/9553390/how-do-i-get-the-users-home-directory-in-objective-c-in-a-sandboxed-app
metadataQuery.searchScopes = ["/Users/mhlebar"]
metadataQuery.start()
}
func didUpdate(with notification: NSNotification) {
}
func didFinish(with notification: NSNotification) {
for item in metadataQuery.results as! [NSMetadataItem] {
print(item.value(forAttribute:NSMetadataItemPathKey)!)
}
metadataQuery.stop()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment