Writing Spotlight metadata to files on OS X (speci

2020-06-27 14:44发布

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条回答
兄弟一词,经得起流年.
2楼-- · 2020-06-27 15:03

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!

查看更多
登录 后发表回答