通过_kMDItemUserTags或kMDItemOMUserTags在xattr中使用多个关键字 [英] Using multiple keywords in xattr via _kMDItemUserTags or kMDItemOMUserTags

查看:235
本文介绍了通过_kMDItemUserTags或kMDItemOMUserTags在xattr中使用多个关键字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在重新组织图像时,由于OSX Mavericks的原因,我正在编写脚本以将标签插入图像文件的xattr字段中,以便可以使用Spotlight搜索它们. (为了安全起见,我也在编辑EXIF.)

While reorganizing my images, in anticipation of OSX Mavericks I am writing a script to insert tags into the xattr fields of my image files, so I can search them with Spotlight. (I am also editing the EXIF just to be safe.)

我的问题是:

  1. 哪个属性最适合使用? _kMDItemUserTags似乎是OSX版本,但是kMDItemOMUserTags已被OpenMeta使用.理想情况下,我希望可以与Linux和OSX向前兼容.

  1. Which attribute is the best to use? _kMDItemUserTags seems to be the OSX version, but kMDItemOMUserTags is already in use by OpenMeta. I would ideally like something that will be Linux and OSX forward compatible.

如何设置多个标签?是用逗号或空格分隔还是其他?

How do I set multiple tags? Are the comma- or space-delimited or something else?

作为示例,我使用python xattr模块发出以下命令:

As an example, using the python xattr module, I am issuing these commands:

xattr.setxattr(FileName, "_kMDItemUserTags", "Name - Sample")
xattr.setxattr(FileName, "kMDItemOMUserTags", "Name,Institution,Sample")

我也看到过提及以下标记:kOMUserTagskMDItemkeywords,但不知道它们是否有可能实现...

I have also seen mention of these tags: kOMUserTags and kMDItemkeywords but don't know if they are likely to be implemented...

进一步的研究表明,对于在10.8中可搜索的内容,

Further investigation has shown that for things to be searchable in 10.8,

  • 您需要以com.apple.metadata开头的kMD:
  • 您必须进行十六进制编码或包装在plist中.

此python代码将为kMDItemFinderComment生成标记,可在聚光灯下对其进行搜索...

This python code will generate the tag for kMDItemFinderComment which is searchable in spotlight...

def writexattrs(F,TagList):

    """ writexattrs(F,TagList):
    writes the list of tags to three xattr field:
    'kMDItemFinderComment','_kMDItemUserTags','kMDItemOMUserTags'
       This version uses the xattr library """

    plistFront = '<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"><plist version="1.0"><array>'
    plistEnd = '</array></plist>'
    plistTagString = ''
    for Tag in TagList:
        plistTagString = plistTagString + '<string>{}</string>'.format(Tag)
    TagText = plistFront + plistTagString + plistEnd

    OptionalTag = "com.apple.metadata:"
    XattrList = ["kMDItemFinderComment","_kMDItemUserTags","kMDItemOMUserTags"]
    for Field in XattrList:
        xattr.setxattr (F,OptionalTag+Field,TagText.encode('utf8'))
            # Equivalent shell command is xattr -w com.apple.metadata:kMDItemFinderComment [PLIST value] [File name]

我无法使其在具有可靠结果的文件夹上递归工作.

I could not get it to work recursively on a folder with reliable results.

推荐答案

  1. 如果您担心兼容性,则必须同时设置属性_kMDItemUserTagskMDItemOMUserTags.我认为没有其他解决方案,因为所有新的OS X应用程序都将使用前一个属性,而旧应用程序仍将使用后一个属性.这只是我的推测,但我猜想OpenMeta最终将停止使用新的本机API.展望未来,即使在Linux环境中,您也可以将_kMDItemUserTags属性用于新的应用程序/脚本.

  1. If you are worried about compatibility you have to set both of the attributes _kMDItemUserTags and kMDItemOMUserTags. I don't think there's a different solution since all the new OS X apps will use the former attribute, while the old apps still use the latter. This is just my speculation, but I guess OpenMeta will eventually be discontinued in favor of the new native API. Looking to the future you can use the _kMDItemUserTags attribute for your new apps/scripts even in Linux environment.

已将标签设置为属性列表编码的字符串数组.我不知道这是否是必需条件,但OS X会以二进制格式而不是XML格式编码属性列表.

The tags are set as a property list-encoded array of strings as you have figured out. I don't know if it is a requirement but the OS X encodes the property list in the binary format and not in XML as you did.

我修改了您的代码,以使用二进制属性列表作为属性值,并且一切正常.这是我的代码.我使用的是Biplist库,您可以通过easy_install biplist来获得.

I adapted your code to use binary property list as attribute values and everything worked. Here's my code. I am using biplist library which you can get with easy_install biplist.

import xattr
import biplist

def write_xattr_tags(file_path, tags):
    bpl_tags = biplist.writePlistToString(tags)
    optional_tag = "com.apple.metadata:"
    map(lambda a: xattr.setxattr(file_path, optional_tag + a, bpl_tags),
        ["kMDItemFinderComment", "_kMDItemUserTags", "kMDItemOMUserTags"])

在Spotlight中使用tag:<some_tag>对文件和目录进行了测试.

Tested with files and directories using tag:<some_tag> in Spotlight.

希望这会有所帮助.

  • 注意:我在此答案中使用的是OS X Lion,但它应该可以在Mavericks上正常使用.
  • 编辑:如果要将标签应用于目录的内容,则必须对每个文件分别进行处理,因为xattr python模块没有递归选项.
  • Note: I am using OS X Lion in this answer but it should work on Mavericks without any problems.
  • Edit: If you want to apply the tags to the contents of a directory it has to be done individually for every file since the xattr python module doesn't have the recursive option.

这篇关于通过_kMDItemUserTags或kMDItemOMUserTags在xattr中使用多个关键字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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