I have been creating a node.js script, which runs on only Mac OS X as HTTP server.
I wanted to show the server status on the Status bar(right-top), but I didn't know how to do that.
(Because I have no experience of Cocoa app programing)

Then I finally find out with NodObjC.
NodObjC is a bridge module between node.js and Objective-C.


Cocoa app:

Before talking about NodObjC, you may need to understand how to add your menu on the Status Bar.
These are typical code with Xcode.

The below is really simple snippet code for status bar.


- (void) applicationDidFinishLaunching : (NSNotification *) aNotification {
  // Get a reference for systemStatusBar.
  NSStatusBar *bar = [NSStatusBar systemStatusBar];

  // Create a new menu.
  // "NSVariableStatusItemLength" indicates the menu size is adjusted automatically.
  NSStatusItem *sbItem = [bar statusItemWithLength:NSVariableStatusItemLength];

  // Keep in memory
  [sbItem retain];

  // Set text string
  [sbItem setTitle : @"Hello World" ];
}

I would like to avoid explaining of this code, because I don't have enough knowledge about cocoa.
This page is more described.
http://undefinedvalue.com/2009/07/07/adding-custom-view-nsstatusitem

Node.JS

First of all, you need to install NodObjC.

$> npm install NodObjC

Then run this snippet code:
var $ = require('NodObjC')
$.import('Cocoa')
 
var pool = $.NSAutoreleasePool('alloc')('init'),
    app  = $.NSApplication('sharedApplication');

// set up the app delegate
var AppDelegate = $.NSObject.extend('AppDelegate')
AppDelegate.addMethod('applicationDidFinishLaunching:', 'v@:@', function (self, _cmd, notif) {
  var systemStatusBar = $.NSStatusBar('systemStatusBar');
  // update: $.NSVariableStatusItemLength does not work on Mac OS X Yosemite
  //var statusMenu = systemStatusBar('statusItemWithLength', $.NSVariableStatusItemLength);
  var statusMenu = systemStatusBar('statusItemWithLength', -1);
  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');

Run this code, then "Hello World" should be on the right-top on your Desktop!
0

Add a comment

Blog Archive
About Me
Popular Posts
Popular Posts
Loading
Dynamic Views theme. Powered by Blogger.