> ## Documentation Index
> Fetch the complete documentation index at: https://docs.directgvr.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Workflow JavaScript

> JavaScript interfaces for running and controlling workflows from a skin.

## Overview

This document describes the classes whereby the Workflow Engine is made available in JavaScript.

## RepositorySource

Repository source enumeration.

| Name |
| - |
| ALIAS |
| CATALOG |
| METAFILELOCAL |
| METAFILEREMOTE |
| STORAGELOCAL |
| STORAGEREMOTE |
| STORAGEDELTA |
| STORAGEPENDING |
| CONFIG |

### nameFromId

Returns a string representing the specified enumeration value.

**Arguments:**

| Name | Type | Description |
| - | - | - |
| id | integer | Repository source value |

## RepositoryState

Repository state enumeration.

| Name | Description |
| - | - |
| INSTALL | An install is required |
| UPDATE | An update is required |
| STALL | Active repository has not processed data in a while |
| ALIVE | Repository sync is in progress |
| LOCALID | Local id of the repository |
| REMOTEID | RemoteId of the repository |
| LOCALMETAFILE | Local metafile has been loaded in its entirety |
| REMOTEMETAFILE | Remote metafile has been loaded in its entirety |
| LOCALMETAFILEALIVE | Local metafile download is currently in progress |
| REMOTEMETAFILEALIVE | Remote metafile download is currently in progress |
| CATALOG | Catalog is present |
| CATALOGALIVE | Catalog retrieval in progress |
| ALIAS | Alias is ready to use |
| ALIASALIVE | Alias retrieval in progress |
| COPY | Copy is currently in progress |
| DELTA | Check for deltas when first downloading |
| BACKUP | Backup store has been configured |
| PENDING | Repository is using the pending storage for writing and/or reading |
| CLEAN | Clean is currently in progress |
| SYNC | Sync is currently in progress |
| REFRESH | Refresh is currently in progress |
| PAUSE | Sync is paused |
| ERASE | Erase is currently in progress |
| APPLY | Apply is currently in progress |
| CLEANPENDING | Cleaning the pending data |

### nameFromId

Returns a string representing the specified enumeration value.

**Arguments:**

| Name | Type | Description |
| - | - | - |
| id | integer | Repository state value |
| separator | string | Optional string separator |

## RepositoryAction

Repository action enumeration.

| Name | Description |
| - | - |
| SYNC | Starts/stops the synchronize process (setting this to false is the same as cancelling) |
| REFRESH | Starts/stops connection to catalog service |
| ACTIVE | Determines if the repository should continue processing events |
| SHUTDOWN | Prepares repository for closure |
| ERASE | Starts/stops erasing of repository |

### nameFromId

Returns a string representing the specified enumeration value.

**Arguments:**

| Name | Type | Description |
| - | - | - |
| id | integer | Repository action value |
| separator | string | Optional string separator |

## Task

The task object represents a task run by a workflow. Typically direct access to task objects are not necessary because the *workflowManager*  keeps track of all local and remote tasks. Because it is possible to run a workflow in a remote process, the task object is only accessible to tasks that are created in the current process space. Accessing the task object is mainly done when developing custom task handler's in JavaScript.

### addError

Adds an error to the task.

**Version:**  5.1

**Arguments:**

| Name | Type | Description |
| - | - | - |
| message | string | Error message to add. |

**Return Value:**

Returns true if successful, false otherwise.

### addWarning

Adds an warning to the task.

**Version:**  5.1

**Arguments:**

| Name | Type | Description |
| - | - | - |
| message | string | Warning message to add. |

**Return Value:**

Returns true if successful, false otherwise.

### complete

Completes the task.

**Version:**  5.1

**Return Value:**

Returns true if successful, false otherwise.

**Arguments:**

| Name | Type | Description |
| - | - | - |
| complete | object | Complete dictionary |

**Changes in 7.0:**

* Added complete object argument

### isCancelled

Gets if the task has been cancelled or not.

**Version:**  5.1

**Return Type:**  boolean

### isCompleted

Gets if the task has been completed or not.

**Version:**  5.1

**Return Type:**  boolean

### getAutoPreventSystemSleep

Returns true if system sleep prevention is automatically handled, false otherwise.

**Version:**  7.2.27

**Return Type:**  boolean

### getBooleanArgument

Gets a boolean argument from the task.

**Version:**  5.1

**Arguments:**

| Name | Type | Description |
| - | - | - |
| name | string | Name of the argument. |

**Return Value:**

Returns a boolean expanded value of the argument.

### getError

Gets a particular error from the task.

**Version:**  5.1

**Arguments:**

| Name | Type | Description |
| - | - | - |
| index | number | Index of error. |

**Return Value:**

Returns the error message at the specified index.

### getErrorCount

Gets the number of errors the task has.

**Version:**  5.1

**Return Type:**  number

### getName

Gets the name of the task.

**Version:**  5.1

**Return Type:**  string

### getNumberArgument

Gets a number argument from the task.

**Version:**  5.1

**Arguments:**

| Name | Type | Description |
| - | - | - |
| name | string | Name of the argument. |

**Return Value:**

Returns a number expanded value of the argument.

### getStringArgument

Gets a string argument from the task.

**Version:**  5.1

**Arguments:**

| Name | Type | Description |
| - | - | - |
| name | string | Name of the argument. |

**Return Value:**

Returns a string expanded value of the argument.

### getTimeElapsed

Gets the amount of time that the task took to complete in seconds.

**Version:**  5.1

**Return Type:**  number

### getType

Get the task's type.

**Version:**  5.1

**Return Type:**  string

### getWarning

Gets a particular warning from the task.

**Version:**  5.1

**Arguments:**

| Name | Type | Description |
| - | - | - |
| index | number | Index of warning. |

**Return Value:**

Returns the warning message at the specified index.

### getWarningCount

Get the number of warnings the task has.

**Version:**  5.1

**Return Type:**  number

### hasArgument

Checks to see if the task was constructed with a particular argument.

**Version:**  5.1

**Arguments:**

| Name | Type | Description |
| - | - | - |
| name | string | Name of the argument. |

**Return Type:**  boolean

### hasError

Checks to see if a task has a particular error message.

**Version:**  5.1

**Arguments:**

| Name | Type | Description |
| - | - | - |
| message | string | Error message to check. |

**Return Value:**

Returns true if task has specified error, false otherwise.

### hasWarning

Checks to see if a task has a particular warning message.

**Version:**  5.1

**Arguments:**

| Name | Type | Description |
| - | - | - |
| message | string | Warning message to check. |

**Return Value:**

Returns true if task has specified warning, false otherwise.

### print

Prints a message to the log associated with the task.

**Version:**  5.1

**Arguments:**

| Name | Type | Description |
| - | - | - |
| message | string | Message to print. |

### runSubAction

Runs a task's action.

**Version:**  5.1

**Arguments:**

| Name | Type | Description |
| - | - | - |
| name | string | Task name. |
| completeCallback | function | Complete function with sender and sub action complete dictionary. |

**Return Value:**

Returns true if successful, false otherwise.

**Changes in 7.0:**

* Added complete callback argument

**Example:**

```json theme={null}
// Workflow might look like:
"myTask": {
   "type": "mySuperTaskType",
   "firstAction": "nameOfNextTask"
},
"nameOfNextTask": {
   "type": "log",
   "message": "Got here!"
}
// Javascript would be:
task.runSubAction("firstAction", function(sender, info) { 
    // Sub action is complete
    task.complete(info);
});
```

### setAutoPreventSystemSleep

Automatically prevent the system sleep based on number of active tasks.

**Version:**  7.2.27

**Arguments:**

| Name | Type | Description |
| - | - | - |
| autoPreventSystemSleep | boolean | If true, it prevents the system from going to sleep when there are active workflow tasks running, excluding `state` entry point tasks. |

**Return Value:**

Returns true if successful, false otherwise.

### setProgress

Sets the progress of a task.

**Version:**  5.1

**Arguments:**

| Name | Type | Description |
| - | - | - |
| progress | number | 0-1 value representing the progress, or -1 for indefinite. |

**Return Value:**

Returns true if successful, false otherwise.

### setStatus

Sets the "status" string on the task.

**Version:**  5.1

**Arguments:**

| Name | Type | Description |
| - | - | - |
| status | string | Status to set. |

**Return Value:**

Returns true if successful, false otherwise.

### setStatusNumber

Sets a status number on the task.

**Version:**  5.1

**Arguments:**

| Name | Type | Description |
| - | - | - |
| key | string | Status key. |
| value | double | Key value. |

**Return Value:**

Returns true if successful, false otherwise.

### setStatusString

Sets a status string on the task.

**Version:**  5.1

**Arguments:**

| Name | Type | Description |
| - | - | - |
| key | string | Status key. |
| value | double | Key value. |

**Return Value:**

Returns true if successful, false otherwise.

### start

Starts a task.

**Version:**  5.1

**Return Value:**

Returns true if successful, false otherwise.

## Workflow

The workflow object represents a workflow that is returned by *workflowManager.add* . Because workflows can be run remotely in another process, it is only possible to call these functions on workflow's in the current process - those in turn should know about their associated remote workflows and tasks.

### addMacro

Adds a macro key value pair to the workflow.

**Version:**  5.1

**Arguments:**

| Name | Type | Description |
| - | - | - |
| key | string | Macro key. |
| value | string | Key value. |

**Notes:**

* Macros in each workflow are isolated and separate from each other.

**Return Value:**

Returns true if successful, false otherwise.

**Example:**

```json theme={null}
workflow.addMacro("bigKey", "smallValue");
```

### addMacroJson

Adds a set of macro key value pairs to the workflow from a JSON string.

**Version:**  5.2

**Arguments:**

| Name | Type | Description |
| - | - | - |
| jsonString | string | JSON formatted string containing dictionary of key/value pairs to add |

**Notes:**

* Macros in each workflow are isolated and separate from each other.

**Return Value:**

Returns true if successful, false otherwise.

**Example:**

```json theme={null}
workflow.addMacroJson("{\"bigKey\": \"smallValue\"}");
```

### cancelTasks

Cancels queued and currently running tasks.

**Version:**  5.1

**Return Value:**

Returns true if successful, false otherwise.

### canCreateTask

Checks to see if it is possible to create the task on the workflow.

**Version:**  5.1

**Arguments:**

| Name | Type | Description |
| - | - | - |
| name | string | Task name to check. |

**Return Type:**  boolean

**Example:**

```json theme={null}
var canDoMyTask = workflow.canCreateTask("myTask");
// If workflow contains a task called "myTask", then queue it up.
if (canDoMyTask) {
    workflow.queueTask("myTask");
}
```

### complete

Completes the workflow

**Version:**  5.1

**Return Value:**

Returns true if successful, false otherwise.

### expandString

Expands a string using the workflow's macro key/value pairs.

**Version:**  5.1

**Arguments:**

| Name | Type | Description |
| - | - | - |
| string | string | String to expand. |

**Return Value:**

Returns expanded string.

**Example:**

```json theme={null}
workflow.addMacro("bigKey", "smallValue");
var expandedString = workflow.expandString("10{bigKey}20");
console.log(expandedString);
// expandedString = "10smallValue20";
```

### hasRemoteTask

Checks to see if a remote task is part of the workflow.

**Version:**  5.1

**Arguments:**

| Name | Type | Description |
| - | - | - |
| instanceId | string | Instance id of the task to check. |

### hasRemoteWorkflow

Checks to see if a remote workflow is part of the workflow.

**Version:**  5.1

**Arguments:**

| Name | Type | Description |
| - | - | - |
| instanceId | string | Instance id of the task to check. |

### hasTask

Checks to see if a task is part of the local workflow.

**Version:**  5.1

**Arguments:**

| Name | Type | Description |
| - | - | - |
| instanceId | string | Instance id of the task to check. |

### isActivated

Checks to see if there are any events that are running.

**Version:**  5.1

### isGlobal

Checks to see if the workflow is global (not run by another workflow).

**Version:**  5.1

### isTaskQueued

Checks to see if a task is queued and waiting to be run.

**Version:**  5.1

**Arguments:**

| Name | Type | Description |
| - | - | - |
| name | string | Task name to check. |

### isTaskRunning

Checks to see if a task is running.

**Version:**  5.1

**Arguments:**

| Name | Type | Description |
| - | - | - |
| name | string | Task name to check. |

### queueTask

Queues a task. Will wait for any already running tasks to finish before running.

**Version:**  5.1

**Arguments:**

| Name | Type | Description |
| - | - | - |
| name | string | Task name. |

**Return Value:**

Returns true if successful, false otherwise.

**Example:**

```json theme={null}
workflow.on("taskComplete", function(info) {
    if (info.name == "myTask") {
        console.log("myTask is complete!");
    }
});
workflow.queueTask("myTask");
```

### queueTaskWithMacros

Queues a task with macros. Will wait for any already running tasks to finish before running.

**Version:**  6.1

**Arguments:**

| Name | Type | Description |
| - | - | - |
| name | string | Task name. |
| macros | object | Macro key/value pair dictionary. |

**Return Value:**

Returns true if successful, false otherwise.

**Example:**

```json theme={null}
workflow.on("taskComplete", function(info) {
    if (info.name == "myTask") {
		var myData = workflow.expandString("{myData}");
        console.log("myTask is complete! {0}".format(myData));
    }
});
workflow.queueTaskWithMacros("myTask", { "myData": "123456" });
```

### runTask

Runs a task immediately.

**Version:**  5.1

**Arguments:**

| Name | Type | Description |
| - | - | - |
| name | string | Task name. |

**Return Value:**

Returns true if successful, false otherwise.

**Notes:**

* This function has a potential to be a blocking operation. Consider using queueTask instead.

**Example:**

```json theme={null}
workflow.on("taskComplete", (info) => {
    if (info.name == "myTask") {
        console.log("myTask is complete!");
    }
});
workflow.runTask("myTask");
```

### runTaskWithMacros

Runs a task immediately with specified macros.

**Version:**  6.1

**Arguments:**

| Name | Type | Description |
| - | - | - |
| name | string | Task name. |
| macros | object | Macro key/value pair dictionary |

**Return Value:**

Returns true if successful, false otherwise.

**Notes:**

* This function has a potential to be a blocking operation. Consider using queueTaskWithMacros instead.

**Example:**

```json theme={null}
workflow.on("taskComplete", (info) => {
    if (info.name == "myTask") {
		var myData = workflow.expandString("{myData}");
        console.log("myTask is complete! {0}".format(myData));
    }
});
workflow.runTaskWithMacros("myTask", { "myData": "123456" });
```

### setStatus

Sets the "status" string on the workflow.

**Version:**  5.1

**Arguments:**

| Name | Type | Description |
| - | - | - |
| status | string | Status to set. |

**Return Value:**

Returns true if successful, false otherwise.

### setStatusNumber

Sets a status number on the workflow.

**Version:**  5.1

**Arguments:**

| Name | Type | Description |
| - | - | - |
| key | string | Status key. |
| value | double | Key value. |

**Return Value:**

Returns true if successful, false otherwise.

### setStatusString

Sets a status string on the workflow.

**Version:**  5.1

**Arguments:**

| Name | Type | Description |
| - | - | - |
| key | string | Status key. |
| value | double | Key value. |

**Return Value:**

Returns true if successful, false otherwise.

## WorkflowManager

The workflow manager has the responsibility of starting workflows and tracking all workflow events. It is necessary for it to track all task and workflow information that is sent to it due to the possibility of running workflows in an elevated process using the *workflowRun*  task.

### add

Adds new workflows.

**Version:**  5.1

**Arguments:**

| Name | Type | Description |
| - | - | - |
| info | string or object | URL of the workflow to start or the workflow start parameters. |
| complete | function | Callback for when the workflow has been started. |

**Complete Callback Arguments:**

| Name | Type | Description |
| - | - | - |
| workflow | workflow object | Object used to track the workflow |
| info | object | Start parameters. |

**Example:**

```json theme={null}
workflowManager.add("https://myurl/workflow.json", function(workflow, info) {
    console.log("Workflow started - {0}".format(interop.getInstanceId(workflow));
    workflow.on("taskStart", function(info) {
         console.log("taskStart - {0}".format(JSON.stringify(info)));
    });
});
```

### addDependency

Creates a new dependency using the ids associated with each workflow when they are started.

**Version:**  5.2

**Arguments:**

| Name | Type | Description |
| - | - | - |
| id | string | Id of the item that is depended upon another |
| dependencyId | string | Id of the dependency |

**Return Value:**

True if successful, false otherwise.

### clearTaskAvailability

Clears tasks available for a workflow to run.

**Version:**  5.2

**Arguments:**

| Name | Type | Description |
| - | - | - |
| workflow | string | Instance id of the workflow. |

**Return Value:**

True if successful, false otherwise.

### getFlowControl

Gets whether or not to manage triggering the start and unload tasks automatically.

**Version:**  5.2

### getMaximumActiveTasks

Gets the maximum number of tasks that can be active at any one time.

**Version:**  5.2

### isTaskInWorkflow

Checks to see if a particular task is run as part of a particular workflow.

**Version:**  5.1

**Arguments:**

| Name | Type | Description |
| - | - | - |
| task | string | Instance id of the task. |
| workflow | string | Instance id of the workflow. |
| traverse | boolean | If *true* , traverse up workflow hierarchy, *false* otherwise. |

**Example:**

```json theme={null}
workflowManager.add("https://myurl/workflow.json", function(workflow, info) {
    console.log("Workflow started - {0}".format(interop.getInstanceId(workflow));

    notificationCenter.addObserver("Download", "Status", function(sender, info) {
        if (!workflowManager.isTaskInWorkflow(info.task, workflow)) {
            return;
        }
        console.log("Remaining bytes {0}".format(info.remainingBytes));
    });
});
```

### isUnloaded

Checks to see if any loaded workflows remain running.

**Version:**  5.1

### isUnloading

Property used to indicate if the workflow manager is unloading workflows.

**Version:**  5.1

### queueAvailableTasks

Queues all the available tasks with a specific name across all workflows.

**Version:**  5.2

**Arguments:**

| Name | Type | Description |
| - | - | - |
| name | string | Name of the task to make available. |

**Return Value:**

True if successful, false otherwise.

### queueAvailableTasksByWorkflow

Queues all the available tasks with a specific name for a certain workflow.

**Version:**  5.2

**Arguments:**

| Name | Type | Description |
| - | - | - |
| name | string | Name of the task to make available. |
| workflow | string | Instance id of the workflow. |

**Return Value:**

True if successful, false otherwise.

### queueTask

Starts a task if there are open spots, otherwise it queues the task.

**Version:**  5.2

**Arguments:**

| Name | Type | Description |
| - | - | - |
| workflow | string | Instance id of the workflow. |
| name | string | Name or in the case of multiple names, it will queue the first one it can. Multiple names are comma-delimited. |

**Return Value:**

True if successful, false otherwise.

### registerTaskHandler

Registers a custom task handler for a custom task type.

**Version:**  5.1

**Arguments:**

| Name | Type | Description |
| - | - | - |
| name | string | Name of the task |
| TypeCreator | class | Class to create when encountering the custom task type. |

**Example:**

```json theme={null}
function BrowseForFolder(instanceId) {
    var self = this;

    this.instanceId = instanceId;
    this.task = createTask(instanceId);

    this.observers = [];
    this.observers.push(notificationCenter.addInstanceObserver("Task", "Complete", this.task, (sender, info) => { self.onComplete(info); }));

    this.onStart();
}

BrowseForFolder.prototype = {
    release: function() {
        this.observers.forEach(function(observer) {
            observer.release();
            observer = null;
        });
        this.observers = [];
        if (this.task) {
            this.task.release();
            this.task = null;
        }
    },
    onStart: function() {
        var self = this;
        var suggestedPath = app.expandString("{userDesktop}");
        if (this.task.hasArgument("suggestedPath")) {
            suggestedPath = this.task.getStringArgument("suggestedPath");
        }
        skinWindow.browseForFolder(app.localizeString("UI_BrowseForFolder"), suggestedPath, (sender, info) => { 
            if (info.successful) {
                self.task.setStatusString("path", info.path);
            } else {
                self.task.addError("UI_UserAbort");
            }
            self.task.complete();
        });
    },
    onComplete: function() {
        this.release();
    }
};

workflowManager.registerTaskHandler("browseForFolder", BrowseForFolder);
```

### setDependencyTaskNames

Sets the names of the tasks that are checked for dependencies before starting. By default tasks are checked from in bottom-to-top order, but task names that start with an exclamation mark are checked from top-to-bottom order.

**Version:**  5.2

**Arguments:**

| Name | Type | Description |
| - | - | - |
| dependencyTaskNames | string | Comma separate list of names of tasks |

**Return Value:**

True if successful, false otherwise.

### setFlowControl

Sets whether or not to manage triggering the start and unload tasks automatically.

**Version:**  5.2

**Arguments:**

| Name | Type | Description |
| - | - | - |
| flowControl | boolean | If *true* , will trigger start task after load task, and unload task after start task. |

**Return Value:**

True if successful, false otherwise.

### setMaximumActiveTasks

Sets the maximum number of tasks that can be active at any one time.

**Version:**  5.2

**Arguments:**

| Name | Type | Description |
| - | - | - |
| maximumActiveTasks | number | Maximum number of active tasks |

**Return Value:**

True if successful, false otherwise.

### setProgress

Sets the progress for an active task.

**Version:**  5.2

**Arguments:**

| Name | Type | Description |
| - | - | - |
| workflow | string | Instance id of the workflow. |
| name | string | Name or in the case of multiple names, it will queue the first one it can. Multiple names are comma-delimited. |
| progress | number | Progress of the task 0-1.0. |

**Return Value:**

True if successful, false otherwise.

### setProgressTaskNames

Sets the names of the tasks whose progress is tracked by the manager.

**Version:**  5.2

**Arguments:**

| Name | Type | Description |
| - | - | - |
| progressTaskNames | string | Comma separate list of names of tasks |

**Return Value:**

True if successful, false otherwise.

### setQueueTaskNames

Sets the names of the tasks that can be queued. If a task whose name matches one of these, it will be queued, otherwise it will be run immediately.

**Version:**  5.2

**Arguments:**

| Name | Type | Description |
| - | - | - |
| queueTaskNames | string | Comma separate list of names of tasks |

**Return Value:**

True if successful, false otherwise.

### setTaskAvailability

Sets whether or not a task is available for a workflow to run.

**Version:**  5.2

**Arguments:**

| Name | Type | Description |
| - | - | - |
| workflow | string | Instance id of the workflow. |
| name | string | Name of the task to make available |
| availabile | boolean | Availability of task to run. |

**Return Value:**

True if successful, false otherwise.

### unload

Unloads all the workflows and fires the complete callback when done.

**Version:**  5.1

**Arguments:**

| Name | Type | Description |
| - | - | - |
| complete | callback | Complete callback |

### unregisterTaskHandler

Unregisters a custom task handler for a custom task type.

**Version:**  5.1

**Arguments:**

| Name | Type | Description |
| - | - | - |
| name | string | Name of the task |

**Example:**

```json theme={null}
workflowManager.unregisterTaskHandler("browseForFolder");
```


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.