Is there any event will be trigger(or other way) when a running nwjs app be double clicked again?
You can use something like File Locking to lock a single file and check that file each time your application runs.
But I can't run any code when the app is already running.
You should create lock file when your app initialisation starts. If you run application second time, lock file will be already created, so your app can check that and prevent/continue running.
If your purpose is to count number of application launching times, you can store a count in the lock file.
A single-instance app can run twice? The problem is that I can do nothing when the app is already running.
How do you think your idea should be implemented? If you can't run any checks while your app is already running, then it's problem with your implementation/idea, because you're asking not logical things to happen.
There is no API in nw.js for such things.
I just want to show() and focus() the running app, when user launch the app again.
Check following docs:
Manifest format
Play with window
Show window after page is ready
Yeah, Thanks, I knew about these docs.
But the nwjs will trigger nothing when user re-launch the app.
The problem is: How can I knew It, when the app re-launched.
Pick one of the following:
App.dataPath.exe's, take a look at EnumProcesses, EnumWindows, and GetWindowThreadProcessId from the Windows API.Either way, you should handle cases where your app is going to be running concurrently under mutliple user accounts.

{
"name": "nw.js",
"main": "index.html",
"single-instance": false,
"window": {
"show": false
}
}
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>NW.js App</title>
<script src="single-instance.js"></script>
</head>
<body>
<h1>Instance: <span id="app-id"></span></h1>
<script>
document.addEventListener('DOMContentLoaded', function() {
document.querySelector('#app-id').innerText = Date.now();
});
</script>
</body>
</html>
(function() {
'use strict';
var gui = require('nw.gui');
var http = require('http');
var ipcServer = http.createServer(function(request, response) {
if(request) gui.Window.get().focus();
response.end();
});
ipcServer.on('error', function(error) {
if(error.errno === 'EADDRINUSE') {
http.get({hostname: '127.0.0.1', port: 37561}, function(response) {
process.exit(0);
});
}
else process.exit(1);
});
ipcServer.listen(37561, '127.0.0.1', function() {
gui.Window.get().show();
});
})();
Also consider playing around with persistent storage and call Window.show when your master process gets a data change notification.
Thanks, rdtsc. It's brilliant.
why not use the open event of nw.App while package.json {"single-instance": true} ?
The open event suggested by @cecilpeng answers the original question perfectly:
var gui = require("nw.gui");
gui.App.on('open', function (argString) {
// Parse argString to find out what args were passed to the second instance.
});
To just show() and focus() the running app when the user tries to open a new instance, the following code (based on comments above) is already sufficient:
```
nw.Window.open(win_url, { }, (win) => {
gui.App.on('open', (argString) => {
win.maximize();
win.show();
win.focus();
});
});
Most helpful comment
The
openevent suggested by @cecilpeng answers the original question perfectly: