Files
最接近的是nsIFile::launch https://developer.mozilla.org/en-US/docs/Mozilla/Tech/XPCOM/Reference/Interface/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) {
// 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.
}
您当然可以创建一个nsIFile
也来自“原始”路径的实例,例如(我在 OSX 上):
var file = Cc["@mozilla.org/file/local;1"].createInstance(Ci.nsIFile);
Cc
and Ci
是捷径Components.classes
and Components.interfaces
大多数 Mozilla 和附加组件代码都使用它。在附加 SDK 中,您可以通过以下方式获取这些内容:Chrome 权威 https://developer.mozilla.org/en-US/Add-ons/SDK/Tutorials/Chrome_Authority.
URIs
Edit我完全忘记了ShellExcute
还将处理 URL。顺便说一句,您确实只询问了“文件类型”。
无论如何,要打开一个随机 URI,您可以使用nsIExternalProtocolService
.
选项 1 - 使用默认处理程序启动(不一定是操作系统处理程序)
要使用默认处理程序(也可以是 Web 协议处理程序或类似处理程序)启动,您可以使用以下代码。请注意,当用户尚未选择协议的默认值时,这可能会显示“选择应用程序”对话框。
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);
选项 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;
// You're allowed to omit the second parameter if you don't have a window.
handler.launchWithURI(uri, window);
}