Created
March 4, 2010 06:20
-
-
Save atr000/321452 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> | |
// author: Martin Michel | |
// eMail: [email protected] | |
// created: 27.01.2010 (Apple tablet day!) | |
void printUsage () { | |
printf("ionicelf sets a given image as the icon of a given file\n"); | |
printf("Usage: ionicelf -img [img file path] -file [file path]\n"); | |
} | |
int main (int argc, const char * argv[]) { | |
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; | |
// collecting and verifying the command line arguments | |
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults]; | |
NSString *imgFilePath = [userDefaults stringForKey:@"img"]; | |
if (!imgFilePath) { | |
printf("Error: The image file path is missing (-img)\n"); | |
return 1; | |
} | |
NSFileManager *fileManager = [NSFileManager defaultManager]; | |
BOOL isDir; | |
if (![fileManager fileExistsAtPath:imgFilePath isDirectory:&isDir]) { | |
printf("Error: The image file does not exist:\n\t%s\n", [imgFilePath UTF8String]); | |
return 1; | |
} else { | |
if (isDir) { | |
printf("Error: The image file path points to a directory:\n\t%s\n", [imgFilePath UTF8String]); | |
return 1; | |
} | |
} | |
NSString *filePath = [userDefaults stringForKey:@"file"]; | |
if (!filePath) { | |
printf("Error: The file path is missing (-file)\n"); | |
return 1; | |
} | |
if (![fileManager fileExistsAtPath:filePath isDirectory:&isDir]) { | |
printf("Error: The file does not exist:\n\t%s\n", [filePath UTF8String]); | |
return 1; | |
} else { | |
if (isDir) { | |
printf("Error: The file path points to a directory:\n\t%s\n", [filePath UTF8String]); | |
return 1; | |
} | |
} | |
// creating an image object from the given image file path | |
NSImage *image = [[NSImage alloc] initWithContentsOfFile:imgFilePath]; | |
if (!image) { | |
printf("Error: Could not create image object from file:\n\t%s\n", [imgFilePath UTF8String]); | |
return 1; | |
} | |
// setting the image as the file's icon | |
NSWorkspace *workSpace = [NSWorkspace sharedWorkspace]; | |
BOOL success; | |
success = [workSpace setIcon:image forFile:filePath options:0]; | |
if (!success) { | |
printf("Error: Could not set given image as icon for file:\n\t%s\n", [filePath UTF8String]); | |
return 1; | |
} | |
[image release]; | |
[pool drain]; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment