Saving Multiple Files in the same folder

For some reason I still can’t wrap my head around the save/token system in UXP

I’ve got a function that saves 2 different files in a folder. At the moment I’m only able to get the function to run if it chooses the folder to save the file every time.

Is there a way to select the save folder on the first file, and then keep it as the save folder for the next one so the user is not alerted/cant change it?

It runs over 30+ files so for the user to click save 30 times is tedious

Current Function Below

document.querySelector("Btn01").onclick = () => {
    makeWebFiles();
}

async function makeWebFiles() {

    const batchPlay = require("photoshop").action.batchPlay;
    const fs = require("uxp").storage.localFileSystem;
    const pluginFolder = await fs.getPluginFolder();
    const doc1 = await pluginFolder.getEntry("doc1.tif");
    const doc2 = await pluginFolder.getEntry("doc2.tif");

    await app.open(doc1);
    await saveWeb("doc1.jpg");
    await app.open(doc2);
    await saveWeb("doc2.jpg");

    async function saveWeb(saveFileName) {

        const persistentFolder = await fs.getFolder();
        const newFile = await persistentFolder.createFile(saveFileName, { overwrite: true });
        const saveFile = await fs.createSessionToken(newFile);

        const result = await batchPlay(
            [
                {
                    "_obj": "save",
                    "as": {
                        "_obj": "JPEG",
                        "extendedQuality": 8,
                        "matteColor": {
                            "_enum": "matteColor",
                            "_value": "none"
                        }
                    },
                    "in": {
                        "_path": saveFile,
                        "_kind": "local"
                    },
                    "copy": true,
                    "lowerCase": true,
                    "saveStage": {
                        "_enum": "saveStageType",
                        "_value": "saveBegin"
                    },
                },
                {
                    "_obj": "close",
                    "saving": {
                        "_enum": "yesNo",
                        "_value": "no"
                    },
                }
            ], {
            "synchronousExecution": false,
            "modalBehavior": "fail"
        });
    }
}

Without testing it… it should be something like this

const batchPlay = require("photoshop").action.batchPlay;
    const fs = require("uxp").storage.localFileSystem;
    const pluginFolder = await fs.getPluginFolder();
    const doc1 = await pluginFolder.getEntry("doc1.tif");
    const doc2 = await pluginFolder.getEntry("doc2.tif");
    const persistentFolder = await fs.getFolder();


//Probably best to create a  loop to assign the file, create the token and save for each file

    await app.open(doc1);
    let newFile = await persistentFolder.createFile("doc1.jpg", { overwrite: true });
    let saveFile = await fs.createSessionToken(newFile);
    await saveWeb(saveFile);
   //may want to add a doc close step

    await app.open(doc2);
    newFile = await persistentFolder.createFile("doc2.jpg", { overwrite: true });
    saveFile = await fs.createSessionToken(newFile);
    await saveWeb(saveFile);
   //may want to add a doc close step


async function saveWeb(saveFile) {

        const result = await batchPlay(
            [
                {
                    "_obj": "save",
                    "as": {
                        "_obj": "JPEG",
                        "extendedQuality": 8,
                        "matteColor": {
                            "_enum": "matteColor",
                            "_value": "none"
                        }
                    },
                    "in": {
                        "_path": saveFile,
                        "_kind": "local"
                    },
                    "copy": true,
                    "lowerCase": true,
                    "saveStage": {
                        "_enum": "saveStageType",
                        "_value": "saveBegin"
                    },
                },
                {
                    "_obj": "close",
                    "saving": {
                        "_enum": "yesNo",
                        "_value": "no"
                    },
                }
            ], {
            "synchronousExecution": false,
            "modalBehavior": "fail"
        });
    }
}
1 Like

Fantastic, that works - thank you so much. It’s so much easier to understand once there is a relevant code example