Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Lists the UTIs for a given list of file extensions
#import <Foundation/Foundation.h>
#import <CoreServices/CoreServices.h>
#import <libgen.h>
/*
convert-ext-to-uti
Shows the Uniform Type Identifier(s) (UTI) for a
file type based on its file extension. Output is
a tab-separated tuple of (extension, UTI) per
line.
SYNOPSIS
convert-ext-to-uti EXT [EXT...]
MORE INFO
http://en.wikipedia.org/wiki/Uniform_Type_Identifier
EXAMPLE USAGE
$ ./convert-ext-to-uti zip tgz
zip public.zip-archive
tgz org.gnu.gnu-zip-tar-archive
*/
int main(int argc, char *argv[]) {
if (argc == 1) {
printf("Usage: %s EXT [EXT...]\n", basename(argv[0]));
return 1;
}
@autoreleasepool {
for (int i = 1; i < argc; i++) {
CFStringRef ext = CFStringCreateWithCString(kCFAllocatorDefault, argv[i], kCFStringEncodingUTF8);
CFArrayRef types = UTTypeCreateAllIdentifiersForTag(kUTTagClassFilenameExtension, ext, nil);
for (CFIndex idx = 0; idx < CFArrayGetCount(types); idx++) {
CFStringRef type = CFArrayGetValueAtIndex(types, idx);
printf("%s\t%s\n", [(__bridge NSString*)ext UTF8String], [(__bridge NSString*)type UTF8String]);
}
CFRelease(ext);
CFRelease(types);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.