Is there a better way of launching same app from GUI or Command line
- by markhunte
Hi All,
I worked out a way of running my Cocoa (GUI) app. From either the normal double clicking it,
Or from the CLI.
I realised that when an app launches from a double click (GUI), it returns an argument count (argc) of 0.
But when launched from the CLI it will have an argc of 1. So long as I do not put any arguments myself.
Which means I can use if.. else.. to determine how the app was launched.
This works fine for my app as I do not need to put arguments.
But I wondered if there was a better way of doing it.
Here is an example of the code in the main.m
int main (int argc, const char * argv[]) {
//This determins if the app is launched from the command line or app itself is opened.
if (argc == 1) {
//app was run from CLI
// Create a object
MyClass *mMyClass;
mMyClass = [[MyClass alloc] init];
// Read the Buffer
[mMyClass readBuffer];
// Write out file on disk
[mMyClass createFile];
[mMyClass doMoreStuff];
[mMyClass release];
mMyClass = nil;
return 0;
} else {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
//app was doubled click, (Opened)
return NSApplicationMain(argc, (const char **) argv);
[pool drain];
// */
// return NSApplicationMain(argc, (const char **) argv);
}}
Many Thanks.
M