从Firefox插件PerFom ShellExecute [英] Perfom a ShellExecute from Firefox Addon

查看:658
本文介绍了从Firefox插件PerFom ShellExecute的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的Firefox扩展中,我想在Windows中使用默认查看器打开某些文件。所以基本上类似于Windows API的 ShellExecute('OPEN')函数调用。可能吗?如果是这样的话,那怎么能实现呢?

最接近的是 nsIFile ::发布 。但是,并没有为每个可以想象的平台实现(但至少对于Windows,OSX,GTK / Gnome和兼容的KDE和Android来说)。

你不能使用 :: launch 来指示操作系统(特别是Windows)使用 open 以外的动词,没有等同于例如 ShellExecute(...,edit,...)



使用它:

  try {
var file = Services.dirsvc.get(Desk,Ci.nsIFile) ;
file.append(screenshot.png);
file.launch();
}
catch(ex){
//由于操作系统返回错误
//或者文件不存在,
//或者这个函数根本就没有为特定的平台实现。

你当然可以创建 nsIFile 从原始路径的实例,例如(我在OSX上):

  var file = Cc [@ mozilla.org/file/local;1] .createInstance(Ci.nsIFile); 

Cc Ci Components.classes 和 Components.interfaces 的快捷键,大部分的mozilla和附加代码使用。在附加SDK中,您可以通过 Chrome Authority

URI



编辑我完全忘了 ShellExcute 也会处理URL。而且你只要求问一下文件类型,BTW。

无论如何,打开一个随机的URI,你可以使用 nsIExternalProtocolService



选项1 - 使用默认处理程序(不一定是OS处理程序)启动

<要使用默认处理程序(也可以是Web协议处理程序或类似程序)启动,可以使用以下代码。请注意,这可能会显示一个选择应用程序对话框,当用户没有选择一个协议的默认值。

  var uri = Services.io.newURI(https://google.com/,null,null); 
var eps = Cc [@ mozilla.org/uriloader/external-protocol-service;1]
.getService(Ci.nsIExternalProtocolService);
//如果你没有窗口,你可以省略第二个参数。
eps.loadURI(uri,window);



选项2 - 使用操作系统默认处理程序(如果有的话)启动

如果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;
//如果你没有窗口,你可以省略第二个参数。
handler.launchWithURI(uri,window);
}


In my Firefox extensions I want to open certain files with the "default viewer" for that file type in Windows. So basically something similar to the ShellExecute('OPEN') function call of the Windows API. Is it possible? If so, how can that be achieved?

解决方案

Files

The closest thing is 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).

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 e.g. 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, e.g. (I'm on 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. In the Add-on SDK you can get these via the Chrome Authority.

URIs

Edit I totally forgot that ShellExcute will also handle URLs. And you did ask only about "file type[s]", BTW.

Anyway, to open a random URI, you can use the nsIExternalProtocolService.

Option 1 - Launch with the default handler (not necessarily OS handler)

To launch with a default handler, which could also be a web protocol handler or similar, you can use the following code. 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);

Option 2 - Launch with the OS default handler, if any

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

这篇关于从Firefox插件PerFom ShellExecute的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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