AnsweredAssumed Answered

MBS New Shell Function Very Cooøl

Question asked by fmpdude on Dec 2, 2017
Latest reply on Dec 4, 2017 by fmpdude

If you do any Python programming, as one example, MBS has a very cool way you can communicate between FMP and Python (and Java, too!).



Using the new MBS "Shell" command, you can call a Python program, pass an argument, and get the result back from Python. Python is incredibly easy to learn and use and extremely (extremely) powerful. Many of the Python stuff you do uses external Python libraries (usually) written in C, so Python is also extremely fast.


Here's an example, using the MBS 7.5 and the Shell command to call a python program:


FMP Script (assumes MBS 7.5 installed):

Set Variable [ $shell ; Value: MBS("") ]

Set Variable [ $s ; Value: MBS("Shell.execute"; $shell ;



               ;PythonMBSTest::Input) ]

Set Variable [ $s ; Value: MBS("Shell.wait"; $shell; 1) ]

Set Variable [ $error ; Value: MBS("Shell.ReadErrorText"; $shell; "UTF=8") ]

Set Variable [ $result ; Value: MBS("Shell.ReadOutputText"; $shell ; "utf-8") ]

Set Field [ PythonMBSTest::output ; $result




And, here's the Python ultra-simple test Script itself (just returns upper-cased string):

x = ""
for arg in sys.argv[1:]:

  x = str(arg)

assert x, "No input."
upperCased = str(x).upper()





Here's the test FMP application that calls Python via MBS

This Shell capability is a nice alternative to writing a micro-service since, other than the Python script itself, all the code is in FMP. Of course, you wouldn't go to all this unnecessary trouble to uppercase a string; just a simple example.





Java works the same way, except you need to be more careful about the environment (classpath and starting directory). For this example, I hard coded the path to the Desktop folder in the MBS Script. Plus using the Shell.setCurrentDirectory(), which wasn't necessary with Python.


Here's the test FMP application that calls Java via MBS

The Java code called in this case was a simple main method with a"toUpperCase()" of the first command-line argument.


Here's the actual demo Java code used:



Hope this information is useful.