JS Preprocessors (for Adobe products)
Cerebro Support avatar
Written by Cerebro Support
Updated over a week ago

For js processors to work, you need a .js file specified in processors. Inside the file, there must be a function specified in function. JavaScript code is executed inside nodejs, so it is possible to include built-in modules such as path, fs, net, etc.
Following helper constants and functions have been added to the execution context:

  • __PROCESSOR_PATH__ is a constant containing the full path to the current processor file. To get a path without a filename, use the dirname method from the path module.

  • callFunc is a function to call host application code:

    function callFunc(method: string, …args: any): Promise<any>

    To execute arbitrary code inside the host application, use the csInterface.evalScript method.
    Signature:

    function evalScript(script: string, callback: (result: string) => void)
  • loadScriptFile is a function to include a JSX-file in the host application:

    function loadScriptFile(scriptPath: string): Promise<bool>
  • csInterface — is an object for working with the host application.

Пример процессоров:

  1. version_pre

First, an additional script is loaded into the host application that describes the dialog box and the function to open it. Next, the showWindow method from the example1.jsx script is called, which opens the window. Depending on the pressed button, the text of the report changes.
version_pre.js

async function version_pre(task_info, args) 
{
await loadScriptFile(path.join(path.dirname(__PROCESSOR_PATH__),
"example1.jsx"));
const result = await callFunc("showWindow");
console.log("result", result);
args.report.plain_text = result;
return args;
}

example1.jsx

var dialogResult = "";

function createDockableUI(thisObj) {
var dlg = new Window("dialog", undefined, undefined, { resizeable: false });
dlg.text = "";

var group1 = dlg.add("group", undefined, { name: "group1" });

var btn = group1.add("button", undefined, undefined, { name: "btn" });
btn.text = "Apply";

btn.onClick = function() {
dialogResult = "Apply";
dlg.close();
}

return dlg;
}

var dlg = createDockableUI(this);

showWindow = function() {
dlg.center();
dlg.show();
return $._ext.successResult(dialogResult);
}

2. screenshot_replace.
An image of a kitten is requested from an external resource, saved to a temporary file, and the final path is written to the argument. As a result, the attached file will have a kitten thumbnail.

screenshot_replace.js

function download_file(file, url) {
return new Promise((resolve) => {
https.get(url, function (response) {
response.pipe(file);
resolve(true);
});
});
}

async function screenshot_replace(task_info, args) {
console.log("task_info", task_info);
console.log("args", args);

const response = await fetch("https://aws.random.cat/meow", {
method: "POST",
});
const json = await response.json();
const catUrl = json.file;
const file = fs.createWriteStream("%temp%/file.jpg");
await download_file(file, catUrl);

args.image_path = file.path;
return args;
}
Did this answer your question?