Running applications: Difference between revisions

From MozillaZine Knowledge Base
Jump to navigationJump to search
(Oops in code)
mNo edit summary
 
(8 intermediate revisions by 3 users not shown)
Line 1: Line 1:
==Runing programs==
Moved to [http://developer.mozilla.org/en/docs/Code_snippets:Running_applications MDC]
This page describes how to run other programs from your JavaScript code, using Mozilla XPCOM interfaces.
[[Category:Redirects]]
 
There are two ways to run external programs. The first is to use [http://xulplanet.com/references/xpcomref/ifaces/nsILocalFile.html nsILocalFile::launch()] method, the second is to use [http://xulplanet.com/references/xpcomref/ifaces/nsIProcess.html nsIProcess] interface.
 
===nsILocalFile::launch()===
This method has the same effect as if you double clicked the file, so for executable files, it will just run the file without any parameters. It may not be implemented on some platforms, so make sure your this method is implemented on your target platforms. It seems to be implemented on Windows.
 
For more information on nsIFile/nsILocalFile, see [[Dev : Extensions : Example Code : File IO | File IO]].
 
===nsIProcess===
The recommended way is to use [http://xulplanet.com/references/xpcomref/ifaces/nsIProcess.html nsIProcess] interface. It is as easy as:
<pre>// create an nsILocalFile for the executable
var file = Components.classes["@mozilla.org/file/local;1"]
                            .getService(Components.interfaces.nsILocalFile);
file.initWithPath("c:\\myapp.exe");
 
// create an nsIProcess
var process = Components.classes["@mozilla.org/process/util;1"]
                            .getService(Components.interfaces.nsIProcess);
process.init(file);
 
// Run the process.
// If first param is true, calling process will be blocked until
// called process terminates.
// Second and third params are used to pass command-line arguments
// to the process.
var args = ["argument1", "argument2"];
process.run(false, args, args.length);</pre>
 
==References==
*[http://xulplanet.com/references/xpcomref/ifaces/nsILocalFile.html nsILocalFile interface]
*[http://xulplanet.com/references/xpcomref/ifaces/nsIProcess.html nsIProcess interface]

Latest revision as of 14:15, 9 November 2006

Moved to MDC