open a file with default program in node-webkit
as PSkocik said, first detect the platform and get the command line :
function getCommandLine() {
switch (process.platform) {
case 'darwin' : return 'open';
case 'win32' : return 'start';
case 'win64' : return 'start';
default : return 'xdg-open';
}
}
second , execute the command line followed by the path
var exec = require('child_process').exec;
exec(getCommandLine() + ' ' + filePath);
Open external file with OS' default application (docx with Word, etc.) using NodeJS and Electron
Use the openItem()
function provided by Electron's shell
module, for example:
const shell = require('electron').shell;
const path = require('path');
shell.openItem(path.join(__dirname, 'test.docx'));
According to the docs the shell
module should be available in both the main/browser and renderer processes.
Note: Electron 9.0.0 The
shell.openItem
API has been replaced with an asynchronousshell.openPath
API. shell.openPath docs
Is there any way to open file in its default application using JavaScript?
Short answer: no. Can you imagine the security nightmare if JavaScript was allowed to execute arbitrary binary files on a user's machine?
Your solution is going to be to find a JavaScript library that allows for handling those files, similar to how PDF.js was made for PDFs in-browser.
How can I find the correct program to open a given file type in node.js?
You could try to use START
command.
var child = spawn("cmd.exe", ["/c", "start", "excelfile.xls"], {env: process.env, cwd: "c:\\"});
child.unref();
open a file with its default programme
There is no way for you to choose which application will be used to open your files with javascript...It just doesn't have that power.
Open all file types with Default OS program
I set nodeIntegration to true and added window.$ = window.jQuery = require('jquery'); to my mainWindow html. and now it recognizes shell.openItem.
Related Topics
How to Determine If a Variable Is 'Undefined' or 'Null'
Difference Between Variable Declaration Syntaxes in JavaScript (Including Global Variables)
How to Check If a Variable Is an Array in JavaScript
Is the JavaScript Date Object Always One Day Off
Read a File One Line At a Time in Node.Js
How to Simulate a Click With JavaScript
Jquery Scroll() Detect When User Stops Scrolling
Ecmascript Template Literals Like 'Some ${String}' Are Not Working
Graph Visualization Library in JavaScript
Why Does Babel Rewrite Imported Function Call to (0, Fn)(...)
JavaScript Call() & Apply() VS Bind()
How to Parse a CSV String With JavaScript, Which Contains Comma in Data
How to Match a Whole Word in JavaScript
Why Don't Logical Operators (&& and ||) Always Return a Boolean Result
How Does This JavaScript/Jquery Syntax Work: (Function( Window, Undefined ) { })(Window)