Friday, October 25, 2013

OS X file types: Uniform Type Identifiers (UTI)

As a follow on from the post about MIME types and default handlers on OS X, here's a bit more about types in general. Apple explains it created the Uniform Type Identifier System in part to allow developers to register for types using a tree hierarchy, so for example if you can handle a text file you can probably handle a lot of text-based filetypes (like XML, HTML etc.). It also abstracts away the underlying complexity of type identification e.g. (multiple extensions like .jpg, .jpeg, and MIME type of image/jpeg).

You declare new UTIs in your application Info.plist. Apple core types can be seen here:
/System/Library/Frameworks/CFNetwork.framework/Versions/A/Resources/CFNetworkCoreTypes-Info.plist
e.g. jpeg looks like this:
            UTTypeConformsTo = "public.image";
            UTTypeDescription = "JPEG image";
            UTTypeIdentifier = "public.jpeg";
            UTTypeTagSpecification =             {
                "com.apple.ostype" = JPEG;
                "public.filename-extension" =                 (
                    jpeg,
                    jpg,
                    jpe
                );
                "public.mime-type" =                 (
                    "image/jpeg",
                    "image/jpg"
                );
            };
and the same file also contains a MIME type to file extension map:
    MIMETypeToExtensionMap =     {
        "application/andrew-inset" =         (
            ez
        );
        "application/mac-compactpro" =         (
            cpt
        );
        "application/msexcel" =         (
            xls
        );
        "application/mspowerpoint" =         (
            ppt
        );
        "application/msword" =         (
            doc
        );
        "application/octet-stream" =         (
            dms,
            lha,
            lzh,
            class,
            so,
            iso,
            fla
        );

No comments: