« eID card reading via … | Home | The annoyance of Non-… »

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 ) 

]1))

];1) 
 
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! 
14 11 19 - 12:49