Last active
October 1, 2026 16:00
-
-
Save samhenrigold/89b5909ecb2cf6b2f69e68f600ab78af to your computer and use it in GitHub Desktop.
Look up Apple device metadata and system artwork from hardware model identifiers using Uniform Type Identifiers
This file contains hidden or 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
| // Created by Sam Henri Gold on 2026-10-01. | |
| import Cocoa | |
| import UniformTypeIdentifiers | |
| public struct AppleDeviceType: Hashable, Sendable, CustomStringConvertible { | |
| public let modelIdentifier: String | |
| public let contentType: UTType | |
| public init?( | |
| _ modelIdentifier: String, | |
| conformingTo supertype: UTType? = nil | |
| ) { | |
| guard | |
| !modelIdentifier.isEmpty, | |
| let contentType = UTType( | |
| tag: modelIdentifier, | |
| tagClass: .appleDeviceModelCode, | |
| conformingTo: supertype | |
| ), | |
| contentType.isDeclared | |
| else { | |
| return nil | |
| } | |
| self.modelIdentifier = modelIdentifier | |
| self.contentType = contentType | |
| } | |
| public var identifier: String { | |
| contentType.identifier | |
| } | |
| public var localizedName: String? { | |
| contentType.localizedDescription | |
| } | |
| public var modelCodes: [String] { | |
| contentType.tags[.appleDeviceModelCode] ?? [] | |
| } | |
| public var icon: NSImage { | |
| NSWorkspace.shared.icon(for: contentType) | |
| } | |
| public var description: String { | |
| localizedName ?? identifier | |
| } | |
| } | |
| private extension UTTagClass { | |
| static let appleDeviceModelCode = UTTagClass(rawValue: "com.apple.device-model-code") | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Usage:
Result of image: