Edit

Scripts

Scripts allow you to automate operations within MoneyWorks. They can read external files (either from your local volumes, or from a web server), intercept user-interface actions, create and update records and more.

A number of sample scripts are included in the Acme Widgets sample file. These are turned off by default, but you can easily turn them on to see how scripts work, and to play with the scripting language.

You can access the scripts by:

  1. Choose Show>Scripts

The Scripts Management window will open.

The first time you do this the scripts window will be empty. In the example above, there are two scripts (listed down the side). The “Test” scripts is activated (indicated by the green tick), whereas the “Pricebook” script is deactivated, so will not run (indicated by the red cross).

To view a particular script:

  1. Click on the script in the sidebar

The script contents will be displayed in the body of the window, where it can be edited. If you do edit a script, you will need to activate it again (see below).

To activate a script

  1. Select the script in the sidebar by clicking it (if it is not already selected)
  1. Click the Activate toolbar icon

The script will be compiled, and (if there are no errors), its Load handler will be called. Normally a script will also have other handlers that MoneyWorks will call.

After loading, a green tick will appear next to the script on the sidebar indicating that it has been activated. If there is a syntax error in the script (i.e. some invalid text that the script compiler doesn't recognise), the script will not activate, and a tooltip will alert you to the error and where it occurs (and the part the script at and following the error will be coloured orange).

To deactivate a script

  1. If the script is not the currently selected one, click on it in the sidebar
  1. Click the Deactivate toolbar icon

If the script has an Unload handler, it will be called.

After the script is unloaded, a red cross will appear next to the script's name in the sidebar indicating that it is no longer active.

Note: If the script contains a public handler, any other scripts that use that handler will also be deactivated.

"Running" scripts

Usually, scripts will implement Handlers that allow the script to respond to certain user actions (like opening a window for a record, clicking a custom toolbar icon, or tabbing out of a particular field). The Load handler can be considered the main entry point of a script in that Load is called when you activate the script or the script is auto-activated when you log in to the document.

If you want a script to just do one thing immediately, the Load handler is the place to do that thing. Having activated the script and had the Load handler execute, keep in mind that the script stays loaded, and the Load handler will execute the next time you (or anyone else) logs into the document. If you don't want this, you should deactivate the script.

Run Once

If you are writing a script to just do one thing (e.g. do a one-off export of some information to a file), and do not want the script to be active for every user of the document when they log in, you can use the Run Once button to just call the Load and Unload handlers. The script won't remain active.

Setting breakpoints

In MoneyWorks 9.2.1 and later you can set breakpoints by clicking in the darker breakpoint margin. A breakpoint shows as a white blob in the margin. Click again to clear the breakpoint. When you run the script, the debugger will be invoked at any breakpoints you have set. See The MWScript Debugger

Log File

The Log File tool will open the MoneyWorks_Gold.log file (in Console.app on Mac, or Notepad2 on WIndows). The log file shows errors and also any output you generate using the Syslog() function.

Navigating to Handlers

The Handlers tool contains a menu of all of the handlers in your script. Large scripts may have many handlers, so this provides a quick way to jump to the declaration of a particular handler.

The Search field

To find text in the current script, activate the search field with ⌘-F/Ctrl-F and type the text to find. All matches in the current script will be highlighted. You can jump to the next match bt pressing Return, or ⌘-G/Ctrl-G. You can also quickly find the next occurrence or a highlighted word with ⌘-G/Ctrl-G.

Find All

The Find All tool can search all of the scripts in the document (or only the active ones). Click on any match in the matches window to instantly jump to that line in the script.