Interacting with Legacy JScript

When used from JScript that is executed from the Mechanical APIs, the JScript function returnFromScript() allows you return values from JScript back to Python or C#

In the following code example, nothing is returned because returnFromScript() is not used:

x = ExtAPI.Application.ScriptByName("jscript").ExecuteCommand("""var x=1""")

However, if you use the function returnFromScript(), you can return the value of x from the JScript back to the Python code:

x = ExtAPI.Application.ScriptByName("jscript").ExecuteCommand("""var     x=1
returnFromScript(x)""")

In this case, x now holds the value 1 because this value was passed into the Python code from the returnFromScript() function.

Supported Return Types

The function returnFromScript() can return values from the following data types:

  • Int

  • Double

  • Boolean

  • String

  • Lists (Windows only)

  • Dictionaries (Windows only) if created as Automation objects:

    var dict = new ActiveXObject("Scripting.Dictionary");

Known Limitations

The following limitations exist:

  • A list can hold a maximum of 65,535 items.

  • Lists and dictionaries do not work on Linux.

  • In the JScript that is to be executed using ExecuteCommand(), ret is a reserved keyword.

Example

The JScript that follows includes many functions for returning different types of variables:

script = """
    	function returnInt(){
        	return 10;
    	}
	Function returnDouble(){
     		return 3.14;
      }
    	function returnString(){
        	return "Testing J Script";
    	}
    	function returnBool(){
        	return true;
    	}
    	var innerDict = new ActiveXObject("Scripting.Dictionary");
   	 
    	function returnList(){
        	var x = 10;
        	var y = 20;
        	var str = "thirty"
        	var nullVal = null;
        	var boolVal = true;
        	var innerList1 = [1 ,2, 3, "str", [1, "str", 2]];
        	var innerList2 = [[1], [1,2,3,[1,5,6]]];
		innerDict.Add("Testing", 1);

        	var list = [x,y, str, nullVal, boolVal, innerList1, 
 innerList2, innerDict]
        	return list;
    	}

	function returnLongList(){
        	var longList = [];
        	for(var i= 0; i<65535; i++){
            	longList[i] = i;
        	}

        	return longList;
    	}

    	function returnDict(){
        	var dict = new ActiveXObject("Scripting.Dictionary");
        	var listTry = [1,2,3]
        	dict.Add("string", "strTest");
        	dict.Add("int", 1);
        	dict.Add("bool", true);
        	dict.Add("null", null);
        	dict.Add("list", [1,2,3])
        	dict.Add(1, "int");
        	dict.Add(true, "bool");
        	dict.Add([1,2,3], "list");
       	 
        	innerDict.Add("Testing", 1);
        	dict.Add("dict", innerDict);
       	 
        	return dict;
    	}
    	var retVal = returnInt();
    	returnFromScript(retVal );
	
""")

As demonstrated, you can set the variable retVal to whatever is returned from some function. You can then pass the variable retVal into returnFromScript() to return it to the Python code.

Return a list

The function returnList() can be used as a reference when returning lists from the JScript code.

Return a dictionary

The function returnDict() can be used as a reference when returning dictionaries from the JScript code.

Passing in a value to the JScript code from Python code

To pass in a value to the JScript code from Python code, you embed string values of Python code in the script.

The goal of this first example is to pass an integer value (5) to the JScript code from the Python code and then increment it by 1 in the JScript code and return the new value (6) to the Python code.

x = 5
script = """
var x = """ + str(x) + """; 
x++;
returnFromScript(x);
"""
x = ExtAPI.Application.ScriptByName("jscript").ExecuteCommand(script)

The goal of this second example is to pass a list (1, 2, 3, 5) to the JScript code from the Python code and then update the fourth element and return the updated list (1, 2, 3, 10) to the Python code.

x = [1, 2, 3, 5]
script = """
var x = """ + str(x) + """; 
x[3] = 10;
returnFromScript(x);
"""
x = ExtAPI.Application.ScriptByName("jscript").ExecuteCommand(script)


Note:  To pass in a Boolean value from Python to JScript, you must first convert it to a string and then make the string all lowercase because Booleans in Python start with an uppercase character where in JScript they start with a lowercase character.