objective-ccocoamacosuti

Getting an Uniform Type Identifier for a given extension


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.


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
    

    Update 11+ years later:

    In Swift, there are two ways to do this, depending on your deployment target:

    1. If you want to run on macOS Catalina and earlier (before Big Sur) or iOS 13 and earlier:

      let fileExtension = "html"
      let unmanagedString = UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension as CFString,
                                                                  fileExtension as CFString, 
                                                                  nil)
      
      let typeIdentifier = unmanagedString?.takeRetainedValue() as String?
      

      The UTTypeCreatePreferredIdentifierForTag() func (still part of the CoreServices module) returns an Unmanaged<CFString>?; this is a type that boxes a CFString and you need to indicate (via one of the .take... methods) what the memory management semantics should be.

      Since the function follows the Create Rule naming pattern, it's going to return a CFString to us that we need to take ownership of. Therefore we use .takeRetainedValue() to transfer the ownership of the CFString from the manual memory management world into the ARC world.

      Then there's a decent amount of bridging to go from String to CFString and vice versa.

    2. macOS Big Sur (and iOS 14) got a new UniformTypeIdentifiers module that makes this a whole lot simpler:

      let fileExtension = "html"
      let typeIdentifier = UTType(filenameExtension: fileExtension)