macos Mac OS X:向任何文件添加自定义元数据字段
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8530825/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Mac OS X : add a custom meta data field to any file
提问by AP.
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
谢谢
采纳答案by Ned Deily
回答by Lri
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 -wx
如果值为纯文本,则不需要:
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.
当您从 Finder 添加 Spotlight 评论时,它会同时存储为扩展属性和 .DS_Store 文件。如果您只是添加扩展属性,则 Spotlight 评论字段在 Finder 中显示为空白,但评论元数据仍由 Spotlight 编制索引。
回答by Gordon Davisson
This sounds like a job for extended attributes. You can get and set them from the command line with xattr, and from programs with getxattrand setxattr.
这听起来像是扩展属性的工作。您可以使用xattr从命令行获取和设置它们,也可以使用getxattr和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 pageclaims spotlight will index other xattrs prefixed by "com.apple.metadata:", but I haven't gotten it to work.
但是,扩展属性(至少通常)不被 Spotlight 索引。我知道的唯一例外是“com.apple.metadata:kMDItemFinderComment”属性,它应该包含带有实际可索引注释的二进制格式plist(参见@PurplePilot 的回答)。 此页面声称聚光灯将索引以“com.apple.metadata:”为前缀的其他 xattrs,但我还没有让它工作。
回答by Anderson Freitas
If you want to programmatically set the "Finder Comment" of a file (see @PurplePilot's answer), try this:
如果您想以编程方式设置文件的“查找器注释”(请参阅@PurplePilot 的回答),请尝试以下操作:
1) Create a regular xml plist file with your comments:
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) Convert the plist to the accepted binary format:
2)将plist转换为可接受的二进制格式:
plutil -convert binary1 my_custom_comment.plist
3) Using xattr
, set the kMDItemFinderComment metadata:
3) 使用xattr
,设置 kMDItemFinderComment 元数据:
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.
您可以看到xattr -l MyFile
评论在那里并且以正确的二进制格式显示,但由于某种原因,Finder 没有在评论栏中显示它(至少对我而言)。
Searching against the spotlight database with mdfind "My Custom Comment"
will return all the files with this comment.
搜索聚光灯数据库mdfind "My Custom Comment"
将返回带有此注释的所有文件。
回答by PurplePilot
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.
右键单击和信息,或 cmd + i 在查找器中选择文件时将打开一个信息面板,您可以在顶部添加将在 Spotlight 中引用的数据。被称为 Spotlight 评论。您也可以对目录执行此操作。我不确定这是否是最好的方法,但这是我所知道的唯一方法。