views:

82

answers:

2

Hi, I am on a Mac, programming with Xcode. I have a command line application that will read/write to files in it's bundle and I was wondering how to access these files.

Thanks, Mr. Man

EDIT: Would it work better if I just made a folder in the user's library folder?

+3  A: 

Not sure if I understand you question, but if you want the path of one of your resources you can use:

NSString *path = (NSString*)[[NSBundle mainBundle] pathForResource:@"sample" ofType:@"png"];

If you just want the app directory you could use:

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [[paths objectAtIndex:0] retain];

or

NSArray *resPaths = NSSearchPathForDirectoriesInDomains(NSApplicationDirectory, NSUserDomainMask, YES);
NSString *resourceDirectory = [[resPaths objectAtIndex:0] retain];

I'm not at my Mac right now, so I'm just pasting from our framework.

Andreas
Note: We actually build our command line tools as normal app bundles.
Andreas
@Andreas, how do you execute them?
Carl Norum
OK, I have used these in Objective-C but my program is in C, would your second and third ones work with regular C arrays?
Mr. Man
@Carl usually just with the full path inside the bundle (".../osx.app/Contents/MacOS/main").
Andreas
+3  A: 

Assuming you actually have a bundle, you can use the CoreFoundation CFBundle API. You can use CFBundleGetMainBundle() to find your app's bundle and the CFBundleCopy*URL() functions to get the URL of the bundle and its various components.

However, applications should not write to their bundles. There are a number of things that can go wrong with that, such as the current user not having write permission or a signed bundle getting broken. Generally, your application should put files it creates that aren't meant to be user-editable* in the ~/Library/Application Support folder. The normal place for user documents is the Documents folder.

(* To clarify "user editable," I mean files that the user is normally expected to navigate to and open himself rather than simply being a private store for data that's solely accessed through your app's interface. So for example TextEdit documents go in Documents while Stickies notes go in Application Support.)

Chuck
ok, so what should I do with files that ARE supposed to be user-editable?
Mr. Man
But don't have your app automatically dump files into `~/Documents` -- it's kind of rude to clutter the user's `Documents` folder.
mipadi
@mipadi: would it be ok to make a folder in the user's documents folder and work from there?
Mr. Man
Preferably the user should be able to choose where documents go.
Chuck
@Mr.Man: If your app *has* to automatically create files, they should go under `~/Library/Application Support`; otherwise, let the user choose where to put them.
mipadi
ok, that last part of your answer fixed it for me, thanks!
Mr. Man