Question

I am new to both Cocoa programming and Xcode. I am wondering how I can setenv (or set environment variable) using MainMenu.nib (or .xib) file. I have someone else's code in the main function like this:

[NSApplication sharedApplication];
[NSBundle loadNibNamed:@"MainMenu" owner: NSApp];

After the second line, it can get an environment variable:

if (!getenv("R_HOME")) {
    fprintf(stderr, "R_HOME is not set.\n");
    return -1;
}

I want to know how one can construct a nib(or xib) file, e.g., MainMenu.xib, in Xcode 3's latest version so that it can be used to setenv. One thing that I did was to code setenv without using a nib file:

setenv("R_HOME", "/Library/Frameworks/R.framework/Resources", 1)

But, this did not work when I execute the Cocoa GUI programming by double clicking the xxx.app although that worked when I execute the program's command line version. So, it seems like that I need a way to set environment variables when a Mac GUI application is launched. I have seen other ways of setting environment variables, but I want to know how one can set environment variables using nib (or xib) files, and by loading it using NSBundle's loadNibNamed method.

Was it helpful?

Solution

I want to know how one can construct a nib(or xib) file, e.g., MainMenu.xib, in Xcode 3's latest version so that it can be used to setenv.

Nib files don't have anything to do with environment variables. They don't contain any code, and they have no impact on the program other than supplying data used to instantiate classes provided by the application's code. I suppose you could write a class that sets environment variables and then use a nib to instantiate that class, but there'd be no difference between doing that and just instantiating the class in your code.

One thing that I did was to code setenv without using a nib file:...But, this did not work when I execute the Cocoa GUI programming by double clicking the xxx.app although that worked when I execute the program's command line version.

How do you know it didn't work? Are you calling getenv() to check the value of R_HOME in your code, or are you using a command like env in the terminal? If the latter, you're not looking at the same environment where the variable was set.

So, it seems like that I need a way to set environment variables when a Mac GUI application is launched.

What are you trying to accomplish? It seems unlikely that you'd be setting an environment variable for your own program's use -- it already knows that value, so going out the environment seems pointless. Are you trying to communicate with another program?

I have seen other ways of setting environment variables, but I want to know how one can set environment variables using nib (or xib) files, and by loading it using NSBundle's loadNibNamed method.

Again, there's really no intersection between nib files and environment variables. If you want to be able to modify your app's behavior based on some external variable, you can set environment variables in your .login file, and those values should be readable by your app. Perhaps a better solution would be to use the defaults system -- your app can read and write values in the defaults system via NSUserDefaults, and you can read and write those same values at the command line using the defaults command.

OTHER TIPS

Environment variables are usually set along with options in your schemes.

From the "Product" menu choose "Edit scheme" and select your desired target. Now on the right hand side choose the "Arguments" tab.

On the "Arguments" tab you can set command line parameters and environment variables to be passed along to the running application.

To set environment variables in your distributable application, such that they will be set even for users (Xcode only affects your debugging runs), set the LSEnvironment property in your app's Info.plist.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top