How can I open an external app from Firefox addon? (Eg: default text editor)

一个人想着一个人 提交于 2019-12-18 04:25:07

问题


I need my addon will be capable of editing some files with an external tool. Any ideas?


回答1:


This snippet reveals or launches a file when provided with a path:

var localFile = Cc["@mozilla.org/file/local;1"].createInstance(Ci.nsILocalFile);
localFile.initWithPath("C:\\some-directory\\some-file.png");

if(localFile.exists()){
    // localFile.reveal();       // open the containing folder of the file
    // localFile.launch();       // launch the file
}

However you might need to use nsIProcess in order to run an executable with parameters (such as the filepath you're trying to open with the executable).

var exeFile = Cc["@mozilla.org/file/local;1"].createInstance(Ci.nsILocalFile);
exeFile.initWithPath("C:\\Program Files\\...\\app.exe");
var parameter="C:\\some-directory\\some-file.txt";
if(exeFile.exists()){
    var process = Cc["@mozilla.org/process/util;1"].createInstance(Ci.nsIProcess);  
    process.init(exeFile);
    process.run(false,[parameter],1);  // launch the executable with another file as parameter.
}

documentation:

https://developer.mozilla.org/en-US/docs/XPCOM_Interface_Reference/nsIFile https://developer.mozilla.org/en-US/docs/XPCOM_Interface_Reference/nsILocalFile https://developer.mozilla.org/en-US/docs/XPCOM_Interface_Reference/nsIProcess https://developer.mozilla.org/en-US/docs/Code_snippets/File_I_O



来源:https://stackoverflow.com/questions/16754621/how-can-i-open-an-external-app-from-firefox-addon-eg-default-text-editor

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!