import AppKit import Darwin enum FinderColorLabel: Int, CaseIterable, Sendable { case red = 6 case orange = 7 case yellow = 5 case green = 2 case blue = 4 case purple = 3 case gray = 1 // Finder stores each tag as its name followed by a newline and a color index. private static let attribute = "com.apple.metadata:_kMDItemUserTags" var name: String { NSWorkspace.shared.fileLabels[rawValue] } var color: NSColor { NSWorkspace.shared.fileLabelColors[rawValue] } private var tag: String { "\(name)\n\(rawValue)" } static func colors(at url: URL) -> [Self] { let tags = (try? readTags(at: url)) ?? legacyTag(at: url) let colors = Set(tags.compactMap { tag in tag.lastIndex(of: "\n") .flatMap { Int(tag[tag.index(after: $0)...]) } .flatMap(Self.init(rawValue:)) }) return allCases.filter(colors.contains) } static func standardColors(at url: URL) -> Set { let tags = Set((try? readTags(at: url)) ?? legacyTag(at: url)) return Set(allCases.filter { tags.contains($0.tag) }) } func toggle(at url: URL) throws { var coordinationError: NSError? var writeError: Error? NSFileCoordinator().coordinate(writingItemAt: url, options: [], error: &coordinationError) { coordinatedURL in do { let storedTags = try Self.readTags(at: coordinatedURL) let legacy = storedTags == nil ? Self.legacyTag(at: coordinatedURL) : [] var tags = storedTags ?? legacy if tags.contains(tag) { tags.removeAll { $0 == tag } } else { tags.append(tag) } let bytes = try PropertyListSerialization.data(fromPropertyList: tags, format: .binary, options: 0) try coordinatedURL.withUnsafeFileSystemRepresentation { path in guard let path else { throw CocoaError(.fileWriteInvalidFileName) } let status = bytes.withUnsafeBytes { setxattr(path, Self.attribute, $0.baseAddress, $0.count, 0, XATTR_NOFOLLOW) } guard status == 0 else { throw NSError(domain: NSPOSIXErrorDomain, code: Int(errno)) } } if !legacy.isEmpty { try (coordinatedURL as NSURL).setResourceValue(0, forKey: .labelNumberKey) } } catch { writeError = error } } if let coordinationError { throw coordinationError } if let writeError { throw writeError } } private static func legacyTag(at url: URL) -> [String] { let freshURL = URL(fileURLWithPath: url.path) guard let index = try? freshURL.resourceValues(forKeys: [.labelNumberKey]).labelNumber, let color = Self(rawValue: index) else { return [] } return [color.tag] } private static func readTags(at url: URL) throws -> [String]? { try url.withUnsafeFileSystemRepresentation { path in guard let path else { throw CocoaError(.fileReadInvalidFileName) } let size = getxattr(path, attribute, nil, 0, 0, XATTR_NOFOLLOW) if size == -1 && errno == ENOATTR { return nil } guard size >= 0 else { throw NSError(domain: NSPOSIXErrorDomain, code: Int(errno)) } var bytes = Data(count: size) let count = bytes.withUnsafeMutableBytes { getxattr(path, attribute, $0.baseAddress, size, 0, XATTR_NOFOLLOW) } guard count >= 0 else { throw NSError(domain: NSPOSIXErrorDomain, code: Int(errno)) } guard let tags = try PropertyListSerialization.propertyList(from: bytes.prefix(count), format: nil) as? [String] else { throw CocoaError(.fileReadCorruptFile) } return tags } } }