Minimal Cocoa application - problem with menu

I need to implement minimal Cocoa application without Xcode. I am trying this example:

https://www.cocoawithlove.com/2010/09/minimalist-cocoa-programming.html


#import <Cocoa/Cocoa.h>
int main()
{
[NSAutoreleasePool new];
[NSApplication sharedApplication];
[NSApp setActivationPolicy:NSApplicationActivationPolicyRegular];
id menubar = [[NSMenu new] autorelease];
id appMenuItem = [[NSMenuItem new] autorelease];
[menubar addItem:appMenuItem];
[NSApp setMainMenu:menubar];
id appMenu = [[NSMenu new] autorelease];
id appName = [[NSProcessInfo processInfo] processName];
id quitTitle = [@"Quit " stringByAppendingString:appName];
id quitMenuItem = [[[NSMenuItem alloc] initWithTitle:quitTitle
action:@selector(terminate:) keyEquivalent:@"q"] autorelease];
[appMenu addItem:quitMenuItem];
[appMenuItem setSubmenu:appMenu];
id window = [[[NSWindow alloc] initWithContentRect:NSMakeRect(10, 10, 200, 200)
styleMask:NSTitledWindowMask backing:NSBackingStoreBuffered defer:NO]
autorelease];
[window setTitle:appName];
[window makeKeyAndOrderFront:nil];
[NSApp activateIgnoringOtherApps:YES];
[NSApp run];
return 0;
}


It works well, except for one little problem: Immediately after the launch, menu is altered, but is inactive (clicking it does nothing). If I select some other application and then get back to that minimal one, menu starts working as expected.

Any idea how to fix this?

looks like NSApplicationActivationPolicyRegular and activateIgnoringOtherApps: were missing according to https://github.com/rgl/minimal-cocoa-app

    [NSApp setActivationPolicy:NSApplicationActivationPolicyRegular];

    dispatch_async(dispatch_get_main_queue(), ^{
        [NSApp activateIgnoringOtherApps:YES];
    });

here is the updated code:

// cc -o min-cocoa min-cocoa.m -framework Cocoa
#import <Cocoa/Cocoa.h>

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        [NSApplication sharedApplication];
        id menubar = [NSMenu new];
        id appMenuItem = [NSMenuItem new];
        [menubar addItem:appMenuItem];
        [NSApp setMainMenu:menubar];
        id appMenu = [NSMenu new];
        id appName = [[NSProcessInfo processInfo] processName];
        id quitTitle = [@"Quit " stringByAppendingString:appName];
        id quitMenuItem = [[NSMenuItem alloc] initWithTitle:quitTitle action:@selector(terminate:) keyEquivalent:@"q"] ;
        [appMenu addItem:quitMenuItem];
        [appMenuItem setSubmenu:appMenu];
        id window = [[NSWindow alloc] initWithContentRect:NSMakeRect(10, 10, 200, 200) styleMask:NSWindowStyleMaskTitled backing:NSBackingStoreBuffered defer:NO];
        [window setTitle:appName];
        [window cascadeTopLeftFromPoint:NSMakePoint(20,20)];
        [window makeKeyAndOrderFront:nil];

        [NSApp setActivationPolicy:NSApplicationActivationPolicyRegular];

        dispatch_async(dispatch_get_main_queue(), ^{
            [NSApp activateIgnoringOtherApps:YES];
        });

        [NSApp run];
    }
    return 0;
}
Minimal Cocoa application - problem with menu
 
 
Q