2013-12-10 20 views
1

我想为文件找到Finder“Kind”。例如,对于文件“foo.css”,我想要字符串“CSS样式表”。如何获取文件的Finder“Kind”?

到目前为止,我在做这样的事情:

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... 
} 

这给我的UTI,在某些情况下效果很好(人类可读的名称“foo.html”给出“HTML文本“),但是对于CSS文件,它返回nil,所以它显然不是Finder在”Kind“列中显示的内容。

你是如何得到一种文件的?

回答

1

尝试LSCopyKindStringForURL,传递文件URL。

1

我能够得到一个.css文件类型的字符串,其中typeOfFile:error:后跟localizedDescriptionForType:,但字符串不是你想要的“CSS样式表”,而是“Dashcode CSS Document”。其他扩展的其他示例:

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