Skip to content

Instantly share code, notes, and snippets.

@orchetect
Last active March 31, 2024 17:55
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save orchetect/e14b15392e2978cb23f50bc939394c64 to your computer and use it in GitHub Desktop.
Save orchetect/e14b15392e2978cb23f50bc939394c64 to your computer and use it in GitHub Desktop.
import AppKit
// MARK: - App Icons
func getIcon(file path: String) -> NSImage? {
guard FileManager.default.fileExists(atPath: path)
else { return nil }
return NSWorkspace.shared.icon(forFile: path)
}
func getIcon(bundleID: String) -> NSImage? {
guard let path = NSWorkspace.shared.absolutePathForApplication(withBundleIdentifier: bundleID)
else { return nil }
return getIcon(file: path)
}
func getIcon(application: String) -> NSImage? {
guard let path = NSWorkspace.shared.fullPath(forApplication: application)
else { return nil }
return getIcon(file: path)
}
// MARK: - Bundles
/// Easily read Info.plist as a Dictionary from any bundle by accessing .infoDictionary on Bundle
func bundle(forBundleID: String) -> Bundle? {
guard let url = NSWorkspace.shared.urlForApplication(withBundleIdentifier: forBundleID)
else { return nil }
return Bundle(url: url)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment