Skip to content

Instantly share code, notes, and snippets.

@bellbind
Created July 19, 2012 20:32
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bellbind/3146586 to your computer and use it in GitHub Desktop.
Save bellbind/3146586 to your computer and use it in GitHub Desktop.
[macosx][objective-c]Make cocoa app without Xcode
// clang -framework Cocoa app.m -o app
// ./app
#import <Cocoa/Cocoa.h>
int main()
{
[NSAutoreleasePool new];
id app = [NSApplication sharedApplication];
[app setActivationPolicy:NSApplicationActivationPolicyRegular];
NSRect frame = NSMakeRect(0, 0, 300, 300);
id window =
[[[NSWindow alloc] initWithContentRect:frame
styleMask:NSTitledWindowMask
backing:NSBackingStoreBuffered
defer:NO] autorelease];
[window cascadeTopLeftFromPoint:NSMakePoint(10, 10)];
[window setTitle:@"Hello"];
[window makeKeyAndOrderFront:nil];
id button = [[[NSButton alloc] initWithFrame:frame] autorelease];
[button setTarget:app];
[button setAction:@selector(terminate:)];
[button setTitle:@"Quit"];
[[window contentView] addSubview:button];
[app activateIgnoringOtherApps:YES];
[app run];
return 0;
}
// clang -framework Cocoa -framework WebKit webkit.m -o webkit
// ./webkit
#import <Cocoa/Cocoa.h>
#import <WebKit/WebKit.h>
int main()
{
[NSAutoreleasePool new];
id app = [NSApplication sharedApplication];
[app setActivationPolicy:NSApplicationActivationPolicyRegular];
NSRect frame = NSMakeRect(0, 0, 640, 480);
int mask = NSTitledWindowMask | NSClosableWindowMask;
id window =
[[[NSWindow alloc] initWithContentRect:frame
styleMask:mask
backing:NSBackingStoreBuffered
defer:NO] autorelease];
[window cascadeTopLeftFromPoint:NSMakePoint(10, 10)];
[window setTitle:@"Hello"];
[window makeKeyAndOrderFront:nil];
id nc = [NSNotificationCenter defaultCenter];
[nc addObserver:app
selector:@selector(terminate:)
name:NSWindowWillCloseNotification
object:window];
id webview = [[[WebView alloc] initWithFrame:frame] autorelease];
id url = [NSURL URLWithString:@"http://example.com/"];
id req = [NSURLRequest requestWithURL:url];
[[webview mainFrame] loadRequest:req];
[[window contentView] addSubview:webview];
[app activateIgnoringOtherApps:YES];
[app run];
return 0;
}
@haydendavenport
Copy link

haydendavenport commented Feb 26, 2019

Do you know of any resources that I could use to learn how to code Mac stuff this way? I've been searching for hours and haven't come up with anything besides this code (which I am now studying intently, lol) and one other GitHub repo

Thanks!
Hayden

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment