All your answers worked correctly for me, but I found another solution that works best for me. I needed to start a Cocoa application from a command line tool, which I reached with the following line:
system("nohup /PATH/Arguments.app/Contents/MacOS/Arguments argument1 argument2 &");
nohup is a unix service that allows you to attach processes to yourself, so if you close the terminal window, the process remains alive.
The next problem that appeared was to capture arguments from a Cocoa application. "How to get arguments from AppDelegate.m
if main.m
is the one that receives them and just returns int."
Among Apple infrastructures and libraries, I found one that definitely solved the problem. This library is called crt_externs.h and contains two useful variables: one for examining the number of arguments and one for getting the arguments themselves.
extern char ***_NSGetArgv(void); extern int *_NSGetArgc(void);
So, inside the AppDelegate from the Cocoa application, we will write the following code to parse the arguments in NSString:
char **argv = *_NSGetArgv(); NSString *argument1 = [NSString stringWithCString:argv[1] encoding:NSUTF8StringEncoding]; NSString *argument2 = [NSString stringWithCString:argv[2] encoding:NSUTF8StringEncoding];
As we can see, we go directly to position 1 of the array of arguments, since position 0 contains the path itself:
argv[0] = '/PATH/Arguments.app/Contents/MacOS/Arguments' argv[1] = 'argument1' argv[2] = 'argument2'
Thank you all for your time and help. I learned a lot from you guys. I also hope this answer helps someone else :)
Greetings and happy coding!