Using JavaScript

jGnash allows you to create and run JavaScript programs inside jGnash. The internals of the jGnash engine and some user interface functions can be accessed to create custom reports, create and modify transactions, etc.

Running a JavaScript program is as simple as using ToolsRun JavaScript command from the menu bar.

Below is an example JavaScript program that displays the accounts in a loaded file and demonstrates how to display a simple dialog. To try the program, create a text file with a name of you choice that ends with a .js extension. After creating the file, simply using the ToolsRun JavaScript command to select the program and run it.

importPackage(javax.swing);
importPackage(Packages.jgnash.ui);
importPackage(Packages.jgnash.engine);

// helper function to print messages to the console
function debug(message) {   
    java.lang.System.out.println(message);
}

// show the console dialog to see the debug information
UIApplication.getFrame().showConsoleWindow();   

// this is how to get the default Engine instance
var engine = EngineFactory.getEngine(EngineFactory.DEFAULT);    

// get a list of accounts
var accountList = engine.getAccountList();  

// loop and print the account names to the console
for (i = 0; i < accountList.size(); i++)   
{
    var account = accountList.get(i);
    debug(account.getName());
}

// just to show how to use Swing
var optionPane = JOptionPane.showMessageDialog(null, 'Hello, world!');