Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/macos/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swift 如何检索所有可用的Finder标记?_Swift_Macos_Cocoa_Finder - Fatal编程技术网

Swift 如何检索所有可用的Finder标记?

Swift 如何检索所有可用的Finder标记?,swift,macos,cocoa,finder,Swift,Macos,Cocoa,Finder,我正在尝试检索所有可用查找器标记的列表 我找到了NSWorkspace().fileLabels,它确实返回一个数组,但只返回一个标签颜色数组,而不是标签本身: print(NSWorkspace.shared().fileLabels) // prints ["None", "Gray", "Green", "Purple", "Blue", "Yellow", "Red", "Orange"] 正如您所看到的,它甚至不是所有的默认标记,它缺少了Home、Work和Important,显然没

我正在尝试检索所有可用查找器标记的列表

我找到了
NSWorkspace().fileLabels
,它确实返回一个数组,但只返回一个标签颜色数组,而不是标签本身:

print(NSWorkspace.shared().fileLabels) // prints ["None", "Gray", "Green", "Purple", "Blue", "Yellow", "Red", "Orange"]
正如您所看到的,它甚至不是所有的默认标记,它缺少了HomeWorkImportant,显然没有我创建的任何自定义标记。看起来只是与FileLabelColor搭配的好名字

我找到了用于实际搜索内容的NSMetadataQuery,但如何获取我在Finder中创建的所有标记的列表?

NSWorkspace.shared()。fileLabels
仅返回创建用户帐户时可用的系统标记(默认系统标记)

不幸的是,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 { $0["n"] as? String }
        }
    }
    return []
}

let all = allTagLabels()
print(all)
这将获取所有查找器标签

也可以仅选择自定义标记(忽略系统标记):


啊,谢谢。接下来的一个问题是,很明显我没有找到关于这个的文档,你知道com.apple.finder.plist这样的东西是在哪里被记录的吗。同样,正如FYI
allTagLabels
返回了所有标签一样,但是
customTagLabels
没有返回我创建的所有自定义标签。了解这些差异实际上可能对我有用。不客气。不幸的是,由于没有用于此的公共API,因此也没有有效的文档。您可以打印
pdict
的内容以查看此plist文件中的所有可用内容。您知道如何从自定义标签中获取颜色吗?不幸的是,启用沙盒后无法访问此
~/Library/SyncedPreferences/com.apple.finder.plist
。@Daniel是的,代码中的注释中提到了它(第一个示例)。沙盒应用程序的解决方案是让用户自己使用NSOpenPanel选择文件,然后您可以获取文件url。
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 []
}