簡體   English   中英

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

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

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

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

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

其次,執行命令行后跟路徑

var exec = require('child_process').exec;

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

您可以使用開放模塊:

npm install --save open

然后在你的 Node.js 文件中調用它:

const open = require('open');
open('my-file.txt');

該模塊已經包含檢測操作系統的邏輯,它運行與您的系統相關聯的默認程序。

對於磁盤上的文件:

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

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

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

檢測平台並使用:

  • 在 Windows 上“開始”
  • 在 Mac 上“打開”
  • Linux 上的“xdg-open”

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

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

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

  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(); 
  }

如果您打算使用默認編輯器編寫某種提示或簡單地打開鏈接文件,則必須等到程序結束或失敗。

靈感來自PSkocikKhalid 的答案。

const {exec} = require('child_process');

let openFile=function(filePath,mute){

    let command=(function() {
        switch (process.platform) { 
            case 'darwin' : return 'open '+filePath+' && lsof -p $! +r 1 &>/dev/null';
            case 'win32' : 
            case 'win64' : return 'start /wait '+filePath;
            default : return 'xdg-open '+filePath+' && tail --pid=$! -f /dev/null';
        }
    })();

    
    if(!mute)console.log(command);
    let child=exec(command);
    if(!mute)child.stdout.pipe(process.stdout);
    
    return new function(){
        this.on=function(type,callback){
            if(type==='data')child.stdout.on('data',callback);
            else if(type==='error')child.stderr.on('data',callback);
            else child.on('exit',callback);
            return this;
        };
        this.toPromise=function(){
            return new Promise((then,fail)=>{
                let out=[];
                this.on('data',d=>out.push(d))
                .on('error',err=>fail(err))
                .on('exit',()=>then(out));
            });
        };
    }();
};

用 :

openFile('path/to/some_text.txt')
.on('data',data=>{
    console.log('output :'+data);
})
.on('error',err=>{
    console.log('error :'+err);
})
.on('exit',()=>{
    console.log('done');
});

或者 :

openFile('path/to/some_text.txt').toPromise()
.then(output=>{
    console.log('done output :'+output.join('\n'));
}).catch(err=>{
    console.log('error :'+err);
});

PS:讓我知道它是否等待 winXX 以外的其他系統(靈感來自Rauno Palosaari 帖子但尚未測試)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM