Writing Spotlight metadata to files on OS X (specifically kMDItemDisplayName)

心已入冬 提交于 2020-01-01 17:08:11

问题


I see that this has been answered previously by Ken T., but I have a case where the code appears to work, and the call to setxattr() returns 0, but the item I want to modify does not change.

Specifically, I'm trying to change the metadata attribute kMDItemDisplayName, and my call looks like this (modeled after the sample posted by Ken T):

[Note: the "name" param below is an NSString *]

rc = setxattr([pathString cStringUsingEncoding:NSUTF8StringEncoding],
                 "kMDItemDisplayName",
                 [name cStringUsingEncoding:NSUTF8StringEncoding],
                 [name lengthOfBytesUsingEncoding:NSUTF8StringEncoding],
                 0,
                 0);

Doing an mdls on the file in question shows that the kMDItemDisplayName attribute is present, but I can't get it to change to anything other than the actual file name (which I assume is the default behavior).

Am I misunderstanding something about how setxattr() is supposed to work?

Any help very much appreciated.

Oh, BTW, why am I trying to do this? It appears (from examining how Bare Bones' Yojimbo does things) that Spotlight uses the kMDItemDisplayName value to list files in the Spotlight search results menu in the finder, which is something I'd like to implement in my app.

Thanks!


Heyyyy... wait a minute...

From the command line, doing xattr -l shows that as far as xattr knows, there is an attribute called kMDItemDisplayName, and it is what I set it to be... However, mdls on the same file still shows the kMDItemDisplayName attribute as the file name.

Do I need to be asking about Launch Services instead of xattr stuff??



回答1:


OK. After hunting around a bit more and reading more Apple documentation I realized what I need to do. I'm answering my own question in the hope that this information may be of some assistance to someone else down the line.

Because I had to write my own mdimporter to support my app's file format, I thought I'd try adding the kMDItemDisplay name item to the metadata store at metadata import time.

To my amazement and delight, it worked on the first try!

So, the answer is, if you want to overwrite or add custom kMDItem* types, you do so at metadata import time, using a Spotlight importer.

Hope that someone finds this helpful!



来源:https://stackoverflow.com/questions/13597668/writing-spotlight-metadata-to-files-on-os-x-specifically-kmditemdisplayname

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!