Mac OS X:向任何文件添加自定义元数据字段
我希望能够为任何文件设置(和获取)自定义元数据属性。
最好的方法是什么?
谢谢
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
自定义属性名称对我有用:
如果值是纯文本,则不需要 xattr -wx:
当您从 Finder 添加 Spotlight 评论时,它会存储为扩展属性并存储在 .DS_Store 文件中。如果您仅添加扩展属性,Spotlight 评论字段在 Finder 中显示为空白,但评论元数据仍由 Spotlight 编制索引。
Custom attribute names work for me:
xattr -wx
is not needed if the value is plain text: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.
OpenMeta 框架是事实上的第三方标准,用于使用以下命令向 OS X 文件添加元数据扩展属性。许多第三方应用程序都使用它。
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.
这听起来像是扩展属性的工作。您可以使用 xattr,以及带有 getxattr 和 setxattr。
但是,扩展属性(至少一般情况下)不会被 Spotlight 索引。我知道的唯一例外是“com.apple.metadata:kMDItemFinderComment”属性,它应该包含一个带有实际可索引注释的二进制格式的plist(请参阅@PurplePilot的答案)。 此页面声称聚光灯将索引以“com”为前缀的其他 xattrs .apple.metadata:”,但我还没有让它工作。
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.
如果您想以编程方式设置文件的“Finder Comment”(请参阅@PurplePilot的答案),请尝试以下操作:
1)使用您的注释创建常规 xml plist 文件:
2)将 plist 转换为可接受的二进制格式:
3)使用
xattr
,设置 kMDItemFinderComment 元数据:您可以使用
xattr -l MyFile
看到注释存在并且采用正确的二进制格式,但由于某种原因Finder 不会在评论栏中显示它(至少对我来说)。使用
mdfind "My Custom Comment"
在聚光灯数据库中搜索将返回带有此评论的所有文件。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:
2) Convert the plist to the accepted binary format:
3) Using
xattr
, set the kMDItemFinderComment metadata: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.在查找器中选择文件后,右键单击“信息”或 cmd + i 将打开一个信息面板,您可以在顶部添加将在 Spotlight 中引用的数据。称为聚光灯评论。您也可以对目录执行此操作。我不确定这是否是最好的方法,但这是我知道的唯一方法。
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.