Question

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.

Was it helpful?

Solution

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

OTHER TIPS

You can use the Terminal and invoke mdls which gives you all kinds of information on a certain file type including UTIs.

mdls /myPath/to/myFile.ext

mdls will then show you the associated UTIs in kMDItemContentTypeTree (it's also possible to call mdls from within your Cocoa App btw).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top