views:

203

answers:

1

I'm trying to put together a simple error reporting package. If my main program crashes, it saves a crashlog, then starts a reporter program. The reporter program asks the user if it can send the crash log to me, then does so. I'm using NSRunAlertPanel to create a basic message box.

For some reason, that message box is showing up buried underneath any other windows that may be open. Run the main package from a Finder window, it shows up on top, force it to crash, the reporter window shows up behind the Finder window.

Why is this happening, and how can it be solved?

Minimal test case:

#import <AppKit/AppKit.h>

int main(int a, char* av) {
  NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
  NSApplication* q = [[NSApplication alloc] init]; 
  NSRunAlertPanel(@"Hello", @"Aloha", @"OK", nil,nil);
  [pool release];
} 

Built with:

g++ test.mm -framework AppKit && ./a.out
+2  A: 

I seem to have come up with a solution, distilled from many tangentially-related webpages:

#import <AppKit/AppKit.h>

int main(int a, char* av) {
  NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
  NSApplication* q = [[NSApplication alloc] init]; 

  ProcessSerialNumber psn = {0, kCurrentProcess};
  TransformProcessType(&psn, kProcessTransformToForegroundApplication);

  [NSApp activateIgnoringOtherApps:YES];

  NSRunAlertPanel(@"Hello", @"Aloha", @"OK", nil,nil);
  [pool release];
} 

I do not pretend to understand this - it's cargo cult programming at its finest. Better answers, or explanations of what each step does, would be greatly appreciated.

ZorbaTHut
It's bringing your application to the front. Why you're doing this in main with no event loop, I have no idea.
Azeem.Butt
The "application" is a 150-line program to ask the user a single question, do a few simple HTTP requests via curl, and possibly deliver a short message to the user. It doesn't really need a full-scale GUI, nor do I want one - I'd rather keep it as cross-platform as possible, all I really need is an equivalent to the Windows MessageBox().
ZorbaTHut
It really doesn't matter what you want, the framework expects an event loop. Your struggle against this convention will more than likely be a complete waste of time.
Azeem.Butt
Well, it works fine, so . . .
ZorbaTHut