Skip to content

Instantly share code, notes, and snippets.

@TooTallNate
Created September 8, 2011 17:07
Show Gist options
  • Save TooTallNate/1203944 to your computer and use it in GitHub Desktop.
Save TooTallNate/1203944 to your computer and use it in GitHub Desktop.
Creating a Cocoa GUI window with NodObjC, with a proper Menu, dock icon, and NSApplicationDelegate.
// This example adapted from Matt Gallagher's "Minimalist Cocoa Programming"
// blog article:
// http://cocoawithlove.com/2010/09/minimalist-cocoa-programming.html
var $ = require('NodObjC')
$.import('Cocoa')
var pool = $.NSAutoreleasePool('alloc')('init')
, app = $.NSApplication('sharedApplication')
app('setActivationPolicy', $.NSApplicationActivationPolicyRegular)
var menuBar = $.NSMenu('alloc')('init')('autorelease')
, appMenuItem = $.NSMenuItem('alloc')('init')('autorelease')
menuBar('addItem', appMenuItem)
app('setMainMenu', menuBar)
var appMenu = $.NSMenu('alloc')('init')('autorelease')
, appName = $('Hello NodeJS!')
, quitTitle = $('Quit ')('stringByAppendingString', appName)
, quitMenuItem = $.NSMenuItem('alloc')('initWithTitle', quitTitle
,'action', 'terminate:'
,'keyEquivalent', $('q'))('autorelease')
appMenu('addItem', quitMenuItem)
appMenuItem('setSubmenu', appMenu)
var styleMask = $.NSTitledWindowMask
| $.NSResizableWindowMask
| $.NSClosableWindowMask
var window = $.NSWindow('alloc')('initWithContentRect', $.NSMakeRect(0,0,200,200)
,'styleMask', styleMask
,'backing', $.NSBackingStoreBuffered
,'defer', false)('autorelease')
window('cascadeTopLeftFromPoint', $.NSMakePoint(20,20))
window('setTitle', appName)
window('makeKeyAndOrderFront', window)
// set up the app delegate
var AppDelegate = $.NSObject.extend('AppDelegate')
AppDelegate.addMethod('applicationDidFinishLaunching:', 'v@:@', function (self, _cmd, notif) {
console.log('got applicationDidFinishLauching')
console.log(notif)
})
AppDelegate.register()
var delegate = AppDelegate('alloc')('init')
app('setDelegate', delegate)
app('activateIgnoringOtherApps', true)
app('run')
@TooTallNate
Copy link
Author

Results in:

@tj
Copy link

tj commented Sep 8, 2011

boom! better irc client here we come

@matths
Copy link

matths commented Dec 20, 2012

the last line > app('run') > results in a "bus error 10".
So What do I wrong?
I am on a quite new MacBookPro, Mountain Lion, latest versions of node and nodObjC.

@matths
Copy link

matths commented Dec 21, 2012

so I could narrow it. It is related to AppDelegate.addMethod(). without that code, it works at least without crashing with a "bus error 10". Any ideas?

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