« HTMLViewer JavaScript… | Home | Xojo's picture cache »

The FileMaker.PerformScript function in FileMaker 19

You may have read the documentation about the new JavaScript integration in FileMaker 19. Let's take a look on this in detail. For every website you load, FileMaker injects automatically a little JavaScript when the page is finished loading. The JavaScript for the PerformScript script in the FileMaker namespace looks like this:

function PerformScript(name, parameter) {
    if (parameter == null) {
        parameter = ""
    }
    var message = '{"command": "PerformScript", "value": { "name": ' + quote(name) + ', "parameter": ' + quote(parameter) + '}}';
    // For mac
    if (window.webkit && window.webkit.messageHandlers.fm != null) {
        webkit.messageHandlers.fm.postMessage(message);
    } else if (window.external != null) {
        // For windows
        window.external.onMessage(message);
    }
}

As you see for MacOS (and iOS), they use the MessageHandler protocol offered by WebKit, which is the same we use for our WebView.AddScriptMessageHandler function in MBS FileMaker Plugin. The message handler is quotes the given parameters, builds a JSON text and passes this to the message handler.

If you need, you can use MBS FileMaker Plugin to add the same functionality for MacOS and iOS (iOS with FileMaker iOS SDK) to your solution for FileMaker 16 to 19. Use WebView.RunJavaScript to install the FileMaker object with the function above. You may need to add a file name parameter as we need that for the plugin to call a script.

For Windows, the use the external messaging, which is available in Internet Explorer control. Sadly we can't access this via plugin interface for our plugin. But a great move by Claris to add this for us!

Now you may notice that the function checks whether window.webkit.messageHandlers.fm is defined, so if the message handler is not yet installed, the calls to PerformScript do nothing. But you may be able to check whether FileMaker is not undefined like this:

if (typeof(FileMaker) != "undefined") { /* okay */ }

In your JavaScript on the website, you may just run setup work on start and set a timer to do any call to FileMaker.PerformScript a few milliseconds later.
You may want to checkout onfmready.js from Stephan Casas, who made a nice utility function to delay the call until FileMaker.PerformScript is available. See also community posting.

See also Check out options for FileMaker JavaScript Integration
23 05 20 - 09:37