« ISO FileMaker Magazin… | Home | Sort with evaluate in… »

Trigger FileMaker Scripts from JavaScript in FileMaker without fmp URL


Did you know you can have a callback from JavaScript in a web viewer in FileMaker without using FMP URL scheme?

For MacOS and iOS in FileMaker 16 or newer, you can use the WebKit message handler feature. First you call Webview.AddScriptMessageHandler function to register a name:

MBS( "Webview.AddScriptMessageHandler"; Name )

Next you can use it in Javascript to trigger a script in FileMaker from JavaScript:

window.webkit.messageHandlers.test.postMessage({'filename':'WebView Message Handler.fmp12', 'parameter':'Hello World from JavaScript', 'scriptName':'test'});

And you can evaluate an expression in JavaScript:

window.webkit.messageHandlers.test.postMessage({'evaluate':'MBS( "Msgbox"; "Hello from Evaluate in JavaScript" )'});

As you see, we get for our message handler a new entry in the namespace called window.webkit.messageHandlers.test. This allows us to call postMessage method there and this triggers the plugin code to trigger scripts or do evaluation. The parameter to the postMessage must be a Javascript object with a few properties:
  • fileName: The name of the file with the script.
  • scriptName: The name of the script to trigger.
  • parameter: The parameter to pass to the script.
  • evaluate: The expression to evaluation.
This works nice for 2 years now and we'd love to see what you can do with this technique.

You can define a JavaScript function to wrap this and install it with WebView.RunJavaScript into the web viewer or include it in the html document:

function CallFileMaker(FileName, ScriptName, Parameter)
{
    window.webkit.messageHandlers.test.postMessage({'filename': FileName, 'parameter': Parameter, 'scriptName': ScriptName });
}

Works in FileMaker Pro, Runtimes and in apps based on the FileMaker iOS SDK.
09 04 20 - 18:15