Because the script is evaluated in the global execution context, any declared variables or functions will be added to the
global object and will be available in subsequent calls from CSPro to JavaScript. This is similar to what happens with scripts attached to the application with the
JavaScript (Global) setting.
The function
returns the "completion value of evaluating the given code. If the completion value is empty,
undefined is returned." The completion value is returned as a string using the value's
toString method. If the JavaScript evaluation ends in an uncaught exception, the exception's message is displayed as an error message and the function returns a blank string.
JS.eval("const artist = 'Roberta Flack';"); // undefined
JS.eval("artist.toUpperCase();"); // ROBERTA FLACK
JS.eval("[ ...artist ];"); // R,o,b,e,r,t,a, ,F,l,a,c,k
// add a function that removes whitespace from a string (using a regular expression)
JS.eval(@"function removeWhitespace(text) {"
@" return text.replace(/\s+/g, '');"
@"}");
// evaluate the function using JS.eval with the result: CSProisaveryusefultool!
JS.eval(@"removeWhitespace('CSPro is a very useful tool!')");
// evaluate the function using JS.invoke
JS.invoke("removeWhitespace", "CSPro is a very useful tool!");
// the Action Invoker is available in the JavaScript environment and may return values similar to:
JS.eval("CS.UI.getMaxDisplayDimensions()"); // [object Object]
JS.eval("JSON.stringify(CS.UI.getMaxDisplayDimensions())"); // {"width":1728,"height":928}
JS.eval("CS.UI.getMaxDisplayDimensions().width"); // 1728