如何检索所有可用的Finder标签? [英] How do I retrieve all available Finder tags?

查看:71
本文介绍了如何检索所有可用的Finder标签?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试检索所有可用的Finder标签的列表.

I'm trying to retrieve a list of all the available Finder tags.

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

I found NSWorkspace().fileLabels, which does return an array, but only an array of the tag colours, not the tags themselves:

print(NSWorkspace.shared().fileLabels) // prints ["None", "Gray", "Green", "Purple", "Blue", "Yellow", "Red", "Orange"]

您所看到的甚至不是所有的默认标签,它缺少了家庭工作重要,并且显然没有有我创建的任何自定义代码.看起来这只是fileLabelColors附带的漂亮名称.

Which as you can see is not even all the default tags, it's missing Home, Work and Important, and obviously doesn't have any of the custom ones that I created. It looks like it's just the nice names that go with fileLabelColors.

我发现NSMetadataQuery可以实际搜索事物,但是如何获得在Finder中创建的所有标签的列表?

I found NSMetadataQuery for actually searching for things, but how do I get a list of all the tags I have created in the Finder?

推荐答案

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

NSWorkspace.shared().fileLabels only returns the system tags that were available when the user account was created (the default system tags).

不幸的是,macOS中没有API可以检索您在Finder中创建的标签.

There's unfortunately no API in macOS to retrieve the tags that you have created yourself in the 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)

这将获得所有 Finder标签标签.

This gets all Finder tags labels.

您还可以仅选择自定义标签(忽略系统标签):

You can also select only the custom tags (ignore the system ones):

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标签?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆