Single instance behaviour on Mac OSX

7 views
Skip to first unread message

Rikki Loades

unread,
Feb 4, 2014, 6:25:29 AM2/4/14
to node-...@googlegroups.com
Hello,

So im trying to build a node-webkit app where there can only be one instance of it running. The package manifest has a property called single-instance which can be set to true (the default) to prevent multiple instances of an app or false to allow multiple instances.

Here is my app for reference:

app/package.json

{
  "name": "nw-demo",
  "main": "spawn-test/index.html",
  "node-main": "spawn-test/index.js",
  "window": {
  "show": true
  },
  "single-instance": true
}


app/spawn-test/index.html

test
<script>
var gui = require('nw.gui');

var win = gui.Window.get();
win.showDevTools();

console.log('Started with args: '+gui.App.argv.join(' '));

gui.App.on('open', function(args) {
console.log('Message received: '+args);
});
</script>

app/spawn-test/index.js

// this file is empty

Now if I package this into a app.nw bundle (the renamed zip) and launch it from two terminal tabs like so it opens two instances:


Now, on windows what happens is the second instance is terminated and  the "open" event is emit on the gui.App object (I need this behaviour on Mac OSX).

This seems to be the incorrect behaviour on Mac OSX.

So I have done some further testing on the Mac and if i bundle the app into a Mac ".app" bundle launching the first time it opens as expected. If you double click the app the second time, it simply moves the original to the foreground, and does not not emit an open event on the original which isn't quite the same as how Windows behaves.

If i launch the first instance by double clicking the .app bundle and then the second instance by running:


It works fine.

I assume then that node-webkit is relying on Mac OSX app bundles to enforce the single-instance behaviour?

These tests have been performed with both 0.8.4 and the 0.9.0 rc1 builds currently on the node-webkit github readme.

What I'm planning to build will be a background app which supports an existing app, so ideally I wouldn't use a mac app bundle and it needs to behave like the windows one to prevent multiple helpers opening. Does anybody know how I would go about making the mac behave like the windows counterpart?

There appears to be a unit test to check this (https://github.com/rogerwang/node-webkit/blob/master/tests/automatic_tests/single_instance/mocha_test.js) so perhaps it should work?

Any help would be much appreciated
Reply all
Reply to author
Forward
0 new messages