CocoaDev

Edit AllPages

Features/fixes to existing non-Apple tools, or just tools you wish you had but don’t have time to make (and maybe someone else will!)


Ask and ye possibly may receive if someone gets bored enough. – MikeFletcher

#import <Foundation/Foundation.h>

int main (int argc, const char * argv[]) { NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

NSDictionary *myDict;
id result;

// Check arguments; print usage if we don't have a filename and
// the key to print
if( argc != 3 ) {
  [[NSFileHandle fileHandleWithStandardError]
    writeData:
      [[NSString stringWithFormat: @"usage: %s in.plist key\n",
                 argv[0]
        ] dataUsingEncoding: NSASCIIStringEncoding]];

  [pool release];
  exit( 1 );
}

// Ask NSDictionary to try and read plist file
myDict = [NSDictionary dictionaryWithContentsOfFile:
                         [NSString stringWithCString: argv[1]]];

// If it couldn't be read, gripe and bail
if( myDict == nil ) {
  [[NSFileHandle fileHandleWithStandardError]
    writeData:
      [[NSString stringWithFormat:
                   @"Couldn't create dictionary from \"%s\"\n",
                 argv[1]
        ] dataUsingEncoding: NSASCIIStringEncoding]];

  [pool release];
  exit( 1 );
}

// try and pull out the requested key
result = [myDict objectForKey: [NSString stringWithCString: argv[2]]];

// If there's no such key, gripe and bail
if( result == nil ) {
  [[NSFileHandle fileHandleWithStandardError]
    writeData:
      [[NSString stringWithFormat:
                   @"Couldn't retrieve value for key \"%s\"\n",
                 argv[2]
        ] dataUsingEncoding: NSASCIIStringEncoding]];

  [pool release];
  exit( 1 );
}

// Write requested value to stdout
[[NSFileHandle fileHandleWithStandardOutput]
  writeData:
    [[NSString stringWithFormat: @"%@\n", result ]
      dataUsingEncoding: NSASCIIStringEncoding]];

[pool release];
return 0; }