Revision 510085 of system

  • Revision slug: Mozilla/Add-ons/SDK/High-Level_APIs/system
  • Revision title: system
  • Revision id: 510085
  • Created:
  • Creator: wbamberg
  • Is current revision? No
  • Comment
Tags: 

Revision Content

Unstable

Query the add-on's environment and access arguments passed to it.

Usage

Querying Your Environment

Using the system module you can access environment variables (such as PATH), find out which operating system your add-on is running on and get information about the host application (for example, Firefox or Fennec), such as its version.

var system = require("sdk/system");
// PATH environment variable
console.log(system.env.PATH);
// operating system
console.log("platform = " + system.platform);
// processor architecture
console.log("architecture = " + system.architecture);
// compiler used to build host application
console.log("compiler = " + system.compiler);
// host application build identifier
console.log("build = " + system.build);
// host application UUID
console.log("id = " + system.id);
// host application name
console.log("name = " + system.name);
// host application version
console.log("version = " + system.version);
// host application vendor
console.log("vendor = " + system.vendor);
// host application profile directory
console.log("profile directory = " + system.pathFor("ProfD"));

Accessing --static-args

Static arguments are accessible by name as properties of the staticArgs property.

var system = require("sdk/system");
console.log(system.staticArgs.foo);

Quit the host application

To quit the host application, use the exit() function.

var system = require("sdk/system");
system.exit();

Globals

Functions

exit(code)

Quits the host application with the specified code. If code is omitted, exit() uses the success code 0. To exit with failure use 1.

var system = require("sdk/system");
system.exit();
Parameters

code : integer
To exit with failure, set this to 1. To exit with success, omit this argument.

pathFor(id)

Firefox enables you to get the path to certain "special" directories, such as the desktop or the profile directory. This function exposes that functionality to add-on authors.

For the full list of "special" directories and their IDs, see "Getting_files in special directories".

For example:

// get Firefox profile path
var profilePath = require('sdk/system').pathFor('ProfD');
// get OS temp files directory (/tmp)
var temps = require('sdk/system').pathFor('TmpD');
// get OS desktop path for an active user (~/Desktop on linux
// or C:\Documents and Settings\username\Desktop on windows).
var desktopPath = require('sdk/system').pathFor('Desk');
Parameters

id : String
The ID of the special directory.

Returns

String : The path to the directory.

Properties

staticArgs

The JSON object that was passed via the cfx --static-args option.

For example, suppose your add-on includes code like this:

var system = require("sdk/system");
console.log(system.staticArgs.foo);

If you pass it a static argument named "foo" using --static-args, then the value of "foo" will be written to the console:

(addon-sdk)~/my-addons/system > cfx run --static-args="{ \"foo\": \"Hello\" }"
Using binary at '/Applications/Firefox.app/Contents/MacOS/firefox-bin'.
Using profile at '/var/folders/me/DVFDGavr5GDFGDtU/-Tmp-/tmpOCTgL3.mozrunner'.
info: system: Hello

env

This object provides access to environment variables.

You can get the value of an environment variable by accessing the property with that name:

var system = require("sdk/system");
console.log(system.env.PATH);

You can test whether a variable exists by checking whether a property with that name exists:

var system = require("sdk/system");
if ('PATH' in system.env) {
  console.log("PATH is set");
}

You can set a variable by setting the property:

var system = require("sdk/system");
system.env.FOO = "bar";
console.log(system.env.FOO);

You can unset a variable by deleting the property:

var system = require("sdk/system");
delete system.env.FOO;

You can't enumerate environment variables.

platform

The type of operating system you're running on. This will be one of the values listed as OS_TARGET, converted to lower case.

var system = require("sdk/system");
console.log("platform = " + system.platform);

architecture

The type of processor architecture you're running on. This will be one of: "arm"``,"ia32", or"x64"`.

var system = require("sdk/system");
console.log("architecture = " + system.architecture);

compiler

The type of compiler used to build the host application. For example: "msvc", "n32", "gcc2", "gcc3", "sunc", "ibmc"

var system = require("sdk/system");
console.log("compiler = " + system.compiler);

build

An identifier for the specific build, derived from the build date. This is useful if you're trying to target individual nightly builds. See nsIXULAppInfo's appBuildID.

var system = require("sdk/system");
console.log("build = " + system.build);

id

The UUID for the host application. For example, "{ec8030f7-c20a-464f-9b0e-13a3a9e97384}" for Firefox. This has traditionally been in the form "{AAAAAAAA-BBBB-CCCC-DDDD-EEEEEEEEEEEE}" but for some applications it may be in the form "appname@vendor.tld".

See nsIXULAppInfo's ID.

var system = require("sdk/system");
console.log("id = " + system.id);

name

The human-readable name for the host application. For example, "Firefox".

var system = require("sdk/system");
console.log("name = " + system.name);

version

The version of the host application.

See nsIXULAppInfo's version.

var system = require("sdk/system");
console.log("version = " + system.version);

platformVersion

The version of XULRunner that underlies the host application.

See nsIXULAppInfo's platformVersion.

var system = require("sdk/system");
console.log("XULRunner version = " + system.platformVersion);

vendor

The name of the host application's vendor, for example: "Mozilla".

var system = require("sdk/system");
console.log("vendor = " + system.vendor);

Revision Source

<div class="note">
 <p>Unstable</p>
</div>
<p><span class="seoSummary">Query the add-on's environment and access arguments passed to it.</span></p>
<h2 id="Usage">Usage</h2>
<h3 id="Querying_Your_Environment">Querying Your Environment</h3>
<p>Using the <code>system</code> module you can access environment variables (such as <code>PATH</code>), find out which operating system your add-on is running on and get information about the host application (for example, Firefox or Fennec), such as its version.</p>
<pre class="brush: js">
var system = require("sdk/system");
// PATH environment variable
console.log(system.env.PATH);
// operating system
console.log("platform = " + system.platform);
// processor architecture
console.log("architecture = " + system.architecture);
// compiler used to build host application
console.log("compiler = " + system.compiler);
// host application build identifier
console.log("build = " + system.build);
// host application UUID
console.log("id = " + system.id);
// host application name
console.log("name = " + system.name);
// host application version
console.log("version = " + system.version);
// host application vendor
console.log("vendor = " + system.vendor);
// host application profile directory
console.log("profile directory = " + system.pathFor("ProfD"));</pre>
<h3 id="Accessing_--static-args">Accessing --static-args</h3>
<p>Static arguments are accessible by name as properties of the <a href="/en-US/Add-ons/SDK/High-Level_APIs/system#staticArgs"><code>staticArgs</code></a> property.</p>
<pre class="brush: js">
var system = require("sdk/system");
console.log(system.staticArgs.foo);</pre>
<h3 id="Quit_the_host_application">Quit the host application</h3>
<p>To quit the host application, use the <a href="/en-US/Add-ons/SDK/High-Level_APIs/system#exit(code)"><code>exit()</code></a> function.</p>
<pre class="brush: js">
var system = require("sdk/system");
system.exit();</pre>
<h2 id="Globals">Globals</h2>
<h3 id="Functions">Functions</h3>
<h4 class="addon-sdk-api-name" id="exit(code)"><code>exit(code)</code></h4>
<p>Quits the host application with the specified <code>code</code>. If <code>code</code> is omitted, <code>exit()</code> uses the success code <code>0</code>. To exit with failure use <code>1</code>.</p>
<pre class="brush: js">
var system = require("sdk/system");
system.exit();</pre>
<h5 id="Parameters">Parameters</h5>
<p><strong>code : integer</strong><br />
 To exit with failure, set this to <code>1</code>. To exit with success, omit this argument.</p>
<h4 class="addon-sdk-api-name" id="pathFor(id)"><code>pathFor(id)</code></h4>
<p>Firefox enables you to get the path to certain "special" directories, such as the desktop or the profile directory. This function exposes that functionality to add-on authors.</p>
<p>For the full list of "special" directories and their IDs, see <a href="https://developer.mozilla.org/en-US/docs/Code_snippets/File_I_O#Getting_files_in_special_directories">"Getting_files in special directories"</a>.</p>
<p>For example:</p>
<pre class="brush: js">
// get Firefox profile path
var profilePath = require('sdk/system').pathFor('ProfD');
// get OS temp files directory (/tmp)
var temps = require('sdk/system').pathFor('TmpD');
// get OS desktop path for an active user (~/Desktop on linux
// or C:\Documents and Settings\username\Desktop on windows).
var desktopPath = require('sdk/system').pathFor('Desk');</pre>
<h5 id="Parameters">Parameters</h5>
<p><strong>id : String</strong><br />
 The ID of the special directory.</p>
<h5 id="Returns">Returns</h5>
<p><strong>String</strong> : The path to the directory.</p>
<h3 id="Properties">Properties</h3>
<h4 class="addon-sdk-api-name" id="staticArgs"><code>staticArgs</code></h4>
<p>The JSON object that was passed via the <a href="/en-US/Add-ons/SDK/Tools/cfx#arguments"><code>cfx --static-args</code> option</a>.</p>
<p>For example, suppose your add-on includes code like this:</p>
<pre class="brush: js">
var system = require("sdk/system");
console.log(system.staticArgs.foo);</pre>
<p>If you pass it a static argument named "foo" using <code>--static-args</code>, then the value of "foo" will be written to the console:</p>
<pre>
(addon-sdk)~/my-addons/system &gt; cfx run --static-args="{ \"foo\": \"Hello\" }"
Using binary at '/Applications/Firefox.app/Contents/MacOS/firefox-bin'.
Using profile at '/var/folders/me/DVFDGavr5GDFGDtU/-Tmp-/tmpOCTgL3.mozrunner'.
info: system: Hello
</pre>
<h4 class="addon-sdk-api-name" id="env"><code>env</code></h4>
<p>This object provides access to environment variables.</p>
<p>You can get the value of an environment variable by accessing the property with that name:</p>
<pre class="brush: js">
var system = require("sdk/system");
console.log(system.env.PATH);</pre>
<p>You can test whether a variable exists by checking whether a property with that name exists:</p>
<pre class="brush: js">
var system = require("sdk/system");
if ('PATH' in system.env) {
  console.log("PATH is set");
}</pre>
<p>You can set a variable by setting the property:</p>
<pre class="brush: js">
var system = require("sdk/system");
system.env.FOO = "bar";
console.log(system.env.FOO);</pre>
<p>You can unset a variable by deleting the property:</p>
<pre class="brush: js">
var system = require("sdk/system");
delete system.env.FOO;</pre>
<p>You <strong>can't</strong> enumerate environment variables.</p>
<h4 class="addon-sdk-api-name" id="platform"><code>platform</code></h4>
<p>The type of operating system you're running on. This will be one of the values listed as <a href="https://developer.mozilla.org/en-US/docs/OS_TARGET">OS_TARGET</a>, converted to lower case.</p>
<pre class="brush: js">
var system = require("sdk/system");
console.log("platform = " + system.platform);</pre>
<h4 class="addon-sdk-api-name" id="architecture"><code>architecture</code></h4>
<p>The type of processor architecture you're running on. This will be one of: <code>"arm"``,</code>"ia32"<code>, or</code>"x64"`.</p>
<pre class="brush: js">
var system = require("sdk/system");
console.log("architecture = " + system.architecture);</pre>
<h4 class="addon-sdk-api-name" id="compiler"><code>compiler</code></h4>
<p>The type of compiler used to build the host application. For example: <code>"msvc"</code>, <code>"n32"</code>, <code>"gcc2"</code>, <code>"gcc3"</code>, <code>"sunc"</code>, <code>"ibmc"</code></p>
<pre class="brush: js">
var system = require("sdk/system");
console.log("compiler = " + system.compiler);</pre>
<h4 class="addon-sdk-api-name" id="build"><code>build</code></h4>
<p>An identifier for the specific build, derived from the build date. This is useful if you're trying to target individual nightly builds. See <a href="https://developer.mozilla.org/en-US/docs/Using_nsIXULAppInfo#Version">nsIXULAppInfo's <code>appBuildID</code></a>.</p>
<pre class="brush: js">
var system = require("sdk/system");
console.log("build = " + system.build);</pre>
<h4 class="addon-sdk-api-name" id="id"><code>id</code></h4>
<p>The UUID for the host application. For example, <code>"{ec8030f7-c20a-464f-9b0e-13a3a9e97384}"</code> for Firefox. This has traditionally been in the form <code>"{AAAAAAAA-BBBB-CCCC-DDDD-EEEEEEEEEEEE}"</code> but for some applications it may be in the form <code>"appname@vendor.tld"</code>.</p>
<p>See <a href="https://developer.mozilla.org/en-US/docs/Using_nsIXULAppInfo#ID">nsIXULAppInfo's <code>ID</code></a>.</p>
<pre class="brush: js">
var system = require("sdk/system");
console.log("id = " + system.id);</pre>
<h4 class="addon-sdk-api-name" id="name"><code>name</code></h4>
<p>The human-readable name for the host application. For example, "Firefox".</p>
<pre class="brush: js">
var system = require("sdk/system");
console.log("name = " + system.name);</pre>
<h4 class="addon-sdk-api-name" id="version"><code>version</code></h4>
<p>The version of the host application.</p>
<p>See <a href="https://developer.mozilla.org/en-US/docs/Using_nsIXULAppInfo#Version">nsIXULAppInfo's <code>version</code></a>.</p>
<pre class="brush: js">
var system = require("sdk/system");
console.log("version = " + system.version);</pre>
<h4 class="addon-sdk-api-name" id="platformVersion"><code>platformVersion</code></h4>
<p>The version of XULRunner that underlies the host application.</p>
<p>See <a href="https://developer.mozilla.org/en-US/docs/Using_nsIXULAppInfo#Platform_version">nsIXULAppInfo's <code>platformVersion</code></a>.</p>
<pre class="brush: js">
var system = require("sdk/system");
console.log("XULRunner version = " + system.platformVersion);</pre>
<h4 class="addon-sdk-api-name" id="vendor"><code>vendor</code></h4>
<p>The name of the host application's vendor, for example: <code>"Mozilla"</code>.</p>
<pre class="brush: js">
var system = require("sdk/system");
console.log("vendor = " + system.vendor);</pre>
Revert to this revision