将文件扩展名转换为 UTI

将文件扩展名转换为 UTI

如何将文件扩展名字符串(如png)转换为统一类型标识符(喜欢public.png)?

我正在寻找一个通用脚本或实用程序,而不是类似的东西mdls -name kMDItemContentTypeTree <file>

答案1

最简单的方法是使用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;
}

编译后的uti可执行文件和源代码:uti.zip

用法:

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

(“未知”文件扩展名映射到以前缀开头的 UTI dyn.)。

答案2

如果这主要是为了方便(但我愿意删除):

#!/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

用法:

$ ./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"
)

答案3

盖图蒂

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

用法

$ getuti png pngooo
public.png
dyn.ah62d4rv4ge81a5xhr7108

答案4

从 1.5.2 版本开始,杜蒂(安装通过自制: brew install duti) 支持-e选项 - 自 1.5.2 起未记录 - 它检索与指定文件扩展名关联的 UTI 声明:

duti -e .png

收益,例如:

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
    }
}

因此,若要仅检索扩展名的 UTI .png,请使用:

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

相关内容