Home
Categories
Dictionnary
Download
Project Details
Changes Log
FAQ
License

Script helper



A global helper is available in all the scripts.

The script helper is available through the helper field in your script.

For example to get the current widget in a Groovy script:

      public void clicked() {
         XULWidget widget = helper.getCurrentWidget();
      }

Helper API

By default the helper has the following methods:

public class ScriptHelper


Modifier and Type Method and Description
boolean dispose()
Dispose the current window parent frame or window
Object evalScript(String method, Object... args)
Evaluate a script method
Object evalScript(String method)
Evaluate a script method without arguments
boolean exit()
Exit the application
ScriptContext getContext()
Return the script context
XULDocument getCurrentDocument()
Return the current document
XULWidget getCurrentWidget()
Return the current widget
String getText(File file)
Return the text associated with a text File
boolean setText(File file, String text)
Set the text for a text File

Evaluating script methods

The evalScript(String method, Object... args) andevalScript(String method) methods will evaluate a method or function for any script referenced in the same XUL document as the script (for embedded scripts of external scripts).

For example, the following XUL file is valid:

      <window id="Hello" title="Hello" orient="horizontal" width="250" height="100"  
      xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul">        
          <script>
      function clicked(){
      var value = compute();
        alert('Hello World...' + value);
      }
          </script>
          <script>
      function compute(){
        return 10;
      }
          </script>                   
          <button label="Push Me!" oncommand="clicked()" />
      </window>      

Returning the current document or widget

The getCurrentDocument() and getCurrentWidget() will return the XUL document or widget from which originates the last event. It will return null before any event have been triggered.

For example to get the current widget in a Groovy script:

      public void clicked() {
         XULWidget widget = helper.getCurrentWidget();
      }

Calling dispose and exit


The dispose() method allows to dispose of the current XUL document parent frame or window. it is only allowed if the ScriptManagerProperties.ALLOW_DISPOSE has been set to true.

The exit() method allows to call System.exit(0). it is only allowed if the ScriptManagerProperties.ALLOW_EXIT has been set to true.

For example in Javascript:

      function exit() {
        helper.exit();
      }       

Using a custom helper

Main Article: ScriptContext

To define a custom XULScriptHelper, you should define a custom XULScriptContext and implement the XULScriptContext.getHelper() with your own XULScriptHelper implementation.

Note that:

See also


Categories: scripts

Copyright 2008-2020 Herve Girod. All Rights Reserved. Documentation and source under the LGPL licence