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}`);
});
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.
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.
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.