[英]Perfom a ShellExecute from Firefox Addon
In my Firefox extensions I want to open certain files with the "default viewer" for that file type in Windows. 在Firefox扩展程序中,我想使用Windows中该文件类型的“默认查看器”打开某些文件。 So basically something similar to the ShellExecute('OPEN')
function call of the Windows API. 因此,基本上类似于Windows API的ShellExecute('OPEN')
函数调用。 Is it possible? 可能吗? If so, how can that be achieved? 如果是这样,那将如何实现?
The closest thing is nsIFile::launch
. 最接近的是nsIFile::launch
。 However, it is not implemented for every conceivable platform (but it is implemented at least for Windows, OSX, GTK/Gnome and compatible, KDE and Android). 但是,并非所有可能的平台都实现了该功能(但至少在Windows,OSX,GTK / Gnome和兼容的KDE和Android上实现了该功能)。
You cannot use ::launch
to instruct the OS (in particular Windows) to use a verb other than open
, though, so there is no equivalent to eg ShellExecute(..., "edit", ...)
. 但是,您不能使用::launch
来指示操作系统(特别是Windows)使用open
以外的动词,因此没有等效于ShellExecute(..., "edit", ...)
。
Here is a sample on how to use it: 以下是有关如何使用它的示例:
try {
var file = Services.dirsvc.get("Desk", Ci.nsIFile);
file.append("screenshot.png");
file.launch();
}
catch (ex) {
// Failed to launch because e.g. the OS returned an error
// or the file does not exist,
// or this function is simply not implemented for a particular platform.
}
You can of course create an nsIFile
instance from "raw" paths as well, eg (I'm on OSX): 当然,您也可以从“原始”路径创建nsIFile
实例,例如(我在OSX上):
var file = Cc["@mozilla.org/file/local;1"].createInstance(Ci.nsIFile);
Cc
and Ci
are shortcuts to Components.classes
and Components.interfaces
that most of the mozilla and add-ons code uses. Cc
和Ci
是大多数mozilla和附加组件代码使用的Components.classes
和Components.interfaces
快捷方式。 In the Add-on SDK you can get these via the Chrome Authority . 在附加SDK中,您可以通过Chrome Authority获得这些。
Edit I totally forgot that ShellExcute
will also handle URLs. 编辑我完全忘记ShellExcute
也将处理URL。 And you did ask only about "file type[s]", BTW. 而且您只询问了“文件类型”,顺便说一句。
Anyway, to open a random URI, you can use the nsIExternalProtocolService
. 无论如何,要打开随机URI,可以使用nsIExternalProtocolService
。
To launch with a default handler, which could also be a web protocol handler or similar, you can use the following code. 要使用默认处理程序(也可以是Web协议处理程序或类似程序)启动,可以使用以下代码。 Note that this might show a "Select application" dialog, when the user didn't choose a default for a protocol yet. 请注意,当用户尚未为协议选择默认值时,这可能会显示“选择应用程序”对话框。
var uri = Services.io.newURI("https://google.com/", null, null);
var eps = Cc["@mozilla.org/uriloader/external-protocol-service;1"]
.getService(Ci.nsIExternalProtocolService);
// You're allowed to omit the second parameter if you don't have a window.
eps.loadURI(uri, window);
If Firefox can find an OS default handler for a particular protocol, then the code will launch that default handler without user interaction , meaning you should be extra careful not to launch arbitrary URIs that might do harm (eg vbscript:...
)! 如果Firefox可以找到特定协议的操作系统默认处理程序,则代码将在没有用户交互的情况下启动该默认处理程序,这意味着您应格外小心,不要启动可能有害的任意URI(例如vbscript:...
)!
var uri = Services.io.newURI("https://google.com/", null, null);
var eps = Cc["@mozilla.org/uriloader/external-protocol-service;1"]
.getService(Ci.nsIExternalProtocolService);
var found = {};
var handler = eps.getProtocolHandlerInfoFromOS(uri.scheme, found);
if (found.value && handler && handler.hasDefaultHandler) {
handler.preferredAction = Ci.nsIHandlerInfo.useSystemDefault;
// You're allowed to omit the second parameter if you don't have a window.
handler.launchWithURI(uri, window);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.