views:

43

answers:

2

Hi there I have my Cocoa Application, which will be called with or without parameters in the command line.

I would like to take decisions over the parameters received on the application, ie, if a special parameter is received I would like to trigger an action on it. Is there anyway for doing this??

Cheers

+4  A: 

Sure, your program has a main() function like any C program. The default one that comes with a new Cocoa project just calls NSApplicationMain(), but you can do other actions if you'd like.

If you want to easily access the command line information from elsewhere in your program, you can use _NSGetArgv(), _NSGetArgc(), _NSGetEnviron(), and _NSGetProgname(). They're declared in crt_externs.h:

extern char ***_NSGetArgv(void);
extern int *_NSGetArgc(void);
extern char ***_NSGetEnviron(void);
extern char **_NSGetProgname(void);

Here's a blog post about these functions, and a link to the NSApplicationMain documentation.

Carl Norum
+1 I think that's the first time I've ever seen anything return a `char***`
Dave DeLong
@Dave It makes more sense as a CharPtrArrayPtr, which is still quite a mouthful but looks less Perlesque.
Jeremy W. Sherman
@Jeremy thinking of it as a "pointer to an array of strings" helps me understand it. I'm primarily confused about why it doesn't just return a `char**` tho...
Dave DeLong
@Dave, So that you can modify argc and argv directly, I would guess.
Carl Norum
A: 

You might find it easier to access the NSArgumentDomain in user defaults:

NSDictionary *const args = [[NSUserDefaults standardUserDefaults]
                             volatileDomainForName:NSArgumentDomain];

This will handle arguments of the form -NSZombieEnabled YES. Other forms of arguments (such as -NSZombieEnabled=YES) might be ignored; I haven't tested or looked at the source.

Jeremy W. Sherman