Skip to main content
S_Stoianov
Inspiring
April 19, 2025
Answered

UXP - Batch export Webp

  • April 19, 2025
  • 4 replies
  • 1778 views

Im trying to write a script that batch exports(among other things) .webp files.

I know there is a great script by Stephen Marsh but i want to do this in the UXP.

So far I got to the point where im exporting them sucessfuly, but since I use "Save as a Copy" from Alchemist  the files all got this annoying "copy" in the end of the name. How do I pass a name in here?:

 

 

async function exportWEBP(destinationFolder) {
   const result = await batchPlay(
      [
         {
            _obj: "save",
            as: {
               _obj: "WebPFormat",
               compression: {
                  _enum: "WebPCompression",
                  _value: "compressionLossless"
               },
               includeXMPData: false,
               includeEXIFData: false,
               includePsExtras: false
            },
            in: {
               _path: await tokenify(destinationFolder.nativePath),
               _kind: "local"
            },
            // fileDoNotAppendCopy: true,
            documentID: 67,
            copy: true,
            lowerCase: true,
            saveStage: {
               _enum: "saveStageType",
               _value: "saveSucceeded"
            },
            _options: {
               dialogOptions: "dontDisplay"
            }
         }
      ],
      {}
   );
}



Correct answer S_Stoianov

Guys I think I did it! The key was here  in the ddbell`s post. Here is the code, it takes a folder and a string as parameters:

async function exportWEBP(destinationFolder, fileName) {
   let activeDocumentID = app.activeDocument.id;
   console.log(activeDocumentID);
   let newFile = await destinationFolder.createFile(fileName, { overwrite: true });
   let saveWebpFile = await fs.createSessionToken(newFile);
   await core.executeAsModal(async () => {
       const result = await batchPlay(
           [
              {
                 _obj: "save",
                 as: {
                    _obj: "WebPFormat",
                    compression: {
                       _enum: "WebPCompression",
                       _value: "compressionLossless"
                    },
                    includeXMPData: false,
                    includeEXIFData: false,
                    includePsExtras: false
                 },
                 in: {
                    _path: saveWebpFile,
                    _kind: "local"
                 },
                 // fileDoNotAppendCopy: true,
                 documentID: activeDocumentID,
                 copy: true,
                 lowerCase: true,
                 saveStage: {
                    _enum: "saveStageType",
                    _value: "saveBegin"
                 },
                 _options: {
                    dialogOptions: "dontDisplay"
                 }
              }
           ],
           {}
        );
   }, );
}

 

4 replies

S_Stoianov
Inspiring
April 20, 2025

@Stephen Marsh @Ciccillotto Here is the fully working code. You need to have an opened photoshop doc with few artboards in it. The script will prompt you to pick a export destination and then will export each artboard as a webp.

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

const {executeAsModal} = require("photoshop").core;

const doc = app.activeDocument;
exportArtboardsAsWEBP();

async function exportArtboardsAsWEBP() {


const  destinationFolder = await uxp.storage.localFileSystem.getFolder();
await core.executeAsModal(async () => {
for (let i = 0; i < doc.artboards.length; i++) {
    const artboard = doc.artboards[i];
    await selectArtboard(artboard.name)
    await duplicateArtboard(artboard.name);
    console.log(doc.name);
    await exportWEBP(destinationFolder, artboard.name);
    app.activeDocument.closeWithoutSaving();
    // // await app.activeDocument.closeWithoutSaving();
    app.activeDocument = doc;

    }
})

}


// exportWEBP takes FOLDER and STRING as parameters!!!
async function exportWEBP(destinationFolder, fileName) {
    let activeDocumentID = app.activeDocument.id;
    console.log(activeDocumentID);
    let newFile = await destinationFolder.createFile(fileName, { overwrite: true });
    let saveWebpFile = await fs.createSessionToken(newFile);
    await core.executeAsModal(async () => {
        const result = await batchPlay(
            [
               {
                  _obj: "save",
                  as: {
                     _obj: "WebPFormat",
                     compression: {
                        _enum: "WebPCompression",
                        _value: "compressionLossless"
                     },
                     includeXMPData: false,
                     includeEXIFData: false,
                     includePsExtras: false
                  },
                  in: {
                     _path: saveWebpFile,
                     _kind: "local"
                  },
                  // fileDoNotAppendCopy: true,
                  documentID: 999,
                  copy: true,
                  lowerCase: true,
                  saveStage: {
                     _enum: "saveStageType",
                     _value: "saveBegin"
                  },
                  _options: {
                     dialogOptions: "dontDisplay"
                  }
               }
            ],
            {}
         );
    }, );
 }


 async function duplicateArtboard(artboardName) {
    console.log(artboardName);
    const result = await batchPlay(
       [
          {
             _obj: "make",
             _target: [
                {
                   _ref: "document",
                }
             ],
             name: artboardName,
             using: {
                _ref: "layer",
                _enum: "ordinal",
                _value: "targetEnum"
             },
             version: 5,
             _options: {
                dialogOptions: "dontDisplay"
             }
          }
       ],
       {}
    );

}


  async function selectArtboard(artboardName) {
   await core.executeAsModal(async () => {
    //   app.activeDocument = app.activeDocument; 
       await batchPlay(
           [
               {
                   _obj: "select",
                   _target: [
                       {
                           _ref: "layer",
                           _name: artboardName,
                       },
                   ],
                   makeVisible: false,
               },
           ],
           {
               synchronousExecution: false,
               modalBehavior: "execute",
           }
       );

      //  console.log("Selected Artboard:", artboardName);
   }); 
}

 

Stephen Marsh
Adobe Expert
April 21, 2025

@S_Stoianov 

 

Thank you again for trying. I have tested in both 2024 and 2025 and no WebP files are saved. I do have a dialog to select a folder, and I then see a progress dialog briefly pop up, but that's it.

New Participant
April 22, 2025

Juts tested it on my work PC. It works as intended. Do you have artboards? Any errors in the console?

S_Stoianov
S_StoianovAuthorCorrect answer
Inspiring
April 20, 2025

Guys I think I did it! The key was here  in the ddbell`s post. Here is the code, it takes a folder and a string as parameters:

async function exportWEBP(destinationFolder, fileName) {
   let activeDocumentID = app.activeDocument.id;
   console.log(activeDocumentID);
   let newFile = await destinationFolder.createFile(fileName, { overwrite: true });
   let saveWebpFile = await fs.createSessionToken(newFile);
   await core.executeAsModal(async () => {
       const result = await batchPlay(
           [
              {
                 _obj: "save",
                 as: {
                    _obj: "WebPFormat",
                    compression: {
                       _enum: "WebPCompression",
                       _value: "compressionLossless"
                    },
                    includeXMPData: false,
                    includeEXIFData: false,
                    includePsExtras: false
                 },
                 in: {
                    _path: saveWebpFile,
                    _kind: "local"
                 },
                 // fileDoNotAppendCopy: true,
                 documentID: activeDocumentID,
                 copy: true,
                 lowerCase: true,
                 saveStage: {
                    _enum: "saveStageType",
                    _value: "saveBegin"
                 },
                 _options: {
                    dialogOptions: "dontDisplay"
                 }
              }
           ],
           {}
        );
   }, );
}

 

Stephen Marsh
Adobe Expert
April 20, 2025

@S_Stoianov 

 

Thank you for trying, I can't get any of the requested fully working demo code samples to work. UXP is a step backwards from ES for me, but that is likely my failing.

S_Stoianov
Inspiring
April 20, 2025

So far UXP Its a tremendous pain in the butt for me too, but since it was announced as The way of writing plugins in the future I want to get used to it. Anyway im glad I found a way to batch export webp. I was banging my head a couple of day over this. If anyone decides to try it I would explain in details. Cheers!

Inspiring
April 19, 2025

I tried your script for webp and it doesn't copy,
it seems ok.

Stephen
this must be like this, it's correct
// fileDoNotAppendCopy: true,

Also where does the following come from?
nativePath

this is a piece of code missing that is at the beginning and tells where to save the file
S_Stoianov did not insert it.

Stephen Marsh
Adobe Expert
April 19, 2025

@S_Stoianov 

 

I have only been met with frustration and sorrow when it comes to UXP scripting when wishing to save files as simply as is possible using ExtendScript.

 

I'm not in front of a computer, however, why did you comment out this line?

 

 // fileDoNotAppendCopy: true,


Also where does the following come from?

 

nativePath

 

S_Stoianov
Inspiring
April 20, 2025

// fileDoNotAppendCopy: true

This is my desperate attempt to make it work by listening to the checkbox in Preferences/File handling/Remove "copy". It doesn't work sadly.

nativePath

I'm passing a newly created folder and then tokenify the actual path.

The code works but with a "copy" added to the file name. Very strange decision from Adobe to make "save as copy" the only way you can export webp. 

Stephen Marsh
Adobe Expert
April 20, 2025

@S_Stoianov 

 

Thanks for explaining, I don't have any real experience with batch saving files in UXP, I have only worked with interactive saves and had no success with silent batch saves.

 

Care to share minimal working code?