Created
July 14, 2011 13:05
-
-
Save drewmccormack/1082407 to your computer and use it in GitHub Desktop.
NSURL category method to get the UTI of a file
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@implementation NSURL (MCExtensions) | |
-(NSString *)UTI | |
{ | |
FSRef fileRef; | |
Boolean isDirectory; | |
NSString *utiString = nil; | |
if ( !self.isFileURL ) return nil; | |
if (FSPathMakeRef((const UInt8 *)[self.path fileSystemRepresentation], &fileRef, &isDirectory) == noErr) { | |
CFDictionaryRef values = NULL; | |
CFStringRef attrs[1] = { kLSItemContentType }; | |
CFArrayRef attrNames = CFArrayCreate(NULL, (const void **)attrs, 1, NULL); | |
if (LSCopyItemAttributes(&fileRef, kLSRolesViewer, attrNames, &values) == noErr) { | |
if (values != NULL) { | |
CFTypeRef uti = CFDictionaryGetValue(values, kLSItemContentType); | |
if (uti != NULL) { | |
utiString = [[(id)uti copy] autorelease]; | |
} | |
CFRelease(values); | |
} | |
} | |
CFRelease(attrNames); | |
} | |
return utiString; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
After writing this, I realized that NSWorkspace already has a method to get the UTI (typeOfFile:error:). I'll leave this here in case it is useful just to see how you can use the C APIs to work with UTIs.