Skip to content

Instantly share code, notes, and snippets.

@xbladesub
Last active July 16, 2021 13:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xbladesub/02619609c4e23f2820b545120103e5aa to your computer and use it in GitHub Desktop.
Save xbladesub/02619609c4e23f2820b545120103e5aa to your computer and use it in GitHub Desktop.
macOS installed apps information
class MacOSAppsInfoFetcher {
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
}
// item.attributes - for more attributes
print(item.value(forAttribute: kMDItemDisplayName as String) ?? "unknown")
print(item.value(forAttribute: kMDItemCFBundleIdentifier as String) ?? "unknown")
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment