Scripting Reference

Script module

Represents the UXP module that contains the properties and methods used for scripting.

Usage

const { script } = require("uxp");

Properties

Name
Type
Access
Description
executionContext
ExecutionContext
readOnly
ExecutionContext passed by the host when invoking Script

ExecutionContext

Passed by the host when invoking scripts. Contains the following:

Usage

const { script } = require("uxp");
const executionContext = script.executionContext;
console.log("isCancelled: ", executionContext.isCancelled);

Properties

Name
Type
Access
Description
isCancelled
bool
readOnly
Used to check if the execution context has been cancelled by the client or host.
hostControl
Object
readOnly
Object with 4 properties, detailed below.

hostControl

The hostControl property is used to suspend and resume history states.

Methods: hostControl

History state suspension

executionContext.hostControl.suspendHistory(options)

Parameters

Name
Type
Description
options
Object
documentID: ID of the document whose history state should be suspended.
name: name that is used for the history state - visible in the History panel

executionContext.hostControl.resumeHistory(suspensionID, commit)

Parameters

Name
Type
Description
suspensionID
string
the suspension identifier that was returned from suspendHistory
commit
bool
If true, then the current document state is committed and a history state is created. If false, the document is rolled back to the time when the state was suspended. (optional and defaults to true)

Example

This example demonstrates suspending the history state on a target document, then resuming the state after modifying the document.

let hostControl = executionContext.hostControl;
// Get an ID for a target document
let documentID = await getTargetDocument();

// Suspend history state on the target document
// This will coalesce all changes into a single history state called
// 'Custom Command'
let suspensionID = await hostControl.suspendHistory({
    "documentID": documentID,
    "name": "Custom Command"
});

// modify the document
// . . .

// resume the history state
await hostControl.resumeHistory(suspensionID);

Automatic document closing

executionContext.hostControl.registerAutoCloseDocument(docID)

executionContext.hostControl.unregisterAutoCloseDocument(docID)

Parameters

Name
Type
Description
docID
number
The ID of the document of this history state

Example

This example demonstrates creating and marking a document as "auto close" first. After adding some contents to the page, the document is unregistered from the set of auto close documents. If the user cancels while the script is running, the document is closed.

let hostControl = executionContext.hostControl;
let docID = await createDocument();
await hostControl.registerAutoCloseDocument(docID);

// Add contents to docID
...

await hostControl.unregisterAutoCloseDocument(docID);

Events

onCancel

executionContext.onCancel.addListener((reason) => {
		// reason would be a json object set by the host while cancelling
		reject("Script Cancelled");
});