在node-webkit中使用默認程序打開一個文件

[英]open a file with default program in node-webkit


I want to give the user any option he want to edit a file, how can I open a file with the default program of the specific file type? I need it to work with Windows and Linux but Mac option would be great too.

我想給用戶任何他想要編輯文件的選項,如何使用特定文件類型的默認程序打開文件?我需要它與Windows和Linux一起使用,但Mac選項也很棒。

4 个解决方案

#1


11  

as PSkocik said, first detect the platform and get the command line :

正如PSkocik所說,首先檢測平台並獲取命令行:

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 sys = require('sys');
var exec = require('child_process').exec;

exec(getCommandLine() + ' ' + filePath);

#2


5  

For file on a disk:

對於磁盤上的文件:

var nwGui = require('nw.gui');
nwGui.Shell.openItem("/path/to/my/file");

For remote files (eg web page):

對於遠程文件(例如網頁):

var nwGui = require('nw.gui');
nwGui.Shell.openExternal("http://google.com/");

#3


2  

Detect the platform and use:

檢測平台並使用:

  • 'start' on Windows
  • 在Windows上“開始”
  • 'open' on Macs
  • 在Mac上'打開'
  • 'xdg-open' on Linux
  • Linux上的'xdg-open'

#4


0  

I am not sure if start used to work as is on earlier windows versions, however on windows 10 it doesn't work as indicated in the answer. It's first argument is the title of the window.

我不確定開始以前是否像以前的Windows版本一樣工作,但是在Windows 10上它不能像答案中所示那樣工作。它的第一個參數是窗口的標題。

Furthermore the behavior between windows and linux is different. Windows "start" will exec and exit, under linux, xdg-open will wait.

此外,Windows和Linux之間的行為是不同的。 Windows“start”將執行並退出,在linux下,xdg-open將等待。

This was the function that eventually worked for me on both platforms in a similar manner:

這是最終在兩個平台上以類似方式為我工作的功能:

  function getCommandLine() {
     switch(process.platform) {
       case 'darwin' :
         return 'open';
       default:
         return 'xdg-open';
     }
  }

  function openFileWithDefaultApp(file) {
       /^win/.test(process.platform) ? 
           require("child_process").exec('start "" "' + file + '"') :
           require("child_process").spawn(getCommandLine(), [file],
                {detached: true, stdio: 'ignore'}).unref(); 
  }

注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2015/04/27/7210b1d192bcc1c0e34e449e0ed494b3.html



 
粤ICP备14056181号  © 2014-2021 ITdaan.com