views:

112

answers:

1

Hello, I'm trying to find some way in Cocoa to translate from file extensions to Uniform Type Identifiers. That is, I want to find some way of doing this:

".jpg" => "public.jpeg"
".html" => "public.html" 
".ttf"=> "public.truetype-font"

I've searched on the NSWorkspace docs but couldn't find anything. The closest I could get was:

- (NSImage *)iconForFileType:(NSString *)fileType

that returns the icon for a file extension, and

– (NSString *)preferredFilenameExtensionForType:(NSString *)typeName

that does exactly the opposite of what I'm trying to do. Do any of you know how to do this?

I really hope I don't have to check for a lot of extensions by hand.

Thanks in advance.

+11  A: 

I needed this about a week ago:

NSString * UTI = (NSString *)UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension, 
                                                                   (CFStringRef)[myFilePath pathExtension], 
                                                                   NULL);

If I run this on the extensions @"php", @"jpg", @"html", and @"ttf", it prints:

public.php-script
public.jpeg
public.html
public.truetype-ttf-font
Dave DeLong
Just don't forget that because the function name has `Create` in it, you're responsible for `release`ing the object.
Alex
Thanks! It worked. I had some problems initially, but it was because I was not trimming whitespace and newline characters from the path strings. (I am reading them from a command line tool.)
Marco Aurélio