batchPlay Save - "invalid file token used"

,

Hi, im working on an uxp plugin, sadly im struggling with the saving part. It is a batch automation plug in, where multiple images get opened from a folder, processed and then they should be saved one after the other in the output folder, but no image gets saved and instead i get invalid file token.
Tried some different approaches with batch play, saveAs, save, but nothing worked yet.

save function:

async function saveProcessedDocument(doc, originalFile, outputFolder) {
const baseName = originalFile.name.replace(/.[^/.]+$/, “”);
const outputFormatElem = document.getElementById(‘outputFormat’);
const format = outputFormatElem ? outputFormatElem.value : ‘jpg’;
let extension, saveDescriptor;
switch (format) {
case ‘jpg’:
extension = ‘.jpg’;
saveDescriptor = {
_obj: “JPEG”,
extendedQuality: 12, // or whatever quality you prefer
matteColor: { _enum: “matteColor”, _value: “none” }
};
break;
default:
extension = ‘.jpg’;
saveDescriptor = {
_obj: “JPEG”,
extendedQuality: 12,
matteColor: { _enum: “matteColor”, _value: “none” }
};
}

const outputFileName = `${baseName}_processed${extension}`;
const outputPath = outputFolder.nativePath.replace(/\\/g, "/") + "/" + outputFileName;
log(`[DEBUG] Output path string sent: ${outputPath} (type: ${typeof outputPath})`);

try {
    const saveAction = {
        _obj: "saveAs",
        as: saveDescriptor,
        in: { _path: outputPath, _kind: "local" }
    };

    await app.batchPlay([saveAction], {});
    log(`Saved successfully: ${outputFileName}`);
} catch (err) {
    log(`[SAVE] ERROR saving file: ${outputFileName}`);
    log(`[SAVE] ERROR object: ${JSON.stringify(err)}`);
    log(`[SAVE] ERROR (stack): ${err.stack || "no stack"}`);
}

}

maybe i have something fundamentally wrong, i appreciate any help and thank you :slight_smile:

Hey i figured it out, i changed it from batch play to a saveas code, i must have had something wrong before, ill leave the corrected version here, in case someone needs it.

async function saveProcessedDocument(doc, originalFile, outputFolder) {
const baseName = originalFile.name.replace(/.[^/.]+$/, “”);
const outputFormatElem = document.getElementById(‘outputFormat’);
const format = outputFormatElem ? outputFormatElem.value : ‘jpg’;

let extension, saveMethod, saveOptions;
switch (format) {
    case 'jpg':
        extension = '.jpg';
        saveMethod = doc.saveAs.jpg;
        saveOptions = { quality: 12 };
        break;
    case 'png':
        extension = '.png';
        saveMethod = doc.saveAs.png;
        saveOptions = {};
        break;
    case 'tiff':
        extension = '.tif';
        saveMethod = doc.saveAs.tiff;
        saveOptions = {};
        break;
    default:
        extension = '.jpg';
        saveMethod = doc.saveAs.jpg;
        saveOptions = { quality: 12 };
}

const outputFileName = `${baseName}_processed${extension}`;
try {
    const file = await outputFolder.createFile(outputFileName, { overwrite: true });
    // This is the magic line:
    await saveMethod.call(doc.saveAs, file, saveOptions);
    log(`Saved successfully: ${outputFileName}`);
} catch (err) {
    log(`[SAVE] ERROR saving file: ${outputFileName}`);
    log(`[SAVE] ERROR object: ${JSON.stringify(err)}`);
    log(`[SAVE] ERROR (stack): ${err.stack || "no stack"}`);
}

}