Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
How to get list of all installed applications on MacOS with Swift 5
var query: NSMetadataQuery? {
willSet {
if let query = self.query {
query.stop()
}
}
}
public func doSpotlightQuery() {
query = NSMetadataQuery()
let predicate = NSPredicate(format: "kMDItemContentType == 'com.apple.application-bundle'")
NotificationCenter.default.addObserver(self, selector: #selector(queryDidFinish(_:)), name: NSNotification.Name.NSMetadataQueryDidFinishGathering, object: nil)
query?.predicate = predicate
query?.start()
}
@objc public func queryDidFinish(_ notification: NSNotification) {
guard let query = notification.object as? NSMetadataQuery else {
return
}
for result in query.results {
guard let item = result as? NSMetadataItem else {
print("Result was not an NSMetadataItem, \(result)")
continue
}
print(item.value(forAttribute: kMDItemDisplayName as String))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment