I'm trying to create a cocoa app with Node.js using NodObjC. I have been creating an app which runs on only MacOS X as HTTP server.

NodObjC https://github.com/TooTallNate/NodObjC

I want to show the server status with icon on StatusBar like this. enter image description here

I tried like this:

var $ = require('NodObjC');
$.import('Foundation');
$.import('Cocoa');

var systemStatusBar = $.NSStatusBar('systemStatusBar');
var _statusItem = systemStatusBar('statusItemWithLength', $.NSVariableStatusItemLength);
_statusItem('setHighlightMode', 'YES');
var title = $.NSString('stringWithUTF8String', 'Test');
_statusItem('setTitle', title);
_statusItem('setMenu', systemStatusBar);

But this code causes an error

node[15637:707] -[NSStatusItem _setMenuOwner:]: unrecognized selector sent to instance 0x10816d810

tmp/node_modules/NodObjC/lib/id.js:158
    throw e
          ^
NSInvalidArgumentException: -[NSStatusItem _setMenuOwner:]: unrecognized selector sent to instance 0x10816d810
    at Function.msgSend (tmp/node_modules/NodObjC/lib/id.js:156:21)
    at id (tmp/node_modules/NodObjC/lib/id.js:119:15)
    at tmp/test.js:22:3
    at wrapper (tmp/node_modules/NodObjC/lib/imp.js:49:20)
    at Number.<anonymous> (tmp/node_modules/NodObjC/node_modules/node-ffi/lib/callback.js:23:23)
    at ForeignFunction.proxy (tmp/node_modules/NodObjC/node_modules/node-ffi/lib/foreign_function.js:84:20)
    at Function.msgSend (tmp/node_modules/NodObjC/lib/id.js:153:23)
    at id (tmp/node_modules/NodObjC/lib/id.js:119:15)
    at Object.<anonymous> (tmp/test.js:30:1)
    at Module._compile (module.js:456:26)

I couldn't find any solution for this error. Could anybody give to me any advices?

有帮助吗?

解决方案

I finally figured out how to do that by myself.

var $ = require('NodObjC')
$.import('Cocoa')

var pool = $.NSAutoreleasePool('alloc')('init'),
    app  = $.NSApplication('sharedApplication'),
    statusMenu;



// set up the app delegate
var AppDelegate = $.NSObject.extend('AppDelegate')
AppDelegate.addMethod('applicationDidFinishLaunching:', 'v@:@', function (self, _cmd, notif) {
  var systemStatusBar = $.NSStatusBar('systemStatusBar');
  statusMenu = systemStatusBar('statusItemWithLength', $.NSVariableStatusItemLength);
  statusMenu('retain');
  var title = $.NSString('stringWithUTF8String', "Hello World");
  statusMenu('setTitle', title);
})
AppDelegate.register()

var delegate = AppDelegate('alloc')('init')
app('setDelegate', delegate)

app('activateIgnoringOtherApps', true)
app('run')

pool('release');

enter image description here http://masashi-k.blogspot.com/2013/07/statusbar-with-nodobjc.html

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top