NSRunAlertPanel appears behind the "active window"
I am trying to put together a simple bug reporting package. If my main program crashes, it will save the crash log and then start the reporter program. The reporter program asks the user if he can send me a crash log and then does so. I am using NSRunAlertPanel to create a main message box.
For some reason, this message box appears below any other window that might be open. Run the main package from the Finder window, it shows up at the top, makes it crash, the reporter window appears 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
a source to share
I seem to have come up with a solution, overridden from many tangent-related web pages:
#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 - this is what he cultivates in his cult. Higher answers or explanations of what each step is doing would be greatly appreciated.
a source to share