views:

13

answers:

1

For some reason I cannot do this on files inside bundles and files residing on different volumes.

Am I the only one experiencing this problem, and if not how do I solve it?

The Finder seems not having difficulties showing spotlight properties for the same files that I'm experiencing problems with.

I want to obtain the kMDItemKind of files as well as some other properties.

CFStringRef path = CFSTR("/Applications/Chess.app/Contents/Info.plist"); // bad
// CFStringRef path = CFSTR("/Applications/Chess.app");                  // good

MDItemRef item = MDItemCreate(kCFAllocatorDefault, (CFStringRef)path);
if(!item) {
 NSLog(@"MDItemCreate returned NULL for path: %@", path);
 return nil;
}

CFStringRef kind = MDItemCopyAttribute( item, kMDItemKind );
if(kind) {
 NSLog(@"file: %@  kind: %@", path, (NSString*)kind);
 CFRelease( kind );                            
} else {
 NSLog(@"file: %@  has no kMDItemKind attribute", path);
}
CFRelease( item );

OUTPUT:

file: /Applications/Chess.app/Contents/Info.plist has no kMDItemKind attribute

Same problem if I use the commandline tool mdls, like this

prompt> mdls /Applications/Chess.app/Contents/Info.plist
kMDItemFSContentChangeDate = 2009-05-19 08:09:03 +0200
kMDItemFSCreationDate      = 2009-05-19 08:09:03 +0200
kMDItemFSCreatorCode       = ""
kMDItemFSFinderFlags       = 0
kMDItemFSHasCustomIcon     = 0
kMDItemFSInvisible         = 0
kMDItemFSIsExtensionHidden = 0
kMDItemFSIsStationery      = 0
kMDItemFSLabel             = 0
kMDItemFSName              = "Info.plist"
kMDItemFSNodeCount         = 0
kMDItemFSOwnerGroupID      = 0
kMDItemFSOwnerUserID       = 0
kMDItemFSSize              = 3294
kMDItemFSTypeCode          = ""

As you can see very little data is returned and no kMDItemKind

How would you obtain the metadata of the Info.plist ?

+1  A: 

Obtain kMDItemKind on files inside bundles returns nil

Correct. You can't get Metadata properties for files that Metadata hasn't indexed, and it doesn't index files inside bundles, in certain folders, and on any volume that has indexing disabled (including, but not limited to, read-only volumes, where Metadata can't write the index to the volume).

The Finder seems not having difficulties showing spotlight properties for the same files that I'm experiencing problems with.

How are you testing this? I Got Info on a PNG file, and on a PNG file inside an app bundle; the first had the usual image properties (e.g., dimensions), but the one inside a bundle showed nothing.

If you just want the Kind, use the LSCopyKindStringForURL function. Like most Core Services functions, it follows the CF memory-management rules.

Peter Hosey
Makes perfect sense. LSCopyKindStringForURL is exactly what I'm looking for, I was on the wrong track. Again you have the solution to my difficult problems. Thank you.
neoneye