如何从Firefox插件打开外部应用程序?(例如:默认文本编辑器)

gal*_*007 4 editor add-on firefox-addon

我需要我的插件才能用外部工具编辑一些文件.有任何想法吗?

jon*_*o45 5

当提供路径时,此代码段会显示或启动文件:

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
}
Run Code Online (Sandbox Code Playgroud)

但是,您可能需要使用nsIProcess才能运行带参数的可执行文件(例如您尝试使用可执行文件打开的文件路径).

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.
}
Run Code Online (Sandbox Code Playgroud)

文档:

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