Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

How can you convert a filename extension string (like png) to a Uniform Type Identifier (like public.png)?

I'm looking for a general purpose script or utility, not anything like mdls -name kMDItemContentTypeTree <file>.

share|improve this question

4 Answers 4

up vote 4 down vote accepted

Easiest way to do that is to use UTTypeCreatePreferredIdentifierForTag():

#import <Foundation/Foundation.h>

int main (int argc, const char * argv[]) {
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
    NSArray *args = [[NSProcessInfo processInfo] arguments];

    if ([args count] < 2) {
        printf("usage:\n");
        printf("uti <filenameExtension>\n");
        exit(EXIT_SUCCESS);
    }

    NSArray *extensions = [args
                 subarrayWithRange:NSMakeRange(1, [args count] - 1)];

    for (NSString *filenameExtension in extensions) {
        NSString *utiType = (NSString *)UTTypeCreatePreferredIdentifierForTag(
                              kUTTagClassFilenameExtension,
                           (CFStringRef)filenameExtension, NULL);

        printf("%s\n", [utiType UTF8String]);
        [(NSString *)utiType release];
    }

    [pool drain];
    return 0;
}

Compiled uti executable and source code: uti.zip

Usage:

MacPro:~ mdouma46$ /Users/mdouma46/Developer/uti/uti png tga mov sdfad
public.png
com.truevision.tga-image
com.apple.quicktime-movie
dyn.age81g3dgqfwa

("unknown" file extensions are mapped to UTIs that begin with the dyn. prefix).

share|improve this answer

From version 1.5.2, duti (installation via Homebrew: brew install duti) supports the -e option - undocumented as of 1.5.2 - which retrieves the UTI declaration associated with a specified filename extension:

duti -e .png

yields, e.g.:

identifier: public.png
description: Portable Network Graphics image
declaration: {
    UTTypeIdentifier = public.png
    UTTypeDescription = Portable Network Graphics image
    UTTypeConformsTo = public.image
    UTTypeTagSpecification = {
        com.apple.ostype = PNGf
        public.mime-type = image/png
        com.apple.nspboard-type = Apple PNG pasteboard type
        public.filename-extension = png
    }
}

Thus, to retrieve just the UTI for extension .png, use:

duti -e .png | awk '{ print $2; exit }' # -> 'public.png'
share|improve this answer

getuti

if [[ $# == 0 ]]; then
    echo "Usage: getuti extension ..."
    exit 1
fi
for x in $@; do
    f="/tmp/me.lri.getuti.$x"
    touch "$f"
    mdimport "$f"
    mdls -name kMDItemContentType "$f" | sed 's|.*"\(.*\)"|\1|'
    rm "$f"
done

Usage

$ getuti png pngooo
public.png
dyn.ah62d4rv4ge81a5xhr7108
share|improve this answer

In case this is primarily about convenience (I'm willing to delete though):

#!/usr/bin/env bash

if [ $# = 0 ] ; then
        echo "Fail param"
        exit 1
fi

ext=$1
export TMPDIR=$HOME/Library
t=$( mktemp -t getuti.XXXXXX )

if [ $? != 0 ] ; then
        echo "Fail mktemp"
        exit 1
fi

mv $t $t.$ext

if [ $? != 0 ] ; then
        echo "Fail mv"
        exit 1
fi

while true ;
do
        uti=$( mdls -name kMDItemContentTypeTree -raw -nullMarker $t $t.$ext )
        if [ "$uti" = "${uti//$t/}" ] ; then
                echo "$uti"
                rm -f $t.$ext
                exit 0
        fi
        sleep 1
done

Usage:

$ ./getuti.sh jpeg
(
    "public.jpeg",
    "public.image",
    "public.data",
    "public.item",
    "public.content"
)
$ ./getuti.sh gif
(
    "com.compuserve.gif",
    "public.image",
    "public.data",
    "public.item",
    "public.content"
)
$ ./getuti.sh mdown
(
    "net.daringfireball.markdown",
    "public.text",
    "public.data",
    "public.item",
    "public.content"
)
$ ./getuti.sh foobarbazqux
(
    "public.data",
    "public.item"
)
share|improve this answer
1  
You wouldn't need to loop at the end if you mdimported the temp file. –  ؘؘؘؘ Oct 12 '11 at 13:05

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.