简体   繁体   English

你如何获得文件的Finder“Kind”?

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

I want to get the Finder "Kind" for a file. 我想得到一个文件的Finder“Kind”。 For example, for a file "foo.css", I want the string "CSS style sheet". 例如,对于文件“foo.css”,我想要字符串“CSS样式表”。

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. 这给了我UTI的人类可读名称,在某些情况下效果很好(“foo.html”给出“HTML文本”),但对于CSS文件,它返回nil,所以显然与Finder不同。显示在“种类”栏中。

How do you get the Kind of a file? 你怎么得到那种文件?

尝试LSCopyKindStringForURL ,传递文件URL。

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". 我能够获得一个带有typeOfFile的.css文件类型的字符串:error:后跟localizedDescriptionForType : ,但字符串不是你想要的“CSS样式表”,它是“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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM