Created
February 8, 2010 05:18
-
-
Save atr000/297911 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #import <Foundation/Foundation.h> | |
| #import <AppKit/AppKit.h> | |
| #import <ScreenSaver/ScreenSaver.h> | |
| // Returns the file path of an unused temporary file | |
| // (e.g. /var/folders/NU/.../1023.tmp) | |
| NSString *getTempFilePath (NSString *extension) { | |
| NSString *tempDirectory = NSTemporaryDirectory(); | |
| NSFileManager *fileManager = [NSFileManager defaultManager]; | |
| NSString *tempFilePath; | |
| BOOL pathExists = YES; | |
| while (pathExists == YES) { | |
| // creating a random integer value between 1000 and 9999 with | |
| // a convenient function found in the ScreenSaver framework | |
| int randnum = SSRandomIntBetween(1000, 9999); | |
| // e.g. /TemporaryDirectory/8765.tmp | |
| tempFilePath = [NSString stringWithFormat:@"%@%d.%@", tempDirectory, randnum, extension]; | |
| if ([fileManager fileExistsAtPath:tempFilePath] == NO) { | |
| pathExists = NO; | |
| } | |
| } | |
| // tempFilePath is an autoreleased object (created with [NSSString stringWithFormat]) | |
| return tempFilePath; | |
| } | |
| // The main function called when the script is opened | |
| int main (int argc, const char * argv[]) { | |
| NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; | |
| // we expect exactly two arguments, the Uniform Type Identifier (UTI) | |
| // and the name of the script itself | |
| if (argc != 2) { | |
| printf("Usage: uticon [Uniform Type Identifier (e.g. public.pdf]\n"); | |
| return 1; | |
| } | |
| // getting the icon (NSImage) for the given UTI | |
| NSArray *args = [[NSProcessInfo processInfo] arguments]; | |
| NSString *UTI = [args objectAtIndex:1]; | |
| NSWorkspace *workspace = [NSWorkspace sharedWorkspace]; | |
| NSImage *icon = [workspace iconForFileType:UTI]; | |
| // converting to TIFF data and writing it to a temporary file | |
| NSData *tiffData = [icon TIFFRepresentation]; | |
| NSString *tiffFilePath = getTempFilePath(@"tiff"); | |
| BOOL writeSuccess = [tiffData writeToFile:tiffFilePath atomically:NO]; | |
| if (writeSuccess == NO) { | |
| printf("Could not write TIFF data to temporary file at: %s\n", [tiffFilePath UTF8String]); | |
| return 1; | |
| } | |
| // converting the TIFF to an ICNS file using the command line tool tiff2icns | |
| NSString *iconFilePath = getTempFilePath(@"icns"); | |
| NSTask *shellCommand = [[NSTask alloc] init]; | |
| [shellCommand setLaunchPath:@"/usr/bin/tiff2icns"]; | |
| [shellCommand setArguments:[NSArray arrayWithObjects:tiffFilePath, iconFilePath, nil]]; | |
| [shellCommand launch]; | |
| [shellCommand waitUntilExit]; | |
| [shellCommand release]; | |
| // removing the temporary TIFF file | |
| NSFileManager *fileManager = [NSFileManager defaultManager]; | |
| [fileManager removeItemAtPath:tiffFilePath error:NULL]; | |
| // returning the ICNS file path | |
| printf("%s\n", [iconFilePath UTF8String]); | |
| [pool drain]; | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment