From: Brian M. <ma...@ex...> - 2008-02-07 19:01:57
|
Using RubyCocoa from Leopard (10.5.1) : You can declare environment variables in a Cocoa app's Info.plist file like this: <key>LSEnvironment</key> <dict> <key>VEX</key> <string>acious!</string> </dict> In an Objective-C app, that value carries over into the environment: NSDictionary *env = [[NSProcessInfo processInfo] environment]; NSLog(@"====Here is the value of VEX in the processInfo environment: %@", [env objectForKey:@"VEX"]); NSDictionary *lsEnv = [[NSBundle mainBundle] objectForInfoDictionaryKey: @"LSEnvironment"]; NSLog(@"====Here is the value of VEX from the Info.plist environment: %@", [lsEnv objectForKey:@"VEX"]); Feb 7 12:34:56 frex Test[1660]: ====Here is the value of VEX in the processInfo environment: acious! Feb 7 12:34:56 frex Test[1660]: ====Here is the value of VEX from the Info.plist environment: acious! However, the value from Info.plist doesn't seem to make it into ENV. More surprisingly, it doesn't make it into the Cocoa processInfo environment: $stderr.puts "==== Here is the value of VEX in the environment:" $stderr.puts ENV['VEX'].inspect $stderr.puts "==== The value of VEX in the processInfo environment:" cocoa_env = NSProcessInfo.processInfo.environment $stderr.puts cocoa_env.objectForKey("VEX") $stderr.puts "==== The value of VEX in Info.plist:" plist_env = NSBundle.mainBundle.objectForInfoDictionaryKey("LSEnvironment") $stderr.puts plist_env.objectForKey("VEX") Feb 7 12:49:45 frex [0x0-0xc70c7].com.exampler.StatusbarApp[1689]: ==== Here is the value of VEX in the environment: Feb 7 12:49:45 frex [0x0-0xc70c7].com.exampler.StatusbarApp[1689]: nil Feb 7 12:49:45 frex [0x0-0xc70c7].com.exampler.StatusbarApp[1689]: ==== The value of VEX in the processInfo environment: Feb 7 12:49:45 frex [0x0-0xc70c7].com.exampler.StatusbarApp[1689]: nil Feb 7 12:49:45 frex [0x0-0xc70c7].com.exampler.StatusbarApp[1689]: ==== The value of VEX in Info.plist: Feb 7 12:49:45 frex [0x0-0xc70c7].com.exampler.StatusbarApp[1689]: acious! ----- Brian Marick, independent consultant Mostly on agile methods with a testing slant www.exampler.com, www.exampler.com/blog, www.twitter.com/marick |