How do you get the Finder "Kind" for a file?

325 views Asked by At

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?

2

There are 2 answers

0
JWWalker On BEST ANSWER

Try LSCopyKindStringForURL, passing the file URL.

0
user2926055 On

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