How can a psjs script be used to loop through images and save them as JPG?

How can this JSX script be modified into a .psjs script? Thank you.

var inputFolder = Folder.selectDialog("Select the folder with images");
var outputFolder = Folder.selectDialog("Select the folder to save processed images");
var fileList = inputFolder.getFiles();
for (var i = 0; i < fileList.length; i++) {
    var doc = app.open(fileList[i]);
    var file = new File(outputFolder + "/" + i + ".jpg");
    doc.saveAs(file, new JPEGSaveOptions(), true, Extension.LOWERCASE);
    doc.close(SaveOptions.DONOTSAVECHANGES);
}

Try this out and let me know if it works for you? Cheers!

const { app, core } = require("photoshop");
const { storage } = require("uxp");
const fs = storage.localFileSystem;

async function main() {
    const inputFolder = await fs.getFolder();
    if (!inputFolder) return;
    console.log(`selected inputFolder ${inputFolder}`);

    const outputFolder = await fs.getFolder();
    if (!outputFolder) return;
    console.log(`selected outputFolder ${inputFolder}`);

    const entries = await inputFolder.getEntries();
    const files = entries.filter((entry) => entry.isFile);

    console.log(`found ${files.length} files`);

    try {
        for (const file of files) {
            console.log(`processing ${file.name}`);

            // create an entry in the outputFolder for saving
            const outputFile = await outputFolder.createEntry(`${file.name}.jpg`);

            // open the file in Ps and save a JPEG copy
            await core.executeAsModal(
                async () => {
                    const document = await app.open(file);

                    // Save as a Copy (High JPG quality)
                    // https://developer.adobe.com/photoshop/uxp/2022/ps_reference/classes/document/#saveas
                    const savedFile = await document.saveAs.jpg(
                        outputFile,
                        { quality: 12 },
                        true
                    );
                    console.log(`saved file ${savedFile}`);

                    document.close();
                },
                { commandName: "Open and Save JPEG" }
            );
        }
    } catch (error) {
        console.log(error);
    }
}

await main();
1 Like

I did a quick test and it worked fine here.

I had already created a similar script, but this one is cleaner.

Thanks for sharing your code.

1 Like

This effect is amazing, thank you!

1 Like