如何检索所有可用的 Finder 标签?
How do I retrieve all available Finder tags?
我正在尝试检索所有可用的 Finder 标签的列表。
我发现 NSWorkspace().fileLabels
,它 return 是一个数组,但只是标签颜色的数组,而不是标签本身:
print(NSWorkspace.shared().fileLabels) // prints ["None", "Gray", "Green", "Purple", "Blue", "Yellow", "Red", "Orange"]
如您所见,这甚至不是所有默认标签,它缺少 Home、Work 和 Important ,显然没有我创建的任何自定义项。看起来这只是 fileLabelColors 的好名字。
我发现 NSMetadataQuery 用于实际搜索内容,但我如何获得我在 Finder 中创建的所有标签的列表?
NSWorkspace.shared().fileLabels
仅 returns 创建用户帐户时可用的系统标签(默认系统标签)。
很遗憾,macOS 中没有 API 来检索您在 Finder 中自己创建的标签。
解决方法是解析~/Library/SyncedPreferences/com.apple.finder.plist
:
func allTagLabels() -> [String] {
// this doesn't work if the app is Sandboxed:
// the users would have to point to the file themselves with NSOpenPanel
let url = URL(fileURLWithPath: "\(NSHomeDirectory())/Library/SyncedPreferences/com.apple.finder.plist")
let keyPath = "values.FinderTagDict.value.FinderTags"
if let d = try? Data(contentsOf: url) {
if let plist = try? PropertyListSerialization.propertyList(from: d, options: [], format: nil),
let pdict = plist as? NSDictionary,
let ftags = pdict.value(forKeyPath: keyPath) as? [[AnyHashable: Any]]
{
return ftags.flatMap { [=10=]["n"] as? String }
}
}
return []
}
let all = allTagLabels()
print(all)
这会得到 all 个 Finder tags 标签。
您也可以 select 仅自定义标签(忽略系统标签):
func customTagLabels() -> [String] {
let url = URL(fileURLWithPath: "\(NSHomeDirectory())/Library/SyncedPreferences/com.apple.finder.plist")
let keyPath = "values.FinderTagDict.value.FinderTags"
if let d = try? Data(contentsOf: url) {
if let plist = try? PropertyListSerialization.propertyList(from: d, options: [], format: nil),
let pdict = plist as? NSDictionary,
let ftags = pdict.value(forKeyPath: keyPath) as? [[AnyHashable: Any]]
{
return ftags.flatMap { tag in
if let n = tag["n"] as? String,
tag.values.count != 2
{
return n
}
return nil
}
}
}
return []
}
我正在尝试检索所有可用的 Finder 标签的列表。
我发现 NSWorkspace().fileLabels
,它 return 是一个数组,但只是标签颜色的数组,而不是标签本身:
print(NSWorkspace.shared().fileLabels) // prints ["None", "Gray", "Green", "Purple", "Blue", "Yellow", "Red", "Orange"]
如您所见,这甚至不是所有默认标签,它缺少 Home、Work 和 Important ,显然没有我创建的任何自定义项。看起来这只是 fileLabelColors 的好名字。
我发现 NSMetadataQuery 用于实际搜索内容,但我如何获得我在 Finder 中创建的所有标签的列表?
NSWorkspace.shared().fileLabels
仅 returns 创建用户帐户时可用的系统标签(默认系统标签)。
很遗憾,macOS 中没有 API 来检索您在 Finder 中自己创建的标签。
解决方法是解析~/Library/SyncedPreferences/com.apple.finder.plist
:
func allTagLabels() -> [String] {
// this doesn't work if the app is Sandboxed:
// the users would have to point to the file themselves with NSOpenPanel
let url = URL(fileURLWithPath: "\(NSHomeDirectory())/Library/SyncedPreferences/com.apple.finder.plist")
let keyPath = "values.FinderTagDict.value.FinderTags"
if let d = try? Data(contentsOf: url) {
if let plist = try? PropertyListSerialization.propertyList(from: d, options: [], format: nil),
let pdict = plist as? NSDictionary,
let ftags = pdict.value(forKeyPath: keyPath) as? [[AnyHashable: Any]]
{
return ftags.flatMap { [=10=]["n"] as? String }
}
}
return []
}
let all = allTagLabels()
print(all)
这会得到 all 个 Finder tags 标签。
您也可以 select 仅自定义标签(忽略系统标签):
func customTagLabels() -> [String] {
let url = URL(fileURLWithPath: "\(NSHomeDirectory())/Library/SyncedPreferences/com.apple.finder.plist")
let keyPath = "values.FinderTagDict.value.FinderTags"
if let d = try? Data(contentsOf: url) {
if let plist = try? PropertyListSerialization.propertyList(from: d, options: [], format: nil),
let pdict = plist as? NSDictionary,
let ftags = pdict.value(forKeyPath: keyPath) as? [[AnyHashable: Any]]
{
return ftags.flatMap { tag in
if let n = tag["n"] as? String,
tag.values.count != 2
{
return n
}
return nil
}
}
}
return []
}