views:

18

answers:

3

I have a console program which is linked to the Foundation framework on Mac. How do I find out the folder the executable is in?

+1  A: 

What about [[NSBundle mainBundle] bundlePath]

David Gelhar
The executable is not in a bundle. It is a standalone mach executable.
Nick Brooks
A: 

The first argument passed to main() ( argv[0] ) is the path to the executable itself. If you wrote said console program, you could get it that way.

Joshua Nozzi
Only if you invoke the program by full path. If you put the program somewhere along your PATH (say, by installing it), then run it by name alone, `argv[0]` will be only the name.
Peter Hosey
Hmmm. I thought under BSD it was always the full path. I'm not sure where I read that and can't find any reference to it, so I suppose it was merely an assumption on my part. Good to know.
Joshua Nozzi
+4  A: 

Even though the tool is not in a bundle, you can still use some of the NSBundle methods. For example:

NSString * binaryPath = [[NSBundle mainBundle] executablePath];
NSString * executableFolder = [binaryPath stringByDeletingLastPathComponent];
Dave DeLong