Skip to content

Instantly share code, notes, and snippets.

@simonwhitaker
Created March 22, 2012 09:48
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save simonwhitaker/2157405 to your computer and use it in GitHub Desktop.
Save simonwhitaker/2157405 to your computer and use it in GitHub Desktop.
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