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

3 Answers 3

up vote 3 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

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
    
You wouldn't need to loop at the end if you mdimported the temp file. –  Lri Oct 12 '11 at 13:05

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

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.