Mac OS X : add a custom meta data field to any file

AP. picture AP. · Dec 16, 2011 · Viewed 17.6k times · Source

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

Answer

Lri picture Lri · Apr 30, 2013

Custom attribute names work for me:

$ xattr -w com.apple.metadata:MyAttribute gfdylvyieo a.txt
$ mdls -n MyAttribute a.txt
MyAttribute = "gfdylvyieo"
$ mdfind gfdylvyieo
/private/tmp/a.txt
$ mdfind 'MyAttribute=*'
/private/tmp/a.txt

xattr -wx is not needed if the value is plain text:

xattr -w com.apple.metadata:kMDItemFinderComment aa file.txt

When you add a Spotlight comment from Finder, it is stored both as an extended attribute and in a .DS_Store file. If you just add an extended attribute, the Spotlight comment field appears blank in Finder, but the comment metadata is still indexed by Spotlight.