Last active
July 16, 2021 13:55
-
-
Save xbladesub/02619609c4e23f2820b545120103e5aa to your computer and use it in GitHub Desktop.
macOS installed apps information
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
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