Skip to content

Instantly share code, notes, and snippets.

@eddieh
Created June 8, 2020 15:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eddieh/6f80981532038ed7852cca48cb4a883b to your computer and use it in GitHub Desktop.
Save eddieh/6f80981532038ed7852cca48cb4a883b to your computer and use it in GitHub Desktop.
Simplest possible single-file Cocoa app with dock icon and menu
#import <Cocoa/Cocoa.h>
int main(int argc, char **argv)
{
NSRect rect;
NSWindowStyleMask style;
NSAutoreleasePool *pool;
NSApplication *app;
NSMenu *menubar, *appMenu;
NSMenuItem *appMenuItem, *quitMenuItem;
NSWindow *win;
style = NSWindowStyleMaskTitled;
style |= NSWindowStyleMaskClosable;
rect = NSMakeRect(100, 350, 400, 400);
pool = [NSAutoreleasePool new];
app = [NSApplication sharedApplication];
// give us a menubar and display app in dock
[app setActivationPolicy:NSApplicationActivationPolicyRegular];
// build a basic menubar
menubar = [[NSMenu new] autorelease];
appMenuItem = [[NSMenuItem new] autorelease];
appMenu = [[NSMenu new] autorelease];
quitMenuItem = [[NSMenuItem new] autorelease];
[menubar addItem:appMenuItem];
[appMenuItem setSubmenu:appMenu];
[appMenu addItem:quitMenuItem];
[quitMenuItem setTitle:@"Quit"];
[quitMenuItem setAction:@selector(terminate:)];
[quitMenuItem setKeyEquivalent:@"q"];
[app setMainMenu:menubar];
// make a window
win = [[[NSWindow alloc]
initWithContentRect:rect
styleMask:style
backing:NSBackingStoreBuffered
defer:NO
] autorelease];
// run the app and bring it to the front
[win makeKeyAndOrderFront:nil];
[app activateIgnoringOtherApps:YES];
[app run];
// cleanup
[app release];
[pool release];
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment