• <GetStart>
  • CSPro User's Guide
    • The CSPro System
    • Data Dictionary Module
    • The CSPro Language
    • Data Entry Module
    • Batch Editing Applications
    • Tabulation Applications
    • Data Sources
    • CSPro Statements and Functions
    • Templated Reporting System
    • HTML and JavaScript Integration
    • Action Invoker
      • Overview
      • Execution Environments
        • CSPro Logic
        • OnActionInvokerResult Global Function
        • JavaScript (Embedded)
        • JavaScript (From Web Views)
        • JSON
        • Android Intent
      • Security and Formatting Options
      • Base Actions
      • Application Namespace
      • Clipboard Namespace
      • Data Namespace
      • Dictionary Namespace
      • File Namespace
      • Hash Namespace
      • Localhost Namespace
      • Logic Namespace
      • Message Namespace
      • Path Namespace
      • Settings Namespace
      • Sqlite Namespace
      • System Namespace
      • UI Namespace
    • Appendix
  • <CSEntry>
  • <CSBatch>
  • <CSTab>
  • <DataViewer>
  • <TextView>
  • <TblView>
  • <CSFreq>
  • <CSDeploy>
  • <CSPack>
  • <CSDiff>
  • <CSConcat>
  • <Excel2CSPro>
  • <CSExport>
  • <CSIndex>
  • <CSReFmt>
  • <CSSort>
  • <ParadataConcat>
  • <ParadataViewer>
  • <CSCode>
  • <CSDocument>
  • <CSView>
  • <CSWeb>

Action Invoker Execution from Embedded JavaScript

Overview
Running Action Invoker actions from JavaScript requires calling static methods contained in an object, CS, that is automatically added to the global scope. Each Action Invoker namespace is an object of CS, with its actions available as static methods in two forms:
  • Synchronous (sequentially): Use the action name.
  • Asynchronous (concurrently): Use the action name followed by Async. These methods return a Promise to run the action (even though these actions are not actually run in a separate thread).
If applicable, an action's arguments are specified by passing an object to the method with the arguments defined by using the action's argument names as the object's properties. For example, the following code puts the text "CSPro" onto the clipboard:
CS.Clipboard.putText({
    text: 
"CSPro"
});
When calling actions asynchronously, you can use standard Promise handling, including chaining. This example shows how to execute two actions asynchronously, first to read a data entry application and then to read its associated dictionary:
CS.File.readTextAsync({
    path: 
"Simple CAPI.ent"
})
.then(entryApplicationJson => {
   
const entryApplication = JSON.parse(entryApplicationJson);

   
if( entryApplication.dictionaries === undefined ) {
       
throw new Error("The application does not have an associated dictionary.");
    }

   
return CS.File.readTextAsync({
        path: entryApplication.dictionaries[0].path
    });
})
.then(dictionaryJson => {
   
const dictionary = JSON.parse(dictionaryJson);
    console.log(`The dictionary name is ${dictionary.name}.`);
})
.
catch(error => {
    console.log(`There was an reading the application or dictionary: ${error}`);
});
Argument Types
Arguments to actions are specified in one of the JSON types: string, number, boolean, array, or object. The help page for each action will list the type, or types, permitted for each argument.
Return Values
On successful execution, the result of an action is returned as undefined, or one of the JSON types: string, number, boolean, array, or object. On error, an exception is thrown.
Exception Handling
At runtime, if any of the arguments are invalid, or if there was an error executing the action, the Action Invoker throws an exception. Each action's help page will indicate if the action throws exceptions. If so, you will want to wrap the action call in try/catch, or add a catch method when calling the action asynchronously.
See also: Action Invoker Overview, Action Invoker Execution from JavaScript Run from Web Views