问题
I want to get the Finder "Kind" for a file. For example, for a file "foo.css", I want the string "CSS style sheet".
So far, I'm doing something like this:
NSURL *fileURL = [[NSURL alloc] initFileURLWithPath: filename];
NSString *utiType;
NSError *error;
BOOL success = [fileURL getResourceValue: &utiType
forKey: NSURLTypeIdentifierKey
error: &error];
if (!success) {
NSLog(@"failure during reading utiType: error = %@", error);
return;
}
NSString *utiDescription = (__bridge NSString *) UTTypeCopyDescription((__bridge CFStringRef) utiType);
if (utiDescription) {
// use utiDescription here...
}
This gives me the human-readable name of the UTI, which works well in some cases ("foo.html" gives "HTML text"), but for CSS files it returns nil, so it's clearly not the same thing that the Finder is showing in the "Kind" column.
How do you get the Kind of a file?
回答1:
Try LSCopyKindStringForURL
, passing the file URL.
回答2:
I was able to get a string for the type of a .css file with typeOfFile:error: followed by localizedDescriptionForType:, but the string wasn't "CSS style sheet" like you wanted, it was "Dashcode CSS Document". Some other examples of other extensions:
ext typeOfFile localizedDescriptionForType
------------------------------------------------------------------
.css com.apple.dashcode.css Dashcode CSS Document
.php public.php-script PHP script
.html public.html HTML text
.txt public.plain-text text
来源:https://stackoverflow.com/questions/20505768/how-do-you-get-the-finder-kind-for-a-file