如何从Firefox插件打开外部应用程序? (例如:默认的文本编辑器) [英] How can I open an external app from Firefox addon? (Eg: default text editor)

查看:924
本文介绍了如何从Firefox插件打开外部应用程序? (例如:默认的文本编辑器)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

解决方案

这段代码在提供路径时显示或启动一个文件:

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

if(localFile.exists()){
// localFile.reveal(); //打开文件的包含文件夹
// localFile.launch(); //启动文件
}

然而,您可能需要使用 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); //以另一个文件为参数启动可执行文件。




$ b

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


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

解决方案

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

这篇关于如何从Firefox插件打开外部应用程序? (例如:默认的文本编辑器)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆