I would like to me able to set (and get) a custom metadata attribute for any file.
What is the best way to do this?
Thanks
This sounds like a job for extended attributes. You can get and set them from the command line with xattr, and from programs with getxattr and setxattr.
However, extended attributes are (at least generally) not indexed by Spotlight. The only exception I know of to this is the "com.apple.metadata:kMDItemFinderComment" attribute, which should contain a binary-format plist with the actual indexable comment (see @PurplePilot's answer). This page claims spotlight will index other xattrs prefixed by "com.apple.metadata:", but I haven't gotten it to work.