[UXP Plugin] SaveAs to Specific Directory Not Working in Plugin

Hello everyone,

I’m a newcomer to UXP plugin development, and I’ve been struggling with the SaveAs functionality for several days. I would greatly appreciate any assistance.

My objective is to save documents to a specific directory rather than prompting for a location. I’ve set “localFileSystem”: “fullAccess” in my manifest (V5).

Here’s my current code:

const uxp = require("uxp");
const fs = uxp.storage.localFileSystem;
const photoshop = require("photoshop");
const app = photoshop.app;

require('photoshop').core.executeAsModal(async () => {
    const activeDocument = app.activeDocument;
    const timestamp = new Date().getTime();
    const fileName = `doc_${timestamp}.psd`;
    const folder = await fs.getEntryWithUrl('file:D:/wenjian');
    const file = await folder.createFile(fileName, { overwrite: true });
        
    await activeDocument.saveAs.psd(file);
}, { commandName: 'Save Document' });

Interestingly, this code executes successfully in the Adobe UXP Developer Tools console panel. However, when integrated into my plugin, it fails to work.
Has anyone encountered a similar issue or could provide guidance on the correct implementation? Thank you in advance for your help!

I don’t know where you want to save it exactly, but if you want to save on D drive on WIndows OS, I replaced the file path with below the code.

const folder = await uxpfs.getEntryWithUrl("D:\\wenjian");

For my environment, I successfully saved the document.

Correct me, if I’m wrong (I’m still learning too :slight_smile: ), but I think You’ll still need the user to grant access to a specific folder anyway.
However You can check, if the plugin has already access to that folder, and if not, let the user chose an output folder, then create a session token from it and store it in localStorage or even create a persisten token, that lasts ‘forever’ (kind of (s. description)). The next time You’d want to save a file, You’ll try to load the token first and create a new file entry from that, which then can be (over)written to that exact same location.

Also, I’d wrap my code in a try catch block to see what exactly goes wrong.

const storage = uxp.storage;
const { domains } = storage;
const fs = storage.localFileSystem;

async function mySaveFunction() {
  require('photoshop').core.executeAsModal(async () => {
    try { 
      const activeDocument = app.activeDocument;
      const timestamp = new Date().getTime();
      const fileName = `doc_${timestamp}.psd`;
      const lsKey = 'super-nice-output-folder'
      let folderToken = localStorage.getItem(lsKey);
      
      // Nothing in localStorage yet.
      if(!folderToken) {
        // Open file picker.
        const folder = await fs.getFolder({ initialDomain: domains.userDocuments })
        
        // User cancelled file picker
        if(!folder) {
          throw 'User cancelled file picker';
        };
        
        // Store the selected folder for the current session
        // If You want persistent access, check out `createPersistentToken`
        // in the docs.
        folderToken = await fs.createSessionToken(folder);
        localStorage.setItem(lsKey, folderToken);
      }
      
      const folderEntry = await fs.getEntryForSessionToken(folderToken)
      
      // Will only be overwritten, if the filename is persistent.
      const file = await folderEntry.createFile(`${fileName}`, { overwrite: true });
      await activeDocument.saveAs.psd(file);
      } catch(e) {
        console.error('[saveAs Test]\n', e)
      }
  }, { commandName: 'Save Document' });
}

Edit: Sorrry, correct a tiny mistake.