NSStatusBar与NodObjC

3
我正在尝试使用NodObjC创建一个基于Node.js的Cocoa应用程序。 我一直在创建一个仅在MacOS X上运行的HTTP服务器应用程序。

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

我希望能在状态栏中像这样使用图标显示服务器状态。 enter image description here 我尝试了以下方法:
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);

但是这段代码会导致错误。
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)

我找不到解决这个错误的方法。 有人可以给我一些建议吗?

1个回答

6

我终于自己想出了如何做到这一点。

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


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接