NSWindow behavior from a terminal App

Thierry Faucounau thierry at rsinc.com
Thu Sep 13 13:38:46 PDT 2001


You Rock!!!!

Thank you.

I hadn't thought of dispatching the NSEvents myself, in fact I haven't 
yet used NSEvents in any capacity (I come from the Mac OS classic side 
of the fence so I'm learning as I go).

This will make our users very happy.

On Thursday, September 13, 2001, at 02:18 PM, Gregory Robert Parker 
wrote:

> Thierry Faucounau <thierry at rsinc.com> wrote:
> > I have a command line application (run from Terminal) which creates 
> new,
> > separate NSWindows.
> >
> >         /* create the global Cocoa objects we need for this tool */
> >         pool = [[NSAutoreleasePool alloc] init];
> >
> >         app = [NSApplication sharedApplication];
> >
> >         /* stop that dock bouncing */
> >         [app finishLaunching];
> >
> >   ( Found that last one in the NSApplication header file. without it 
> the
> > dock icon bounces for quite a while)
> >
> > Then I periodically let the NSRunLoop go with the following code:
> >
> >         /* run one iteration of the cocoa run loop */
> >         [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode
> > beforeDate:[NSDate distantPast]];
>
> [NSApp run] doesn't really do that. Translating the description of
> -run to code, it behaves more like
> [NSApp sendEvent:[NSApp nextEventMatchingMask:...]].
> The runloop itself is probably run from -nextEventMatchingMask.
>
> Here's some code that has worked for me in the past. Windows are
> properly activated and window titlebar controls work (I've never
> tried the resize box). Note that [NSApp run] may contain undocumented
> magic required for complete AppKit operation; in particular, my
> windows didn't show up in the Dock menu.
>
>
> int main (int argc, const char * argv[]) {
>     NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
>     [NSApplication sharedApplication];
>     // [NSBundle loadNibNamed:@"MainMenu.nib" owner:NSApp];
>     [NSApp finishLaunching];
>
>     // make windows and do stuff here
>
>     while (1) {
>       NSEvent *event = [NSApp nextEventMatchingMask:NSAnyEventMask
>                               untilDate:[NSDate distantFuture]
>                               inMode:NSDefaultRunLoopMode
>                               dequeue:YES];
>       if (event) [NSApp sendEvent:event];
>     }
>
>     [pool release];
>     return 0;
> }
>
>
> (This is not a statement or advice from Apple; this is me speaking
> as an individual who's spent long hours hacking at Cocoa apps.)
>
>
> --
> Greg Parker     gparker at apple.com     gparker-omni at sealiesoftware.com
>

--
Thierry Faucounau
Research Systems, Inc.



More information about the MacOSX-dev mailing list