Topics   All   Mac OS X (Only)   Windows (Only)   Linux (Only, Not)   iOS (Only, Not)  
Components   Crossplatform Mac & Win   Server   Client   Old   Guides   Examples
New in version: 8.2   8.3   8.4   8.5   9.0   9.1   9.2   9.3   9.4   9.5    Statistic  

SyntaxColoring.AddContextMenuCommand

Adds a new command for contextual menu.

Component Version macOS Windows Server FileMaker Cloud FileMaker iOS SDK
SyntaxColoring 9.5 Yes No No No No
MBS( "SyntaxColoring.AddContextMenuCommand"; Title; Expression; InstallWhen { ; keyEquivalent; KeyEquivalentModifierMask } )   More

Parameters

Parameter Description Example value
Title The title for the menu command.
If multi line, we build submenus based on the path descripted.
If title is just "-", we add a separator.
"MyCommands¶Hello"
Expression The expression to evaluate in FileMaker when menu command is choosen.
InstallWhen Whether to enable only if selection is present.
0: always on
1: need no row selected
2: need exactly one row selected
3: need one or more rows selected
4: need all rows selected

Add 8 to allow for discontinuous selections (in version 10.0 or later).
0
keyEquivalent Optional
Available in MBS FileMaker Plugin 10.0 or newer.
The key equivalent.
Is shown right in the menu and can be pressed while menu is open.
"T"
KeyEquivalentModifierMask Optional
Available in MBS FileMaker Plugin 10.0 or newer.
The menu items's keyboard equivalent modifiers.
NSShiftKeyMask is a valid modifier for any key equivalent in mask. This allows you to specify key-equivalents such as Command-Shift-1 that are consistent across all keyboards. However, with a few exceptions (such as the German "ß” character), a lowercase character with NSShiftKeyMask is interpreted the same as the uppercase character without that mask. For example, Command-Shift-c and Command-C are considered to be identical key equivalents.

NSShiftKeyMask 131072 Shift Key
NSControlKeyMask 262144 Control Key
NSAlternateKeyMask 524288 Alternate/Option Key
NSCommandKeyMask 1048576 Command Key
524288 + 131072

Result

Returns OK or error.

Description

Adds a new command for contextual menu.
Given commands are added to contextual menu in Script Workspace when right clicking on script steps.
Commands are stored in preferences file and can be removed via SyntaxColoring.ClearContextMenuCommands function.

If you use Clipboard.GetFileMakerData and Clipboard.SetFileMakerData functions, you can get the XML for the current step. You may want to use Menubar.RunMenuCommand to run cut/copy/paste/delete commands from the FileMaker menu with the respective command IDs: 57635 for cut, 57634 for copy, 57637 for paste, 57632 for delete and 57642 for select all.

Possible uses include commands
  • to add new script steps.
  • to check scripts for errors.
  • to search and replace things
  • to add common snippets, possibly using SQL or text file functions to read the XML for them.
  • to pass script steps as XML to external tools.
Use SyntaxColoring.RemoveContextMenuCommand to remove one or SyntaxColoring.ClearContextMenuCommands to clear all.

Examples

Add a greeting command:

MBS( "SyntaxColoring.AddContextMenuCommand"; "Greeting"; "MBS(\"Msgbox\"; \"Hello World\")"; 0 )

Add a rename command for variable:

MBS( "SyntaxColoring.AddContextMenuCommand";
"Rename $I to $counter";
"Let ([ r = MBS( \"Menubar.RunMenuCommand\"; 57634 /* copy */ ); xml = MBS( \"Clipboard.GetFileMakerData\"; \"ScriptStep\" ); xml = Substitute ( xml; \"$i\"; \"$counter\"); r = MBS( \"Clipboard.SetFileMakerData\"; \"ScriptStep\"; xml ); r = MBS( \"Menubar.RunMenuCommand\"; 57637 /* paste */ ) ]; \"\" )"; 0 )

Add an insert loop command:

MBS( "SyntaxColoring.AddContextMenuCommand";
"Insert For Loop";
"Let ([
xml = \"<fmxmlsnippet type=\\\"FMObjectList\\\"><Step enable=\\\"True\\\" id=\\\"89\\\" name=\\\"# (comment)\\\"><Text>loop counting up from 1 to $count</Text></Step><Step enable=\\\"True\\\" id=\\\"141\\\" name=\\\"Set Variable\\\"><Value><Calculation><![CDATA[\\\"/* add count here */\\\"]]></Calculation></Value><Repetition><Calculation><![CDATA[1]]></Calculation></Repetition><Name>$count</Name></Step><Step enable=\\\"True\\\" id=\\\"141\\\" name=\\\"Set Variable\\\"><Value><Calculation><![CDATA[1]]></Calculation></Value><Repetition><Calculation><![CDATA[1]]></Calculation></Repetition><Name>$index</Name></Step><Step enable=\\\"True\\\" id=\\\"68\\\" name=\\\"If\\\"><Calculation><![CDATA[$index ≤ $count]]></Calculation></Step><Step enable=\\\"True\\\" id=\\\"71\\\" name=\\\"Loop\\\"></Step><Step enable=\\\"True\\\" id=\\\"89\\\" name=\\\"# (comment)\\\"><Text>your script steps here</Text></Step><Step enable=\\\"True\\\" id=\\\"89\\\" name=\\\"# (comment)\\\"></Step><Step enable=\\\"True\\\" id=\\\"89\\\" name=\\\"# (comment)\\\"><Text>next</Text></Step><Step enable=\\\"True\\\" id=\\\"141\\\" name=\\\"Set Variable\\\"><Value><Calculation><![CDATA[$index + 1]]></Calculation></Value><Repetition><Calculation><![CDATA[1]]></Calculation></Repetition><Name>$index</Name></Step><Step enable=\\\"True\\\" id=\\\"72\\\" name=\\\"Exit Loop If\\\"><Calculation><![CDATA[$index > $count]]></Calculation></Step><Step enable=\\\"True\\\" id=\\\"73\\\" name=\\\"End Loop\\\"></Step><Step enable=\\\"True\\\" id=\\\"70\\\" name=\\\"End If\\\"></Step></fmxmlsnippet>\";
r = MBS( \"Clipboard.SetFileMakerData\"; \"ScriptStep\"; xml );
r = MBS( \"Menubar.RunMenuCommand\"; 57637 /* paste */ )
]; \"\" )"; 0 )

Add a search and replace command:

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 )

Add an insert JSONSetElement command:

MBS( "SyntaxColoring.AddContextMenuCommand";
"Insert set variable for JSONSetElement";
"Let ([
xml = \"<fmxmlsnippet type=\\\"FMObjectList\\\"><Step enable=\\\"True\\\" id=\\\"141\\\" name=\\\"Variable setzen\\\"><Value><Calculation><![CDATA[JSONSetElement ( \" & Char(10) & \" \\\"{}\\\" ;\" & Char(10) & \" [ \\\"keyOrIndexOrPath\\\" ; \\\"value\\\" ; JSONString ] ;\" & Char(10) & \" [ \\\"keyOrIndexOrPath\\\" ; \\\"value\\\" ; JSONString ]¶)]]></Calculation></Value><Repetition><Calculation><![CDATA[1]]></Calculation></Repetition><Name>$json</Name></Step></fmxmlsnippet>\";
r = MBS( \"Clipboard.SetFileMakerData\"; \"ScriptStep\"; xml );
r = MBS( \"Menubar.RunMenuCommand\"; 57637 /* paste */ )
]; \"\" )"; 0 )

// we use Char(10) to get the new line in the XML. And we use \\\ to escape quotes in escaped calculation.

Add menu command to write script to desktop:

MBS( "SyntaxColoring.AddContextMenuCommand";
"Write script to desktop";
"MBS( \"Text.WriteTextFile\";
        MBS( \"ScriptWorkspace.ScriptText\" );
        MBS( \"Path.AddPathComponent\";
            MBS( \"Folders.UserDesktop\" );
            \"Script \" & substitute(substitute(MBS( \"ScriptWorkspace.CurrentTab\" ); \":\"; \"-\"); \"/\"; \"-\") & \".txt\" );
        \"UTF-8\"
    )"; 0 )

Add separator item:

MBS( "SyntaxColoring.AddContextMenuCommand"; "-"; "-"; 0 )

// needs version 10.0 of the plugin.

Add function header:

MBS( "SyntaxColoring.AddContextMenuCommand";
"Insert function header";
"Let ([
xml = \"<fmxmlsnippet type=\\\"FMObjectList\\\"><Step enable=\\\"True\\\" id=\\\"89\\\" name=\\\"# (comment)\\\"><Text>Description</Text></Step><Step enable=\\\"True\\\" id=\\\"89\\\" name=\\\"# (comment)\\\"><Text>Author</Text></Step><Step enable=\\\"True\\\" id=\\\"89\\\" name=\\\"# (comment)\\\"><Text>Modifications</Text></Step><Step enable=\\\"True\\\" id=\\\"89\\\" name=\\\"# (comment)\\\"></Step><Step enable=\\\"True\\\" id=\\\"85\\\" name=\\\"Allow User Abort\\\"><Set state=\\\"False\\\"></Set></Step><Step enable=\\\"True\\\" id=\\\"86\\\" name=\\\"Set Error Capture\\\"><Set state=\\\"True\\\"></Set></Step><Step enable=\\\"True\\\" id=\\\"89\\\" name=\\\"# (comment)\\\"></Step><Step enable=\\\"True\\\" id=\\\"141\\\" name=\\\"Set Variable\\\"><Value><Calculation><![CDATA[Get(ScriptParameter)]]></Calculation></Value><Repetition><Calculation><![CDATA[1]]></Calculation></Repetition><Name>$param</Name></Step><Step enable=\\\"True\\\" id=\\\"89\\\" name=\\\"# (comment)\\\"></Step><Step enable=\\\"True\\\" id=\\\"89\\\" name=\\\"# (comment)\\\"></Step><Step enable=\\\"True\\\" id=\\\"89\\\" name=\\\"# (comment)\\\"></Step><Step enable=\\\"True\\\" id=\\\"141\\\" name=\\\"Set Variable\\\"><Value><Calculation><![CDATA[\\\"\\\"]]></Calculation></Value><Repetition><Calculation><![CDATA[1]]></Calculation></Repetition><Name>$result</Name></Step><Step enable=\\\"True\\\" id=\\\"103\\\" name=\\\"Exit Script\\\"><Calculation><![CDATA[$result]]></Calculation></Step></fmxmlsnippet>\";
r = MBS( \"Clipboard.SetFileMakerData\"; \"ScriptStep\"; xml );
r = MBS( \"Menubar.RunMenuCommand\"; 57637 /* paste */ )
]; \"\" )"; 0 )

Add command to paste snippet from folder picked by dialog:

// put snippets as xml files into ~/Library/Application Support/FileMaker/Snippets folder.

MBS( "SyntaxColoring.AddContextMenuCommand";
"Insert snippet from snippet folder";
"Let([
Path = MBS( \"Path.AddPathComponent\"; MBS( \"Folders.UserHome\" ); \"Library\"; \"Application Support\"; \"FileMaker\"; \"Snippets\");
Files = MBS( \"Files.List\"; Path; 1+4);
Names = MBS( \"List.RemovePostfix\"; Files; \".xml\"; 1);
r = MBS( \"ListDialog.Reset\" ) & MBS( \"ListDialog.AddItemsToList\"; Names; Files) & MBS( \"ListDialog.SetPrompt\"; \"Choose snippet to insert:\" );
answer = MBS( \"ListDialog.ShowDialog\" );
r = If(answer = \"OK\"; MBS( \"Clipboard.SetFileMakerData\"; \"ScriptStep\"; MBS( \"Text.ReadTextFile\"; MBS( \"Path.AddPathComponent\"; Path; MBS( \"ListDialog.GetSelectedTag\" )); \"UTF-8\") ) & MBS( \"Menubar.RunMenuCommand\"; 57637 /* paste */ ))]; 0)"; 0 )

See also

Blog Entries

Release notes

Created 18th October 2019, last changed 4th December 2019


StoreRegistration   -   SyntaxColoring.AddTag

Feedback: Report problem or ask question.




Links
MBS Xojo tutorial videos