Saving a JPG to a selected folder

Hi, I’m trying to save out a JPG to a user-selected folder. I’ve tried a few methods and had it saving successfully to a temp folder. I cannot figure out, though, how to save it to a user-selected folder.

This brings up the dialog for selecting a folder and saves it as a variable:

const userFolder = require('uxp').storage.localFileSystem.getFolder();

Once I’ve made all my changes to the layers I initiate the export function with a filename:

async function exportFile(filename) {
    const newFile = await userFolder.createFile(filename + '.jpg');
    console.log("*** Path: " + newFile.nativePath);
}

This doesn’t seem to work however and there is no error in the console due to it being an async function. I suspect it has something to do with userFolder but I have no idea what to try next.

Any assistance would be greatly appreciated.

Can you go into more detail as to what doesn’t work? Is the log not being displayed?

Hi, thanks for replying :slight_smile: Images simply will not save out to a user-selected folder. What is the right way to approach this? UXP or batchplay?

My latest endeavour has been the following to no avail:

await userFolder.createFile(fileName + ".jpg").then(async result => {
            console.log("*** Path: " + result.nativePath);
            await fs.createSessionToken(result).then(async token => {
                document.save(token);
            });
    });

and:

await fs.getFileForSaving(fileName + ".jpg").then(async entry => {
    console.log("*** Path: " + result.nativePath);
    await fs.createSessionToken(entry).then(async token => {
        document.save(token);
    });
});

Document#save doesn’t need you to create a session token; it’ll do so on its own. Also, there’s no need to mix await and Promises like you’re doing here. Also, you’re referring to document, but in UXP, document generally refers to the HTML document of the plugin, not Photoshop’s document. Use activeDocument from require("photoshop").app instead.

This should work for you:

async function saveFileToFolder(userFolder, fileName="test") {
    const file = await userFolder.createFile(`${fileName}.jpg`);
    const activeDocument = require("photoshop").app.activeDocument;
    return activeDocument.save(file);
}
1 Like

Thank you so much! It’s finally working :scream:

Now I just need to figure out how to specify the jpg export quality.

Did you get anywhere with the export quality params?

Just pass an options object as the second argument.
activeDocument.save(file, {quality: 12})

1 Like

The Export As window that opens has lots of things that I can add. I’m interested in switching on the C2PA and the copyright details. Any ideas?

I don’t think Export As is in the API yet; you could try using Alchemist to see if it’s triggerable via batchPlay.
If not, then you could set your metadata manually first and then save.
There’s been a couple of recent threads regarding metadata that have more detail - it’s not exactly straightforward.

1 Like