Skip to content Skip to sidebar Skip to footer

Using Multiple Keywords In Xattr Via _kmditemusertags Or Kmditemomusertags

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

Solution 1:

  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

defwrite_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:<some_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.

Post a Comment for "Using Multiple Keywords In Xattr Via _kmditemusertags Or Kmditemomusertags"