# BeanShell Commands

BeanShell includes a set of commands; subroutines that can be called from any script or macro. The following is a summary of those commands which may be useful within jEdit.

Java classes in plugins cannot make use of BeanShell commands directly. However, these commands can be called from BeanShell code that is part of a plugin, for example the snippets in `actions.xml`, or any BeanShell scripts shipped with the plugin and loaded on startup.

## Output Commands

- void `cat` String filename

  Writes the contents of filename to the activity log.

- void `javap` String | Object | Class target

  Writes the public fields and methods of the specified class to the output stream of the current process. Requires Java 2 version 1.3 or greater.

- void `print` arg

  Writes the string value of the argument to the activity log, or if run from the Console plugin, to the current output window. If arg is an array, `print` runs itself recursively on the array's elements.

## File Management Commands

- void `cd` String dirname

  Changes the working directory of the BeanShell interpreter to dirname.

- void `cp` String fromFile String toFile

  Copy fromFile to toFile.

- void `dir` String dirname

  Displays the contents of directory dirname. The format of the display is similar to the Unix `ls -l` command.

- void `mv` String fromFile String toFile

  Moves the file named by fromFile to toFile.

- File `pathToFile` String filename

  Create a File object corresponding to `filename`. Relative paths are resolved with reference to the BeanShell interpreter's working directory.

- void `pwd`

  Writes the current working directory of the BeanShell interpreter to the output stream of the current process.

- void `rm` String pathname

  Deletes the file name by pathname.

## Component Commands

- JFrame `frame` Component frame

  Displays the component in a top-level JFrame, centered and packed. Returns the JFrame object.

- Object `load` String filename

  Loads and returns a serialized Java object from filename.

- void `save` Component component String filename

  Saves component in serialized form to filename.

- Font `setFont` Component comp int ptsize

  Set the font size of component to ptsize and returns the new font.

## Resource Management Commands

- URL `getResource` String path

  Returns the resource specified by path. An absolute path must be used to return any resource available in the current classpath.

## Script Execution Commands

- Thread `bg` String filename

  Run the BeanShell script named by filename in a copy of the existing namespace and in a separate thread. Returns the Thread object so created.

- void `exec` String cmdline

  Start the external process by calling `Runtime.exec()` on cmdline. Any output is directed to the output stream of the calling process.

- Object `eval` String expression

  Evaluates the string expression as a BeanShell script in the interpreter's current namespace. Returns the result of the evaluation of `null`.

- org.gjt.sp.jedit.bsh.This `run` String filename

  Run the BeanShell script named by filename in a copy of the existing namespace. The return value represent the object context of the script, allowing you to access its variables and methods.

- void `setAccessibility` boolean flag

  If flag is `true`, BeanShell scripts are allowed to change and modify private variables, and call private methods. The default is `false`.

- void `setStrictJava` boolean flag

  If flag is `true`, BeanShell scripts must follow a much more strict, Java-like syntax, and are not able to use the convenience features described in .

- void `source` String filename

  Evaluates the contents of filename as a BeanShell script in the interpreter's current namespace.

## BeanShell Object Management Commands

- `bind` org.gjt.sp.jedit.bsh.This ths org.gjt.sp.jedit.bsh.Namespace namespace

  Binds the scripted object ths to namespace.

- void `clear`

  Clear all variables, methods, and imports from this namespace. If this namespace is the root, it will be reset to the default imports.

- org.gjt.sp.jedit.bsh.This `extend` org.gjt.sp.jedit.bsh.This object

  Creates a new BeanShell This scripted object that is a child of the parameter object.

- void `importObject` Object object

  Import an object into this namespace. This is somewhat similar to Java 1.5 static class imports, except you can import the methods and fields of a Java object instance into a BeanShell namespace, for example:

      Map map = new HashMap();
          importObject( map );
          put("foo", "bar");
          print( get("foo") ); // "bar"

- org.gjt.sp.jedit.bsh.This `object`

  Creates a new BeanShell This scripted object which can hold data members. You can use this to create an object for storing miscellaneous crufties, like so:

      crufties = object();
      crufties.foo = "hello world";
      crufties.counter = 5;
      ...

- `setNameSpace` org.gjt.sp.jedit.bsh.Namespace namespace

  Set the namespace of the current scope to namespace.

- org.gjt.sp.jedit.bsh.This `super` String scopename

  Returns a reference to the BeanShell This object representing the enclosing method scope specified by scopename. This method work similar to the `super` keyword but can refer to enclosing scope at higher levels in a hierarchy of scopes.

- void `unset` String name

  Removes the variable named by name from the current interpreter namespace. This has the effect of undefining the variable.

## Other Commands

- void `debug`

  Toggles BeanShell's internal debug reporting to the output stream of the current process.

- `getSourceFileInfo`

  Returns the name of the file or other source from which the BeanShell interpreter is reading.