Created
June 8, 2020 15:31
-
-
Save eddieh/6f80981532038ed7852cca48cb4a883b to your computer and use it in GitHub Desktop.
Simplest possible single-file Cocoa app with dock icon and menu
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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