Mac OS X:向任何文件添加自定义元数据字段

发布于 2024-12-21 18:17:57 字数 62 浏览 6 评论 0原文

我希望能够为任何文件设置(和获取)自定义元数据属性。

最好的方法是什么?

谢谢

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

冷默言语 2024-12-28 18:17:57

自定义属性名称对我有用:

$ 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:

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

当您从 Finder 添加 Spotlight 评论时,它会存储为扩展属性并存储在 .DS_Store 文件中。如果您仅添加扩展属性,Spotlight 评论字段在 Finder 中显示为空白,但评论元数据仍由 Spotlight 编制索引。

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.

听不够的曲调 2024-12-28 18:17:57

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.

掌心的温暖 2024-12-28 18:17:57

这听起来像是扩展属性的工作。您可以使用 xattr,以及带有 getxattrsetxattr

但是,扩展属性(至少一般情况下)不会被 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.

梦幻的味道 2024-12-28 18:17:57

如果您想以编程方式设置文件的“Finder Comment”(请参阅​​@PurplePilot的答案),请尝试以下操作:

1)使用您的注释创建常规 xml plist 文件:

<!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)将 plist 转换为可接受的二进制格式:

plutil -convert binary1 my_custom_comment.plist

3)使用xattr,设置 kMDItemFinderComment 元数据:

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

您可以使用 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:

<!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.

英雄似剑 2024-12-28 18:17:57

在查找器中选择文件后,右键单击“信息”或 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文