# XULRunner入门

## 步骤1：下载XULRunner

Mac版本的XULRunner被发布成tar.bz2格式的归档文件。你可以解压缩到任何你喜欢的位置，但在本文档中的多个位置都假设了你已经将解压缩得到的文件放置在了/Library/Frameworks目录中。

FIREFOX_VERSION=grep -Po  "\d{2}\.\d+" /usr/lib/firefox/platform.ini ARCH=uname -p XURL=https://ftp.mozilla.org/pub/mozilla.org/xulrunner/releases/$FIREFOX_VERSION/runtimes/xulrunner-$FIREFOX_VERSION.en-US.linux-$ARCH.tar.bz2 cd /opt sudo sh -c "wget -O-$XURL | tar -xj" sudo ln -s /opt/xulrunner/xulrunner /usr/bin/xulrunner sudo ln -s /opt/xulrunner/xpcshell /usr/bin/xpcshell 

## 步骤2：安装XULRunner

Optionally, if you've downloaded the compressed archive of XULRunner and would like to install it on your system you can do so by running作为可选的步骤，如果你已经下载了XULRunner的压缩文档并想将其安装到你的系统中，你可以运行以下命令来做到这一点

xulrunner --register-global

作为管理名以上命令会将XULRunner注册给本机上的所有用户。仅注册给当前用户可以运行以下命令

xulrunner --register-user

In all systems you should unzip the omni.ja file into some example directory and take a look at all the awesome! First change the file extension to zip and then use your normal filesystem's decompression tool to open it up. The contents of omni.ja are available to XULRunner applications and are what make it possible to build amazing applications easily!

## 步骤3：创建应用程序目录结构

Hint: Skip ahead and download the sample application, you can experiment with it while following this tutorial. You can download the sample application from https://github.com/matthewkastor/XULRunner-Examples. Please continue reading to learn the "what", "why" and "how" parts of building a XULRunner application.

+ myapp/
|
+-+ chrome/
| |
| +-+ content/
| | |
| | +-- main.xul
| | |
| | +-- main.js
| |
| +-- chrome.manifest
|
+-+ defaults/
| |
| +-+ preferences/
|   |
|   +-- prefs.js
|
+-- application.ini
|
+-- chrome.manifest


Note: In XULRunner 2.0, the chrome.manifest is now used to register XPCOM components in addition to its previous uses. Part of this change means the /chrome/chrome.manifest is no longer considered the "root" manifest. XULRunner will not check that folder location for a root-level chrome.manifest. You need to move your existing chrome.manifest to the application root folder, remembering to update the relative paths within the file. You could also just create a new application root-level manifest that includes the /chrome/chrome.manifest, which is what this tutorial will do.

## 步骤4：设置 application.ini

application.ini 文件为你的应用扮演着XULRunner入口点的角色。它指定了你的应用打算如何使用XULRunner平台以及配置一些信息以告诉XULRunner如何运行你的程序。下面是我的文件内容：

[App]
Vendor=XULTest
Name=myapp
Version=1.0
BuildID=20100901
ID=xulapp@xultest.org

[Gecko]
MinVersion=1.8
MaxVersion=200.*

Note: MinVersion 和MaxVersion字段指示了你的应用所兼容的Gecko版本的范围；确保你设置了它们而且你所使用的XULRunner的版本也是在这个范围之内，否则你的应用将不能工作。
Note: 确保你的应用名称是小写的，并且长度大于3个字符。

## 步骤5：设置chrome清单文件

The chrome manifest file is used by XULRunner to define specific URIs which in turn are used to locate application resources. This will become clearer when we see how the “chrome://” URI is used. Application chrome can be in a single or a few JAR files or uncompressed as folders and files. I am using the uncompressed method for now. Here is the chrome/chrome.manifest:

 content myapp content/


As mentioned in Step 3, the default location of the chrome.manifest has changed in XULRunner 2.0, so we also need a simple chrome.manifest in the application root which will include the the manifest in our chrome root. Here is the application root chrome.manifest:

manifest chrome/chrome.manifest

## 步骤6：设置配置

The prefs.js file tells XULRunner the name of the XUL file to use as the main window. Here is mine:

pref("toolkit.defaultChromeURI", "chrome://myapp/content/main.xul");

/* debugging prefs, disable these before you deploy your application! */
pref("browser.dom.window.dump.enabled", true);
pref("javascript.options.showInConsole", true);
pref("javascript.options.strict", true);
pref("nglayout.debug.disable_xul_cache", true);


XULRunner specific preferences include:

toolkit.defaultChromeURI
Specifies the default window to open when the application is launched.
toolkit.defaultChromeFeatures
Specifies the features passed to window.open() when the main application window is opened.
toolkit.singletonWindowType
Allows configuring the application to allow only one instance at a time.

The toolkit preferences are described in further detail in XULRunner:Specifying Startup Chrome Window.

The debugging preferences are discussed in Debugging a XULRunner Application

## 步骤7：创建一些XUL

Finally, we need to create a simple XUL window, which is described in the file main.xul. Nothing fancy here, just the minimum we need to make a window. No menus or anything.

main.xul:

<?xml version="1.0"?>

<?xml-stylesheet href="chrome://global/skin/" type="text/css"?>

<window id="main" title="My App" width="300" height="300" xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul">

<script type="application/javascript" src="chrome://myapp/content/main.js"/>

<caption label="Hello World"/>
<separator/>
<button label="More >>" oncommand="showMore();"/>
<separator/>
<description id="more-text" hidden="true">This is a simple XULRunner application. XUL is simple to use and quite powerful and can even be used on mobile devices.</description>

</window>

Note: Make sure there is no extra whitespace at the beginning of the XML/XUL file

The application also has a JavaScript file. Most XUL applications will include some external JavaScript, so the sample application does too, just to show how to include it into the XUL file.

main.js:

function showMore() {
document.getElementById("more-text").hidden = false;
}


## 步骤8：运行应用程序

The moment of truth. We need to get XULRunner to launch the bare-bones application.

### Windows

From a Windows command prompt opened to the myapp folder, we should be able to execute this:

 C:\path\to\xulrunner.exe application.ini


Of course, if you opted to install xulrunner then you could simply do

​%ProgramFiles%\xulrunner.exe application.ini

or on 64 bit systems

​%ProgramFiles(x86)%\xulrunner.exe application.ini

Note: you can also install your application when you're finished debugging it. See XUL Application Packaging for details.

### Mac

On the Mac, before you can run a XULRunner application with everything intact, you must install it using the --install-app xulrunner commandline flag. Installing the application creates an OS X application bundle:

 /Library/Frameworks/XUL.framework/xulrunner-bin --install-app /<path>/<to>/myapp.zip


Once installed, you can run the application:

 /Library/Frameworks/XUL.framework/xulrunner-bin "/Applications/Finkle/TestApp.app/Contents/Resources/application.ini"


You may run it without installing (but with the menu bar and dock icon missing) in OS X by typing:

/Library/Frameworks/XUL.framework/xulrunner-bin "/<full path>/TestApp/application.ini"


Note: The full path is required or a "Error: couldn't parse application.ini."-message will be returned.

This might also be simplified using a very simple shell script (i call mine "run.sh"):

#!/bin/sh
/Library/Frameworks/XUL.framework/xulrunner-bin pwd/application.ini


### Linux

On Ubuntu, you can run the application from a terminal. First change into the \myapp folder, then start the application by:

 xulrunner application.ini


You should now see a window that looks something like this. This particular screenshot is from Ubuntu 10.

### Alternative: 通过Firefox运行XUL应用

With Firefox 3 and later, you can tell the Firefox executable to run a XUL application from the command line. The XUL application will run instead of the Firefox browser that normally starts. This is similar to starting a XUL app using XULRunner. See Using Firefox to run XULRunner applications. This does not work if Firefox itself was installed as a XUL app - you need to use the installed XULRunner directly.