How do you get the Finder “Kind” for a file?

白昼怎懂夜的黑 提交于 2019-12-08 09:16:11

问题


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

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