Goodies 33: Custom context menu commands

At the FileMaker conference 2019 in Hamburg there was a nice idea from Russel Watson to extend the Script Workspace further. For our context menu showing in the Script Workspace, we give you a new function SyntaxColoring.AddContextMenuCommand to register a custom menu entry. You provide a title (with ΒΆ for submenus), an expression and whether you need a selection. Once the menu item is chosen the plugin will evaluate the expression.

We expect that some developers from the FileMaker community will provide their own database with various expressions you could install. Our examples for this function show a few possible commands. Our examples show you how to just add a dialog box. Another example uses Menubar.RunMenuCommand to ask FileMaker to copy the current script steps. Then Clipboard.GetFileMakerData is used to get the XML for the script steps. This XML can be modified and passed to Clipboard.SetFileMakerData to put it back on the clipboard and paste it again with Menubar.RunMenuCommand function. As you see, you can have commands to edit script steps in place with our new feature. You can include XSLT via XML.ApplyStylesheet, using a command line tool (Shell functions) or even an AppleScript.

Here is an example video:

Available in MBS Plugin version since version 9.5. Email us if you like to try it out.

Please check the example file Script Workspace Context Menu Commands, which contains a couple of example calculations. You can add your own snippets there.

The custom menu commands are stored in the FileMaker Pro preferences file and loaded on startup of the plugin.

Add a search and replace command for FileMaker Script Workspace

Have you seen the SyntaxColoring.AddContextMenuCommand command in our latest plugin?
It allows you to have a customized contextual menu in Script Workspace with your own commands. And here is a powerful command we just developed for you:

MBS( "SyntaxColoring.AddContextMenuCommand"

"Search and Replace"

"Let ([ 

/* ask for search text via dialog */

ResetResult = MBS( \"Dialog.Reset\" );

ClearResult = MBS( \"Dialog.ClearFields\" );

MessageResult = MBS( \"Dialog.SetMessage\"; \"Search and replace in script:\" );

InfoResult = MBS( \"Dialog.SetInformativeText\"; \"Search text can include XML tags.\" );

Field1Result = MBS( \"Dialog.AddField\"; \"Search for:\" );

Field2Result = MBS( \"Dialog.AddField\"; \"Replace with:\" );

Button1Result = MBS( \"Dialog.SetDefaultButton\"; \"Replace\");

Button2Result = MBS( \"Dialog.SetAlternateButton\"; \"Cancel\");

DialogResult = MBS( \"Dialog.Run\" );

SearchText = MBS( \"Dialog.GetFieldText\"; 0 );

ReplaceText = MBS( \"Dialog.GetFieldText\"; 1 );

/* do replace */

r = If(DialogResult = \"Replace\"; Let([

/* copy script steps */

r = MBS( \"Menubar.RunMenuCommand\"; 57634 ); 

/* get XML from clipboard */

xml = MBS( \"Clipboard.GetFileMakerData\"; \"ScriptStep\" ); 

/* search and replace */

xml = Substitute ( xml; SearchText; ReplaceText); 

/* put XML back on clipboard */

r = MBS( \"Clipboard.SetFileMakerData\"; \"ScriptStep\"; xml ); 

/* paste script steps */

r = MBS( \"Menubar.RunMenuCommand\"; 57637 ) 

]; 1))

];1)"; 3 )

Copy and paste this into the data viewer and evaluate it once to add the command to the contextual menu. Now you can just select a few lines, choose the menu command and we do a search and replace. The new lines are pasted below the old license and you can review them.
To learn how it works, here the version without the extra backslashes:

Let ([ 

/* ask for search text via dialog */

ResetResult = MBS( "Dialog.Reset" );

ClearResult = MBS( "Dialog.ClearFields" );

MessageResult = MBS( "Dialog.SetMessage""Search and replace in script:" );

InfoResult = MBS( "Dialog.SetInformativeText""Search text can include XML tags." );

Field1Result = MBS( "Dialog.AddField""Search for:" );

Field2Result = MBS( "Dialog.AddField""Replace with:" );

Button1Result = MBS( "Dialog.SetDefaultButton""Replace");

Button2Result = MBS( "Dialog.SetAlternateButton""Cancel");

DialogResult = MBS( "Dialog.Run" );

SearchText = MBS( "Dialog.GetFieldText"0 );

ReplaceText = MBS( "Dialog.GetFieldText"1 );

/* do replace */

r = If(DialogResult = "Replace"Let([

/* copy script steps */

r = MBS( "Menubar.RunMenuCommand"57634 )

/* get XML from clipboard */

xml = MBS( "Clipboard.GetFileMakerData""ScriptStep" )

/* search and replace */

xml = Substitute ( xml; SearchText; ReplaceText)

/* put XML back on clipboard */

r = MBS( "Clipboard.SetFileMakerData""ScriptStep"; xml )

/* paste script steps */

r = MBS( "Menubar.RunMenuCommand"57637 ) 


We use two Let statements to combine a lot of MBS calls. First we show a dialog with two fields to enter the search text. Then if "Replace" button is clicked, we  copy the script steps, get the XML, do a replace and paste the modified XML right into the script editor. Please try and let us know whether it works for you.
Please try and let us know if you find another cool menu command to add! 

Variable Colors 33 Text position