views:

16

answers:

1

I need to set an environment variable for my application when it is started from the Finder. It is the location of a directory. The path is relative to $HOME. I am using the Info.plist file to set this environment variable. However, I cannot get it to take $HOME or ~. Can this be done?

i.e. I want something like this:

MYAPPDIR=~/myappname

Yes, I am using the .plist editor. It does work, i.e. my app finds it if I hardcode the full path like this:

MYAPPDIR=/Users/myname/myappname

A: 

Why do you need to do that? If you really want to set the environment variable in your app, do this:

 NSString* expanded=[@"~/myappname" stringByExpandingTildeInPath];
 setenv("MYAPPDIR",[expanded UTF8String],1);

please disregard the following rants if you really wanted to set the environment variable.


However, never force a user to put your app in a certain directory.

In addition, never save a file in a user-visible non-user-configurable place.

In OS X, applications are made into .app bundles so that a user can freely move it.

If you need to get the app's current position, just use

NSString*appPath=[[NSBundle mainBundle] bundlePath];

If you'd like to save a big file somewhere, create a directory inside ~/Library/Application Support/ called

~/Library/Application Support/yourappname/

and save files inside. If you just need to save a few data, use NSUserDefaults. It automatically saves the data to

~/Library/Preferences/com.yourcompany.yourapp.plist

and re-loads the content when the app is launched again. Just do

[[NSUserDefaults standardUserDefaults] setObject:@"boo" forKey:@"bar"];

and retrieve, after your app's relaunched, using

NSString* boo=[[NSUserDefaults standardUserDefaults] stringForKey:@"bar"];

etc.

Yuji
I appreciate the rants. I am a Mac novice and am glad to have the advice.
JJ3