Re: Finding the executable path in a foundation tool.
Re: Finding the executable path in a foundation tool.
- Subject: Re: Finding the executable path in a foundation tool.
- From: Prachi Gauriar <email@hidden>
- Date: Tue, 08 Jul 2003 17:21:08 -0500
On Tuesday, July 8, 2003, at 5:02 PM, Thomas Harrington wrote:
On Tuesday, July 8, 2003, at 02:40 PM, Douglas Davidson wrote:
On Tuesday, July 8, 2003, at 12:50 PM, James Quick wrote:
For a Cocoa application
[[NSBundle mainBundle] executablePath];
will return to path to the currently running executable.
Since there is no bundle for a foundation tool, what's the
recommended approach.
A tool isn't bundled, but there is still an [NSBundle mainBundle]. A
synthetic one is created so that there will always be a main bundle.
The only exception would be if something went quite wrong, and e.g.
NSBundle could not locate the executable.
However if you use this approach in a Foundation tool, you'll find
that -executablePath returns nil, which is not terribly useful.
One way around the problem is demonstrated in Apple's AuthSample demo,
as part of the self-repair functionality. It's a bit ugly, but it
seems to do the job. I don't know of a Cocoa-based method, but would
be interested to hear of one.
How about the old-fashioned C way of just looking at argv[0]? Contents
of main.m:
#import <Foundation/Foundation.h>
int main (int argc, const char * argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
NSString *executablePath = [NSString stringWithUTF8String:argv[0]];
NSLog(@"%@\n", executablePath);
[pool release];
return 0;
}
Result:
2003-07-08 17:19:02.027 Test[1010] /tmp/Test/build/Test
-Prachi
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.