Using multiple keywords in xattr via _kMDItemUserTags or kMDItemOMUserTags

前端 未结 1 2032
情深已故
情深已故 2021-02-02 03:55

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 wi

1条回答
  •  北荒
    北荒 (楼主)
    2021-02-02 04:44

    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.

    2. 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.

    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"])
    

    Tested with files and directories using tag: in Spotlight.

    Hope this helps.

    • 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.

    0 讨论(0)
提交回复
热议问题