360Works ScriptMaster User Guide

Introduction

ScriptMaster is a free, general-purpose, modular plugin. It includes modules for file manipulation, URL and network utilities, Web Services, shell scripting, event/script triggering, and many others. Modules for ScriptMaster are written in a programming language called Groovy, which is very similar to Java. Because they are so similar, we often use the terms Java and Groovy interchangeably in this documentation, although it is more technically accurate to refer to it as Groovy. You do not need to be a Java programmer to use ScriptMaster, but if you are, you can write your own modules or enhance the ones that come with it.

ScriptMaster modules work just a lot like FileMaker custom functions: They take some named input parameters, process those inputs, and return an output. They appear in your FileMaker function list, just like custom functions.

In other ways, however, they improve on what you can do with custom functions. Custom functions only allow you to do things you can already do with features built into FileMaker, but ScriptMaster modules can access all of the power in the Java language and class libraries. It's also easier to share ScriptMaster modules across all of your FileMaker files than sharing custom functions, because custom functions must be duplicated into every FileMaker file, while ScriptMaster modules are available to every file running in FileMaker.

To get started with ScriptMaster, start by installing the ScriptMaster plugin (see installation instructions below). Then open the ScriptMaster.fp7 file that comes with it. You'll see a list of all of the modules that come with ScriptMaster. To try one out, click on it in the list view. 'Screen Capture' is a good one to start with, because the Java code is very short and easy to understand, and it also demonstrates how to work with data in container fields. Click the 'Evaluate Script' button to run the module, which will execute the Java code based on the values set for the input variables. The 'Evaluate Script' button is a very easy way to try out modules in the development file, but it's not how you would use it within your own solution. For that, we'll talk about registering this as a function.

Registering functions

Registering a function will make it appear in your list of available FileMaker functions. Because it compiles the code, it will also run much faster as a registered function than using the 'Evaluate Script' button. To register a module, simply click the register button. This takes you to the registration screen, compiles the code, and adds it to the list of available FileMaker functions. You can see this by going into the calculation dialog and pulling down to the 'External functions' grouping. The registered function will appear under the 'ScriptMaster' section, and can be called just like any other built-in FileMaker function.

Now that you've registered the function, you're almost ready to start using it in your solution. The only problem is that functions only remain registered while FileMaker is running - if you were to quit FileMaker and start it back up, the module you just registered would no longer appear in the FileMaker functions list. We'll discuss two options for registering ScriptMaster modules every time you open your solution file.

The first option, which is the one that we recommend in most cases, is to simply check the 'register on startup' box from the registration screen (the layout that you are taken to after clicking the 'register' button). All modules with this option enabled are registered every time you open the ScriptMaster.fp7 file, so you can just include the ScriptMaster.fp7 file with your solution, open it in your startup script, and then immediately close it (it doesn't need to stay open after the functions have been registered). Here's an example of what you would put in your startup script:

Open File ["ScriptMaster"]
Close File ["ScriptMaster"]
We recommend this option because it is very easy to use. Plus, if you uncheck the 'Ask user before installing plugin' checkbox, you can have the ScriptMaster file install the ScriptMaster plugin for you. Please refer to the '1-2-3 Go' section of the demo file for more information.

The second, more advanced option is to copy the registration code into your own startup script. This is a better choice if you do not want to include the ScriptMaster.fp7 file with your solution. To do this, click the 'copy' button from the registration screen. This puts all of the registration code onto your clipboard. Now go to the startup script in your solution, add a 'set variable' script step (you can name the variable anything you like), and paste in the registration code as the formula to calculate. Now the function will be registered every time that your startup script runs. Here's what that script step would look like:

Set Variable [$result; Value:RegisterGroovy( "GetURLasText( url )"; "new URL(url).getText();" )]
It is a good idea to check for errors after registering the function, in case there is any problem compiling the Java code. See the section below on error reporting, or watch the Youtube movie in this documentation to see an example of this complete setup. Here is an example that does the same thing as the previous one, but checks for errors and displays them to the user in case the function cannot be compiled:
If [RegisterGroovy( "GetURLasText( url )"; "new URL(url).getText();" ) = "ERROR"]
Show Custom Dialog ["Could not register ScriptMaster function"; SMLastError]
End If

Pricing

The ScriptMaster plugin is completely free. You may distribute it with your own solution. It would be great if you mention 360Works or ScriptMaster in your documentation / about screen, but there is no requirement that you do this. This plugin may be deployed with the Web Publishing Engine, for scripts that will be called from Instant Web Publishing or Custom Web Publishing. It may also be deployed on FileMaker Server for use with scripts that are scheduled to run on the server. See the installation section below for more information on how to do this.

Tutorial video

We've created a video showing how to use ScriptMaster. This is a quick (9 minute) jump-start showing you how to register a module and incorporate it into your solution.

Advanced techniques

Your ScriptMaster functions can reference compiled Java libraries (stored in a the .jar format) by using the SMLoadJar function. Pass in a container field containing a .jar file, or a URL pointing to a .jar file, and any scripts you execute will have access to the classes in that .jar. This means you can access libraries like iText (a powerful PDF manipulation library) or qtjava (QuickTime for Java) from within FileMaker. You could also easily write your own java code, package it as a .jar, and distribute it with your FileMaker solution! And since the .jar is stored in FileMaker, deploying new versions of the java code to the FileMaker clients is as easy as updating a single container field.

Keep in mind that if you are copying and pasting the function registration into your startup script, and that module requires any jar libraries, you will need to add those jar files to container fields in your solution and use SMLoadJar to load them before registering the function. This is not an issue if using the 'register on startup' option with the ScriptMaster.fp7 development file, since that file includes all of the jar files needed for the modules that come with ScriptMaster.

After a ScriptMaster function executes, you can access any of the named variables which were set during execution using the SMGetVariable function. This can be very useful for returning multiple output values, similar to setting global variables in a FileMaker script.

Getting help

If you get the word 'ERROR' as the result of a ScriptMaster function, be sure to read the section below on Error reporting for how to get more information on that.

Phone and e-mail support for ScriptMaster are available at our standard hourly rate of $155. There is also a free ScriptMaster discussion forum where you can get help from the growing community of ScriptMaster users.

If you create your own module that you think would be useful to others, post it on the fmforums discussion group! If it seems like it will have broad appeal, we'll include it with the next release of ScriptMaster (preference will be given to modules that do not require 3rd party libraries).

New modules

In addition to the many existing modules that come free with ScriptMaster, 360Works is available to create custom modules suited to your specific needs. Send us an email ( plugins@360works.com ) or give us a call at 866-662-9185 and let us know what you're looking for, and we'll get you an estimate at no charge.

The other choice is to do it yourself! We want to start off by stressing that you can skip the rest of this section if you're not interested in learning Groovy or Java. ScriptMaster comes with tons of useful modules that you can use without having to learn a new programming language. However, if the built in modules have stirred your curiosity, you'll be able to take your FileMaker solutions to a whole new level with the power of Groovy and Java. Because Groovy is cross-platform, modules you create will run on both Windows and Mac OS X.

To create your own module, just create a new record from the menu. Give it a title and start coding! You can learn about Groovy at http://groovy.codehaus.org/Beginners+Tutorial (you can skip all of the steps about setting up your Java and Groovy environment; that's all taken care of by ScriptMaster).

Here are a few tips about using ScriptMaster with FileMaker:

In the 'Screen Capture' module, one thing you'll notice is that the Integer.valueOf() calls at the beginning. This is because all ScriptMaster functions are transmitted as text. The Integer.valueOf(someText) translates this into a numeric value. Java is much stricter about data types than FileMaker, so this can take some getting used to for FileMaker programmers.

In addition to setting variables prior to script execution, you can access the FileMaker calculation engine directly from within your Groovy script. This is accomplished using the reserved fmpro object. Every Groovy script which is executed will have an fmpro variable set to an instance of an FMPro object, which has these important methods:

evaluate(fmcalculation)
Evaluates the supplied FileMaker calculation text, returning the result as a String. The fmcalculation parameter can be any valid FileMaker calculation string.
performScript(fileName, scriptName [ , paramString ])
Performs a FileMaker script in the current file, passing in the optional paramString as a parameter. Note: fileName is required.
merge(template)
Processes some text with merge fields embedded, replacing the merge fields with their evaluated values. An example of a merge field is <<table::field>>. If an error occurs during evaluation, the field is left as is.

A script evaluated in ScriptMaster always returns the value of the last line, or the value which is implicitly returned by the script. This can be any type of Java object. ScriptMaster does its best to convert this to a FileMaker object.

The following conversions occur:

Error Handling/Reporting

When something unexpected happens, a plugin function returns a result of "ERROR". This makes it easy to check for errors. If a plugin function returns "ERROR", call the SMLastError function to get a detailed description of what went wrong.

Here is an example of basic error reporting:

Set Variable [ $result = MyPluginFunction("x" ; "y" ; "z") ]
If [ $result = "ERROR" ]
    Show Custom Dialog [ "An error occurred: " & SMLastError ]
End If

Chaining Multiple Functions Together

Since the string "ERROR" evaluates to false when evaluated by FileMaker, and most plugin functions return a 1 when successful, you can chain multiple dependent plugin operations together using the "and" operator. However, in this case the result will be a 1 or a 0, not "ERROR". For example:

// chain multiple calls together
// if any of the functions fail, the calculation will 
// short-circuit with a result of false,
// and none of the subsequent function calls will be evaluated.
Set Variable [ $success = 
    FirstPluginFunction("x") and 
    SecondPluginFunction("y") and 
    ThirdPluginFunction("z") 
]
If [not $success]
    Show Custom Dialog [ "An error occurred: " & SMLastError ]
End If

Note: the above only works for plugin functions which return 1 on success! Check the documentation for each function used in this manner.

Additional Error Checking - Plugin not installed

If a plugin is not installed correctly, calls to a plugin function will return "?". As part of your startup script, you should check for this occurrence and display a warning accordingly that the plugin needs to be installed. Note: when treated as a boolean true/false value, FileMaker will treat ? as true.

Installation

Requirements

FileMaker version 7 or higher.

Java Virtual Machine (JVM) version 1.4.2 or later. If you are running a JVM earlier than 1.4.2, you should upgrade. Download a JVM from http://www.java.com/en/download/. If you are not sure what version of Java you have installed, you can do java -version on the command line in Windows or OS X.

Windows, or Mac OS X version 10.4 or higher.

Note to intel Mac users: running this plugin under Rosetta is not supported. Upgrade to FileMaker 8.5 to run our plugin in native Intel mode.

Install Steps for FileMaker Pro

Drag the plugin from the MAC or WIN folder into your FileMaker extensions, and restart FileMaker.

This will also enable the plugin for use with Instant Web Publishing from the FileMaker Pro client software.

If the plugin does not load correctly, double-check that you meet the system requirements.

Install steps for FileMaker Web Publishing Engine / Instant Web Publishing

You do not need to do this step unless you plan on using the plugin with Instant Web Publishing or Custom Web Publishing with FileMaker Server Advanced. You will need an Enterprise License to use this feature.

For installing into the Web Publishing Engine with FileMaker 9 Server or FileMaker Server Advanced, drag the plugin from the MAC or WIN folder into the FileMaker Server/Web Publishing/publishing-engine/wpc/Plugins folder. If there is no Plugins folder inside the wpc folder, then create it manually. Restart FileMaker Web Publishing, and now the plugins should be ready to go.

Note that due to a bug which we and other plugin vendors have reported to FileMaker, web plugins do not work in FileMaker Web Publishing Engine 8.0v4 on Mac OS X. You will need to use a later version, like 9, or an earlier version, like 8.0v3. The Windows FileMaker Server 8.0v4 does not have this bug, and will work correctly.

The easiest way to test whether the plugin is working is to have a calculation which calls the version function of the plugin, and display that on an IWP layout. If it shows "?", then the plugin is not working. If it shows a number, then the plugin has been installed successfully.

Install steps for FileMaker Server 9 or later

You do not need to do this step unless you plan on using the plugin with scheduled script triggering, a new feature in FileMaker Server 9. You will need an Enterprise License to use this feature.

  1. Drag the plugin from the MAC or WIN folder into the FileMaker Server extensions folder. On Mac OS X, this is located at /Library/FileMaker Server/Database Server/Extensions folder. On Windows, this is at C:\Program Files\FileMaker\FileMaker Server\Database Server\Extensions.
  2. In the Server Admin application, restart FileMaker Server by stoppping and starting it.
  3. Go to Configuration -> Database Server->Server Plug-ins and check the box that says 'Enable FileMaker Server to use plug-ins', and then check the 'enabled' box for this plugin. Click the 'save' button and wait a few seconds to make sure that the 'enabled' check box stays checked. If it does not, then there was an error loading the plugin and you should contact us for help troubleshooting. You should now be able to write schedules that trigger scripts which use the plugin.

Auto Update

360Works has created an AutoUpdate helper database which makes setting up Auto Update much easier. This file includes pre-configured plugin files which you can place on your server, and an auto-update script for each of our plugins which you can paste into your own solution.

You can get the AutoUpdate360Works file at fmp7://autoupdate.360works.com/AutoUpdate360Works. Follow the instructions included in the file to either host your own Auto Update server or pull the files from ours.

Demo mode and Registering the plugin

Plugins will run in demo mode until they are registered. While running in Demo mode, the product will run for 2 hours every time you launch FileMaker / FileMaker Server / FileMaker Web Publishing Engine. The 2 hour time limit will reset every time you relaunch FileMaker. There is no expiration date when Demo mode stops working. There are no feature differences between the Demo version and the licensed version.

Once you have purchased the plugin, you can register it with the license key. Once a valid license key is entered, the product will run for as long as FileMaker is running. After FileMaker starts up with the plugin installed, open FileMaker preferences, click on the Plug-ins tab, select the plugin from the list, and click the Configure button. Enter your license key and company name in this dialog. You will only need to do this once on a given machine. Alternately, you can use the registration function to register the plugin during a startup script.

Note that if you are running the plugin with FileMaker Server / FileMaker Web Publishing Engine, you must use the registration function to register the plugin, since there is no preferences dialog on FileMaker Server to enter the license key and company name.

Feedback

We love to hear your suggestions for improving our products! If you are experiencing problems with this plugin, or have a feature request, or are happy with it, we'd appreciate hearing about it. Send us a message on our website, or email us!

Function Summary

Function Detail

EvaluateGroovy ( groovyScript )

Compiles and Evaluates a Groovy script in the foreground. This is the primary method in ScriptMaster, which is responsible for executing the actual groovy script. It is recommended that this only be used for testing purposes. For production use, register your scripts using the RegisterGroovy function, which avoids the compilation overhead and is much more convenient.

Call SMLastError to get a detailed description of the compilation error.

Call SMLastStackTrace to get a detailed description of any exception which occurs during script execution.

Parameters:
groovyScript - a block of groovy/java code
Returns: the return value of the evaluated expression, or "ERROR" on failure.

RegisterGroovy ( signature ; script )

Registers a ScriptMaster script as a custom plugin function. After calling this, you can call the function by name from any calculation dialog.

You must pass in a signature for the function being registered. This is the full signature of the function, including any parameters. For example:

Set Variable[$result = RegisterGroovy (
    "logMessage ( text )" ;
    "System.out.println(text)"
)
Note that the name of the function must not conflict with any pre-existing FileMaker functions.

The ScriptMaster.fp7 file included in the ScriptMaster download will take care of registering your ScriptMaster scripts as dynamic plugin functions at startup.

Calling RegisterGroovy twice with the same function name will overwrite the previously registered function. The function is uniquely identified by its name (case sensitive). You can change the arguments of a function without changing its unique id.

Parameters:
signature - the full signature of the plugin function, as it appears in the FileMaker list of functions.
script - the actual script.
Returns: 1 if the function is successfully registered, "ERROR" if an error occurs.

SMGetVariable ( variableName )

Returns the value of a named variable which was set in the previously executed call to EvaluateGroovy. This can be very useful if your custom script needs to return multiple values, or for debugging purposes. You can set any named variable within your script and retrieve the value of that value after evaluation has been completed.

Parameters:
variableName - the name of the variable whose value is being gotten.
Returns: the value which was assigned to the named variable during the last script execution.

SMLastError

Returns defailed information about the last error generated by this plugin. If another plugin function returns the text "ERROR", call this function to get a user-presentable description of what went wrong.

Returns: Error text, or an empty string if there was no error.

SMLastStackTrace

Returns the stack trace of the last generated exception, if any. This can be handy for troubleshooting cryptic error messages, when SMLastError does not return enough info.

Returns: the stack trace of the last execution.

SMLoadJar ( externalJar )

Load a custom jar file for use in a custom script - This should be called before calling EvaluateGroovy.

Note that all .jar files in the ScriptMaster.fp7 file are loaded when the file is first opened, so if you are using the ScriptMaster.fp7 file you typically will not need to call this function.

There are two methods of loading the driver .jar file: via URL, or via container field.

Loading a custom .jar file from a URL

You can load a jar file which is on the local file system or accessible on the network by passing a URL parameters as the externalJar parameter. The URL should be of the form file:///path/to/my.jar or http://example.org/path/to/my.jar.

Loading a custom .jar file from a container field

As an alternative to specifying a URL, you can embed any java .jar file into a container field in your FileMaker solution, and load the .jar from there. Be sure that the driver is not stored as a reference, or the ScriptMaster plugin will not be able to read the information.

Caching Behavior

Jars are cached according to their name. If you register two files with the same name, the second will not be registered. The jar file will remain registered until you call SMReset or quit FileMaker.

Parameters:
externalJar - jar file location or data. This can be a URL pointing to a .jar file, or an actual container field containing a .jar file.
Returns: 1 if the jar was loaded successfully, or an error message if the jar could not be loaded.

SMReset

Resets any loaded jars and variables. This is only needed if you are reloading jar files.

Returns: 1

SMSetVariable ( variableName ; value )

Set a variable to be used in a ScriptMaster Evaluate function call. The next script evaluated will have access to this named variable.

Note that the value will always be converted to a String. If you need to use this as some other object type (date, etc) you are responsible for converting it within your script.

All variables are cleared after every call to an Evaluate function. Setting a variable also clears the named variables from the previous script execution. If a script dealing with memory-intensive objects has been completed, it's a good idea to set a dummy variable to free up this memory.

Parameters:
variableName - the name of the variable being set
value - the value of the variable being set
Returns: null

SMVersion

Returns the version number of the plugin.

Returns: a string representation of the version number