/* ** Copyright (c) 2001 by Ian P. Cardenas, all rights reserved. ** ** This software is provided AS-IS. The author gives no warranty, ** real or assumed, and takes no responsibility whatsoever for any ** use or misuse of this software, or any damage created by its use ** or misuse. ** ** This software may be freely copied and distributed. */ /* ** A simple command line application to read a key from a plist. ** Example: plist read -Key pinning -Path com.apple.dock.plist ** ** questions and comments to ian.cardenas@ultraviolent.com */ /* renamed to plist.m to compile: cc -Wall -framework Foundation plist.m -o plist */ #import #define ERR_INIT 1 #define ERR_MISSING_KEY 2 int main (int argc, const char * argv[]) { NSAutoreleasePool * pool; NSString *plistKey, *plistPath; NSDictionary *plist; id plistKeyValue; int returnValue; returnValue = 0; pool = [NSAutoreleasePool new]; if (argc == 0) { printf("usage: plist read -Key keyname -Path path/to/file\n"); exit(ERR_INIT); } plistKey = [[NSUserDefaults standardUserDefaults] stringForKey:@"Key"]; NSCAssert(plistKey, @"please specify a key to read with -Key keyname"); plistPath = [[NSUserDefaults standardUserDefaults] stringForKey:@"Path"]; NSCAssert(plistPath, @"please specify the path to a plist with -Path path/to/file"); plist = [NSDictionary dictionaryWithContentsOfFile:plistPath]; NSCAssert1(plist, @"Unable to read file at path '%@'", plistPath); plistKeyValue = [plist valueForKeyPath:plistKey]; if (plistKeyValue == nil) { NSLog(@"key '%@' not found", plistKey); returnValue = ERR_MISSING_KEY; } else { printf("%s\n", [[plistKeyValue description] UTF8String]); } [pool release]; return returnValue; }