Trigger FileMaker Scripts from JavaScript in FileMaker 16
Now we have a new way for WebKit 2.x using a JavaScript bridging. With macOS 10.10 (and iOS 8.0) Apple introduced the ability to install custom JavaScript message handlers for WKWebView. We just added them for our plugins to call back from JavaScript to FileMaker scripts.
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.
PS: Works in FileMaker 16 runtimes, too.