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

5

26

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

Rescind answered 16/12, 2011 at 6:59 Comment(1)
How do you want Spotlight to interact with that?Udo
W
11

The OpenMeta framework is a de-facto third-party standard for adding metadata to OS X files using extended attributes. It is used by a number of third-party applications.

Wipe answered 16/12, 2011 at 18:14 Comment(2)
+1 although, as a side note, it seems that at least one application that previously supported OpenMeta will (on Mavericks) no longer find files that use OpenMeta tags alone.Inductive
Any documentation on this?Multiplicity
S
26

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.

Silverware answered 30/4, 2013 at 18:4 Comment(3)
That seems to work just fine now too on Catalina, 10.15.4 (19E287).Multiplicity
When adding the com.apple.metadata:kMDItemFinderComment field with xattr, as above, the comment shows up as a separate "Comment" meta tag in the "General" info section (Finder->Get info). So this is compatible with Finder AND it is indexed by Spotlight. (High Sierra) Thanks for this, solved my issues with tagging batches.Blackleg
Quick link to available extended attributes list, fields. Apple Developers - Meta Attributes, available keysBlackleg
W
11

The OpenMeta framework is a de-facto third-party standard for adding metadata to OS X files using extended attributes. It is used by a number of third-party applications.

Wipe answered 16/12, 2011 at 18:14 Comment(2)
+1 although, as a side note, it seems that at least one application that previously supported OpenMeta will (on Mavericks) no longer find files that use OpenMeta tags alone.Inductive
Any documentation on this?Multiplicity
C
10

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.

Capernaum answered 16/12, 2011 at 7:32 Comment(2)
Thanks for your answer. Is there a way to get all the file having a specific attribute?Rescind
If you can get Spotlight to index the xattr entries, you should be able to use it to look for files with a particular xattr. If not, I think you have to walk the entire filesystem, and use getxattr to check each file for metadata.Capernaum
B
7

If you want to programmatically set the "Finder Comment" of a file (see @PurplePilot's answer), try this:

1) Create a regular xml plist file with your comments:

<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<string>My Custom Comment</string>
</plist>

2) Convert the plist to the accepted binary format:

plutil -convert binary1 my_custom_comment.plist

3) Using xattr, set the kMDItemFinderComment metadata:

xattr -wx "com.apple.metadata:kMDItemFinderComment" "`xxd -ps my_custom_comment.plist`" MyFile

You can see with xattr -l MyFile that the comments are there and in the right binary format, but for some reason Finder doesn't show it (at least for me) in the Comments column.

Searching against the spotlight database with mdfind "My Custom Comment" will return all the files with this comment.

Blumenthal answered 18/12, 2011 at 23:18 Comment(0)
M
1

Right click and Info, or cmd + i when the file is selected in the finder will open an information panel and you can add data at the top that will be referenced in Spotlight. Is called Spotlight Comments. You can do this with directories as well. I am not sure if it is the best way but it is the only way i know of doing it.

Minimus answered 16/12, 2011 at 7:8 Comment(2)
There must be a tool to do this programmatically, too.Udo
I am assuming you would be able to do it from applescript and or automator. Had a quick look at automator and there are a load of prebuilt actions against folders and files so assume it is quite do-ableMinimus

© 2022 - 2024 — McMap. All rights reserved.