How to programatically change text source content from an extension

Some plugins expose functions externally that allow you to perform actions on a source from another source or an extension. Unfortunately, the text source doesn’t support this. Instead, we can utilize the option of updating the actual underlying config of the text source to do this.

Below is a sample script showing how you can change the content of a text source. Written as an anonymous async function but can be achieved using standard promise syntax.

Example

(async () => {

    // Make sure XJS is ready before we use any XJS code
    await xjs.ready();

    // Get the text source we want to manipulate. Returns an array, so we grab the first item it returns
    const targetTextItem = (await xjs.Scene.searchItemsByName('Custom Text'))[0];

    // Load the text source config using loadConfig()
    const textItemConfig = await targetTextItem.loadConfig();

    // Manipulate the text using the text property
    textItemConfig.text = "Billy Bob";

    // Find and attach to the text source using the ID and SearchVideoItem2
    const { _id } = targetTextItem;
    const attachedTextItem = await xjs.exec('SearchVideoItem2', _id);
    
    // Use SetLocalPropertyAsync2 and stringified config
    await xjs.exec(
        'SetLocalPropertyAsync2', 
        'prop:BrowserConfiguration', 
        JSON.stringify(textItemConfig)
    );

}();