views:

39

answers:

2

I'm trying to create a Mac app, which should return an error code in some cases. This is an Intel bundle. (It's a Carbon C++ project, but this is probably irrelevant.) The standard way to do it in C++ would be to have the main function return the value, and that's what I do, so I think that part is correct. (I also tried explicitly calling exit(42)) The question is: how do I get at this value? When I launch the app from the terminal with "open myapp", the return value is always 0, even if I always return a different number. (checked using echo $?) Is this the return value from the open command and is my app's return value being swallowed somewhere? I also always get a 0 return code if I launch it from another program using execve (which is how the app is designated to be used). Is it possible to retrieve a return code from an app? Thanks!

Sidenote: I need the program to be in a bundle.

+1  A: 

I think you probably need to execute the application binary directly, i.e. drill down into the bundle like this:

$ /Applications/Address\ Book.app/Contents/MacOS/Address\ Book

Paul R
I'm doing that. It doesn't help.
Cornelius Scarabeus
There's a difference between `open your.app` and executing `your.app/Contents/MacOS/yourApp`. The former doesn't give the return code of the app, but the latter does. That's what Paul wrote.
Yuji
I understood, and it was a good idea. In fact only the latter seems to work with execve(), but the return code doesn't seem to arrive, I get 0 when I wait for the process with wait(), not what I returned with exit(). I also tried it with the terminal, and I get a 0 there too.
Cornelius Scarabeus
I'm wondering whether your app is exiting form somewhere within, i.e. never getting back to `return status;` or `exit(status);` in `main()` ?
Paul R
I tried both - exiting using exit(status) and letting it return from main, and it did reach return then. It made no difference.I worked around the problem by just returning the value in a file inside the bundle. Of course it would be good to know the reason.
Cornelius Scarabeus
+1  A: 

You probably can't do that, as your main will return a NSApplicationMain() object. The end of the program is determined by the user, chosing to click on a "quit" button.

However, you may use the applicationWillTerminate method to do what you want.

Guillaume Lebourgeois
The problem already occurs without creating an NSApplicationMain object. A pretty much do-nothing app's return value already seems to go lost. Nor does calling exit(value) explicitly solve the problem.
Cornelius Scarabeus
There also doesn't seem to be a way to specify a return code in applicationWillTerminate (if it were, that could explain where the 0 return value is coming from). It shouldn't make any difference whether I call exit() from applicationWillTerminate or anywhere else in the code - althought I might give this a try, thanks for the idea!
Cornelius Scarabeus
Btw. NSApplicationMain is actually a function, not a class. And there is a gotcha there - it never returns. But this isn't the problem here.
Cornelius Scarabeus