Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
2

Looking for Action or Script to Resize and Save WebP Files

Explorer ,
Dec 06, 2023 Dec 06, 2023

For a new website build, we were originally asked to save Webp images at 2048 pixels in width.  Now, they want those images to be 1000 pixels wide.  There are THOUSANDS of images in dozens of folders that need to be resized, so I thought I would create a simple action to run in a batch process to resize all the images in the master folder and sub folders, and then save to another folder.  Easy enough; however, when Photoshop goes to save the new WebP image during the batch process in the new folder, it stops and waits for me to choose the compression level of the new WebP image, which I would need to manually do for each image, which won't work. I've seen a script to resize images, and another that converts images to WebP and automatically save them, but nothing that combines both functions.  Can anyone help? 

 

Here is the script to open images in one folder, and then save WebP images in another folder:

 

https://community.adobe.com/t5/photoshop-ecosystem-discussions/batch-process-images-to-webp/m-p/1410...

 

Is it possible to add a command in the middle of that script to resize the images to 1000 pixels in width?

 

Thanks for the help!

TOPICS
Actions and scripting
6.9K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 2 Correct answers

Explorer , Dec 07, 2023 Dec 07, 2023

That must be it Stephen ... and the latest version of Photoshop produced the same dialogue (image attached).  In your first script,  you had compression parameters:

// Compression parameters = "compressionLossless" | "compressionLossy"
                descriptor2.putEnumerated(s2t("compression"), s2t("WebPCompression"), s2t(compType)); // string variable
                var WebPCompIsLossless = false; // set the default flag for compression
                if (WebPCompIsLossless == false) {
     
...
Translate
Community Expert , Dec 07, 2023 Dec 07, 2023

The second script just saves, it assumes that the WebP values are known from the file metadata, which worked for my test files...but who knows how your WebP files were saved.

 

Yes, the entire save as WebP function would need to be used.

 

 l'll look into it later...

 

EDIT:

 

@eznewmedia 

 

Try this 1.2 version:

 

/*
Batch Save As WebP 1000px Wide.jsx
https://community.adobe.com/t5/photoshop-ecosystem-discussions/looking-for-action-or-script-to-resize-and-save-webp-files/td-p/14282886
v1.2 - 8t
...
Translate
Adobe
Community Expert ,
Apr 10, 2024 Apr 10, 2024

@JonDemand 

 

The 1.1 and 1.2 versions in this topic resize the width to 1000px, so that isn't what you are looking for, thus your request.

 

Yes, I can add a scripted "fit image" 1000px to the longest edge to the code so that the script doesn't need to run an action.

 

My question is, do you only wish to process one input folder of images at a time (as per the original 1.0 script), or do you wish to point the script to a root/top-level folder and have it process files from all sub-folders under the root level (as per the 1.1 and 1.2 versions in this topic)?

 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Apr 10, 2024 Apr 10, 2024

it's more i want the tif|psd|jpg etc script to work like this one and point to a root/top-level folder and process everything it finds,  i just couldn't figure out a way to take the code from this script,  and put it in the other one - Export many files at once in webp format 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 10, 2024 Apr 10, 2024
quote

it's more i want the tif|psd|jpg etc script to work like this one and point to a root/top-level folder and process everything it finds,  i just couldn't figure out a way to take the code from this script,  and put it in the other one - Export many files at once in webp format 


By @JonDemand

 

I see, yes, I'll use the following script from this topic:

 

 

Batch Save As WebP 1000px Wide.jsx
v1.1 - 8th December 2023

 

 

And add support for other input file formats than just webp:

 

webp|tif|tiff|jpg|jpeg|psd|psb|png

 

And to save a copy to webp (not just saving in the current format).

 

Then have the output saved to a single folder of your selection.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 10, 2024 Apr 10, 2024

@JonDemand 

 

Try the following script.

 

NOTE: The output folder should not be under the same path as the input folder (otherwise remove webp from the file format name match).

 

As you have requested all files to be saved to a single output folder, you need to consider how to handle duplicate files, as the source folder structure may contain files with the same name as they are in different folders, however, when saved to a single common folder there may be duplicates. This script simply overwrites existing webp files without any alert or logging. You will need to check for missing files yourself, as the input file count may not match the output file count.

 

Batch Save As WebP and Fit Longest Edge to 1000px Wide.jsx

 

/*
Batch Save As WebP and Fit Longest Edge to 1000px Wide.jsx
https://community.adobe.com/t5/photoshop-ecosystem-discussions/looking-for-action-or-script-to-resize-and-save-webp-files/td-p/14282886
v1.0 - 11th April 2024, Stephen Marsh
NOTE:
* The output folder should NOT be under the same path as the input folder
* Existing webp files with duplicate file names in the output folder will be overwritten without warning!
*/

#target photoshop

    (function () {

        if (app.documents.length === 0) {

            // Ensure that version 2022 or later is being used
            var versionNumber = app.version.split(".");
            var versionCheck = parseInt(versionNumber);

            // Fail
            if (versionCheck < 23) {
                alert("You must use Photoshop 2022 or later to save using native WebP format...");
                return;

            // Pass
            } else {

                // Optionally run a specified action
                //var actionName = "Molten Lead"; // Action to run, change as needed
                //var actionSet = "Default Actions"; // Action set to run, change as needed

                if (confirm('This script will proportionally fit all supported files to 1000px on the longest edge. All sub-folders under the selected input folder will be processed. Continue?', false)) {

                    // Root/top-level input folder
                    var imageFolder = Folder.selectDialog('Select the root/top-level folder to process:');
                    // The output folder should NOT be under the same path as the input folder!
                    var outputFolder = Folder.selectDialog("Please select the output folder to save the WebP files to:");
                    // To do: add supporting code to check for this

                    var origDialogs = app.displayDialogs;
                    app.displayDialogs = DialogModes.NO;

                    // Script Execution Timer - Function    
                    var timeDiff = {
                        setStartTime: function () {
                            d = new Date();
                            time = d.getTime();
                        },
                        getDiff: function () {
                            d = new Date();
                            t = d.getTime() - time;
                            time = d.getTime();
                            return t;
                        }
                    };

                    // Script Execution Timer - Start    
                    timeDiff.setStartTime();

                    if (imageFolder != null) processFolder(imageFolder);

                    function processFolder(folder) {
                        // Special thanks to Paul MR for the recursive folder processing code:
                        // photoshopgurus.com/forum/threads/batch-and-subfolders.22134/
                        var fileList = folder.getFiles()
                        for (var i = 0; i < fileList.length; i++) {
                            var file = fileList[i];
                            if (file instanceof File && file.name.match(/\.(webp|tif|tiff|jpg|jpeg|psd|psb|png)$/i)) {

                                open(file);

                                /* Start Main Code Block */

                                // Proportionally fit image to 1000px on the longest edge
                                fitImage(1000, 1000);

                                /////
                                // If the doc isn't in RGB mode
                                if (activeDocument.mode !== DocumentMode.RGB) {

                                    // Convert to sRGB & 8 bpc
                                    activeDocument.convertProfile("sRGB IEC61966-2.1", Intent.RELATIVECOLORIMETRIC, true, false);
                                    activeDocument.changeMode(ChangeMode.RGB);
                                    activeDocument.bitsPerChannel = BitsPerChannelType.EIGHT;

                                    // Run the optional action
                                    //app.doAction(actionName, actionSet);

                                    // Save as a copy and close
                                    saveWebP("compressionLossy", 75, true, true, true, true);
                                    activeDocument.close(SaveOptions.DONOTSAVECHANGES);

                                    // If the doc is in RGB mode
                                } else {
                                    // Convert to sRGB & 8 bpc
                                    //activeDocument.convertProfile("sRGB IEC61966-2.1", Intent.RELATIVECOLORIMETRIC, true, false);
                                    activeDocument.bitsPerChannel = BitsPerChannelType.EIGHT;

                                    // Run the optional action
                                    //app.doAction(actionName, actionSet);

                                    // Save as a copy and close
                                    saveWebP("compressionLossy", 75, true, true, true, true);
                                    activeDocument.close(SaveOptions.DONOTSAVECHANGES);
                                }

                                /* End Main Code Block */

                            } else
                            if (file instanceof Folder) {
                                processFolder(file);
                            }
                        }
                    }

                    // End of script
                    app.displayDialogs = origDialogs;
                    app.beep();
                    alert('Batch resize and save to WebP completed!' + '\r' + '(' + timeDiff.getDiff() / 1000 + ' seconds)');
                    // Open the output folder in the Finder or Explorer
                    outputFolder.execute();
                }
            }

        } else {
            alert('Please close all open documents before running this script!');
        }


        ///// FUCTIONS /////

        function fitImage(fWidth, fHeight) {
            /* NEARESTNEIGHBOR | BILINEAR | BICUBIC | BICUBICSMOOTHER | BICUBICSHARPER | BICUBICAUTOMATIC */
            if (activeDocument.height.value > activeDocument.width.value) {
                activeDocument.resizeImage(null, UnitValue(fHeight, "px"), null, ResampleMethod.BICUBIC);
            } else {
                activeDocument.resizeImage(UnitValue(fWidth, "px"), null, null, ResampleMethod.BICUBIC);
            }
        }

        function saveWebP(compType, compValue, xmpData, exifData, psData, asCopy) {
            /*
            v1.1 - 12th March 2023, Stephen Marsh
            https://community.adobe.com/t5/photoshop-ecosystem-discussions/saving-webp-image-by-script/td-p/13642577
            */
            // Doc and path save variables
            var WebPDocName = activeDocument.name.replace(/\.[^\.]+$/, ''); // Remove file extension
            var WebPSavePath = outputFolder + "/" + WebPDocName + ".webp" // Change path as needed
            var WebPFile = new File(WebPSavePath); // Create the file object

                function s2t(s) {
                    return app.stringIDToTypeID(s);
                }
                var descriptor = new ActionDescriptor();
                var descriptor2 = new ActionDescriptor();

                // Compression parameters = "compressionLossless" | "compressionLossy"
                descriptor2.putEnumerated(s2t("compression"), s2t("WebPCompression"), s2t(compType)); // string variable
                var WebPCompIsLossless = false; // set the default flag for compression
                if (WebPCompIsLossless == false) {
                    // 0 (lowest lossy quality) - 100 (highest lossy quality)
                    descriptor2.putInteger(s2t("quality"), compValue); //  number variable
                }

                // Metadata options
                descriptor2.putBoolean(s2t("includeXMPData"), xmpData); // Boolean param moved to function call
                descriptor2.putBoolean(s2t("includeEXIFData"), exifData); // Boolean param moved to function call
                descriptor2.putBoolean(s2t("includePsExtras"), psData); // Boolean param moved to function call

                // WebP format and save path
                descriptor.putObject(s2t("as"), s2t("WebPFormat"), descriptor2);
                descriptor.putPath(s2t("in"), WebPFile); // Save path variable

                // Save As = false | Save As a Copy = true
                descriptor.putBoolean(s2t("copy"), asCopy); // Boolean param moved to function call

                // The extension
                descriptor.putBoolean(s2t("lowerCase"), true);

                // Execute the save
                executeAction(s2t("save"), descriptor, DialogModes.NO); // Change NO to ALL for dialog
        }

    }());

 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 10, 2024 Apr 10, 2024

@JonDemand 

 

This 1.1 version script will skip over duplicate destination/output files. The input file count and the output file count are reported in the alert at the end of the script, so hopefully you will have some idea that the two quantities don't match. With a little more work, duplicate file paths and names could be silently written to a log file.

 

Note: The selected output folder should be empty for the reported file count to be correct.

 

Batch Save As WebP and Fit Longest Edge to 1000px Wide v1-1.jsx

 

/*
Batch Save As WebP and Fit Longest Edge to 1000px Wide v1-1.jsx
https://community.adobe.com/t5/photoshop-ecosystem-discussions/looking-for-action-or-script-to-resize-and-save-webp-files/td-p/14282886
v1.1 - 11th April 2024, Stephen Marsh
NOTE:
The output folder should NOT be under the same path as the input folder!
*/

#target photoshop

    (function () {

        if (app.documents.length === 0) {

            // Ensure that version 2022 or later is being used
            var versionNumber = app.version.split(".");
            var versionCheck = parseInt(versionNumber);

            // Fail
            if (versionCheck < 23) {
                alert("You must use Photoshop 2022 or later to save using native WebP format...");
                return;

            // Pass
            } else {

                // Optionally run a specified action
                //var actionName = "Molten Lead"; // Action to run, change as needed
                //var actionSet = "Default Actions"; // Action set to run, change as needed

                if (confirm('This script will proportionally fit all supported files to 1000px on the longest edge. All sub-folders under the selected input folder will be processed. Continue?', false)) {

                    // Root/top-level input folder
                    var imageFolder = Folder.selectDialog('Select the root/top-level folder to process:');
                    // The output folder should NOT be under the same path as the input folder!
                    var outputFolder = Folder.selectDialog("Please select the output folder to save the WebP files to:");
                    // To do: add supporting code to check for this

                    var origDialogs = app.displayDialogs;
                    app.displayDialogs = DialogModes.NO;

                    // Script Execution Timer - Function    
                    var timeDiff = {
                        setStartTime: function () {
                            d = new Date();
                            time = d.getTime();
                        },
                        getDiff: function () {
                            d = new Date();
                            t = d.getTime() - time;
                            time = d.getTime();
                            return t;
                        }
                    };

                    // Script Execution Timer - Start    
                    timeDiff.setStartTime();

                    // Set the input file processing counter
                    var inputFileCounter = 0;
                    // Set the output file processing counter
                    var fileCounter = 0;

                    if (imageFolder != null) processFolder(imageFolder);

                    function processFolder(folder) {
                        // Special thanks to Paul MR for the recursive folder processing code:
                        // photoshopgurus.com/forum/threads/batch-and-subfolders.22134/
                        var fileList = folder.getFiles()
                        for (var i = 0; i < fileList.length; i++) {
                            var file = fileList[i];
                            if (file instanceof File && file.name.match(/\.(webp|tif|tiff|jpg|jpeg|psd|psb|png)$/i)) {

                                open(file);

                                /* Start Main Code Block */

                                // Proportionally fit image to 1000px on the longest edge
                                fitImage(1000, 1000);

                                /////
                                // If the doc isn't in RGB mode
                                if (activeDocument.mode !== DocumentMode.RGB) {

                                    // Convert to sRGB & 8 bpc
                                    activeDocument.convertProfile("sRGB IEC61966-2.1", Intent.RELATIVECOLORIMETRIC, true, false);
                                    activeDocument.changeMode(ChangeMode.RGB);
                                    activeDocument.bitsPerChannel = BitsPerChannelType.EIGHT;

                                    // Run the optional action
                                    //app.doAction(actionName, actionSet);

                                    // Save as a copy and close
                                    saveWebP("compressionLossy", 75, true, true, true, true);
                                    activeDocument.close(SaveOptions.DONOTSAVECHANGES);

                                    // If the doc is in RGB mode
                                } else {
                                    // Convert to sRGB & 8 bpc
                                    //activeDocument.convertProfile("sRGB IEC61966-2.1", Intent.RELATIVECOLORIMETRIC, true, false);
                                    activeDocument.bitsPerChannel = BitsPerChannelType.EIGHT;

                                    // Run the optional action
                                    //app.doAction(actionName, actionSet);

                                    // Save as a copy and close
                                    saveWebP("compressionLossy", 75, true, true, true, true);
                                    activeDocument.close(SaveOptions.DONOTSAVECHANGES);
                                }

                                // Increment the input file saving counter
                                inputFileCounter++

                                /* End Main Code Block */

                            } else
                            if (file instanceof Folder) {
                                processFolder(file);
                            }
                        }
                    }

                    // End of script
                    app.displayDialogs = origDialogs;
                    app.beep();
                    alert('Batch resize and save to WebP completed!' + '\r' + inputFileCounter + ' source files' + '\r' + fileCounter + ' WebP files saved' + '\r' + '(' + timeDiff.getDiff() / 1000 + ' seconds)');
                    // Open the output folder in the Finder or Explorer
                    outputFolder.execute();
                }
            }

        } else {
            alert('Please close all open documents before running this script!');
        }

        ///// FUCTIONS /////

        function fitImage(fWidth, fHeight) {
            /* NEARESTNEIGHBOR | BILINEAR | BICUBIC | BICUBICSMOOTHER | BICUBICSHARPER | BICUBICAUTOMATIC */
            if (activeDocument.height.value > activeDocument.width.value) {
                activeDocument.resizeImage(null, UnitValue(fHeight, "px"), null, ResampleMethod.BICUBIC);
            } else {
                activeDocument.resizeImage(UnitValue(fWidth, "px"), null, null, ResampleMethod.BICUBIC);
            }
        }

        function saveWebP(compType, compValue, xmpData, exifData, psData, asCopy) {
            /*
            v1.1 - 12th March 2023, Stephen Marsh
            https://community.adobe.com/t5/photoshop-ecosystem-discussions/saving-webp-image-by-script/td-p/13642577
            */
            // Doc and path save variables
            var WebPDocName = activeDocument.name.replace(/\.[^\.]+$/, ''); // Remove file extension
            var WebPSavePath = outputFolder + "/" + WebPDocName + ".webp" // Change path as needed
            var WebPFile = new File(WebPSavePath); // Create the file object

            // Check for existing file object
            if (WebPFile.exists) {
                //alert("'" + app.activeDocument.name + "'" + " exists and has been skipped!")
            } else {
                function s2t(s) {
                    return app.stringIDToTypeID(s);
                }
                var descriptor = new ActionDescriptor();
                var descriptor2 = new ActionDescriptor();

                // Compression parameters = "compressionLossless" | "compressionLossy"
                descriptor2.putEnumerated(s2t("compression"), s2t("WebPCompression"), s2t(compType)); // string variable
                var WebPCompIsLossless = false; // set the default flag for compression
                if (WebPCompIsLossless == false) {
                    // 0 (lowest lossy quality) - 100 (highest lossy quality)
                    descriptor2.putInteger(s2t("quality"), compValue); //  number variable
                }

                // Metadata options
                descriptor2.putBoolean(s2t("includeXMPData"), xmpData); // Boolean param moved to function call
                descriptor2.putBoolean(s2t("includeEXIFData"), exifData); // Boolean param moved to function call
                descriptor2.putBoolean(s2t("includePsExtras"), psData); // Boolean param moved to function call

                // WebP format and save path
                descriptor.putObject(s2t("as"), s2t("WebPFormat"), descriptor2);
                descriptor.putPath(s2t("in"), WebPFile); // Save path variable

                // Save As = false | Save As a Copy = true
                descriptor.putBoolean(s2t("copy"), asCopy); // Boolean param moved to function call

                // The extension
                descriptor.putBoolean(s2t("lowerCase"), true);

                // Execute the save
                executeAction(s2t("save"), descriptor, DialogModes.NO); // Change NO to ALL for dialog

                // Increment the file saving counter
                fileCounter++;
            }
        }

    }());

 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 10, 2024 Apr 10, 2024

@JonDemand 

 

This 1.2 version adds a log text file to the output folder, listing any skipped files that would have overwritten an existing file.

 

Batch Save As WebP and Fit Longest Edge to 1000px Wide v1-2.jsx
 
/*
Batch Save As WebP and Fit Longest Edge to 1000px Wide v1-2.jsx
https://community.adobe.com/t5/photoshop-ecosystem-discussions/looking-for-action-or-script-to-resize-and-save-webp-files/td-p/14282886
v1.2 - 11th April 2024, Stephen Marsh
NOTE:
The output folder should NOT be under the same path as the input folder!
*/

#target photoshop

    (function () {

        if (app.documents.length === 0) {

            // Ensure that version 2022 or later is being used
            var versionNumber = app.version.split(".");
            var versionCheck = parseInt(versionNumber);

            // Fail
            if (versionCheck < 23) {
                alert("You must use Photoshop 2022 or later to save using native WebP format...");
                return;

                // Pass
            } else {

                // Optionally run a specified action
                //var actionName = "Molten Lead"; // Action to run, change as needed
                //var actionSet = "Default Actions"; // Action set to run, change as needed

                if (confirm('This script will proportionally fit all supported files to 1000px on the longest edge. All sub-folders under the selected input folder will be processed. Continue?', false)) {

                    // Root/top-level input folder
                    var imageFolder = Folder.selectDialog('Select the root/top-level folder to process:');
                    // The output folder should NOT be under the same path as the input folder!
                    var outputFolder = Folder.selectDialog("Please select the output folder to save the WebP files to:");
                    // To do: add supporting code to check for this

                    var origDialogs = app.displayDialogs;
                    app.displayDialogs = DialogModes.NO;

                    // Script Execution Timer - Function    
                    var timeDiff = {
                        setStartTime: function () {
                            d = new Date();
                            time = d.getTime();
                        },
                        getDiff: function () {
                            d = new Date();
                            t = d.getTime() - time;
                            time = d.getTime();
                            return t;
                        }
                    };

                    // Script Execution Timer - Start    
                    timeDiff.setStartTime();

                    // Set the input file processing counter
                    var inputFileCounter = 0;
                    // Set the output file processing counter
                    var fileCounter = 0;

                    if (imageFolder != null) processFolder(imageFolder);

                    function processFolder(folder) {
                        // Special thanks to Paul MR for the recursive folder processing code:
                        // photoshopgurus.com/forum/threads/batch-and-subfolders.22134/
                        var fileList = folder.getFiles()
                        for (var i = 0; i < fileList.length; i++) {
                            var file = fileList[i];
                            if (file instanceof File && file.name.match(/\.(webp|tif|tiff|jpg|jpeg|psd|psb|png)$/i)) {

                                open(file);

                                /* Start Main Code Block */

                                // Proportionally fit image to 1000px on the longest edge
                                fitImage(1000, 1000);

                                /////
                                // If the doc isn't in RGB mode
                                if (activeDocument.mode !== DocumentMode.RGB) {

                                    // Convert to sRGB & 8 bpc
                                    activeDocument.convertProfile("sRGB IEC61966-2.1", Intent.RELATIVECOLORIMETRIC, true, false);
                                    activeDocument.changeMode(ChangeMode.RGB);
                                    activeDocument.bitsPerChannel = BitsPerChannelType.EIGHT;

                                    // Run the optional action
                                    //app.doAction(actionName, actionSet);

                                    // Save as a copy and close
                                    saveWebP("compressionLossy", 75, true, true, true, true);
                                    activeDocument.close(SaveOptions.DONOTSAVECHANGES);

                                    // If the doc is in RGB mode
                                } else {
                                    // Convert to sRGB & 8 bpc
                                    //activeDocument.convertProfile("sRGB IEC61966-2.1", Intent.RELATIVECOLORIMETRIC, true, false);
                                    activeDocument.bitsPerChannel = BitsPerChannelType.EIGHT;

                                    // Run the optional action
                                    //app.doAction(actionName, actionSet);

                                    // Save as a copy and close
                                    saveWebP("compressionLossy", 75, true, true, true, true);
                                    activeDocument.close(SaveOptions.DONOTSAVECHANGES);
                                }

                                // Increment the input file saving counter
                                inputFileCounter++

                                /* End Main Code Block */

                            } else
                            if (file instanceof Folder) {
                                processFolder(file);
                            }
                        }
                    }

                    // End of script
                    app.displayDialogs = origDialogs;
                    app.beep();
                    alert('Batch resize and save to WebP completed!' + '\r' + inputFileCounter + ' source files' + '\r' + fileCounter + ' WebP files saved' + '\r' + '(' + timeDiff.getDiff() / 1000 + ' seconds)');
                    // Open the output folder in the Finder or Explorer
                    outputFolder.execute();
                }
            }

        } else {
            alert('Please close all open documents before running this script!');
        }

        ///// FUCTIONS /////

        function fitImage(fWidth, fHeight) {
            /* NEARESTNEIGHBOR | BILINEAR | BICUBIC | BICUBICSMOOTHER | BICUBICSHARPER | BICUBICAUTOMATIC */
            if (activeDocument.height.value > activeDocument.width.value) {
                activeDocument.resizeImage(null, UnitValue(fHeight, "px"), null, ResampleMethod.BICUBIC);
            } else {
                activeDocument.resizeImage(UnitValue(fWidth, "px"), null, null, ResampleMethod.BICUBIC);
            }
        }

        function saveWebP(compType, compValue, xmpData, exifData, psData, asCopy) {
            /*
            v1.1 - 12th March 2023, Stephen Marsh
            https://community.adobe.com/t5/photoshop-ecosystem-discussions/saving-webp-image-by-script/td-p/13642577
            */
            // Doc and path save variables
            var WebPDocName = activeDocument.name.replace(/\.[^\.]+$/, ''); // Remove file extension
            var WebPSavePath = outputFolder + "/" + WebPDocName + ".webp" // Change path as needed
            var WebPFile = new File(WebPSavePath); // Create the file object

            // Check for existing file object
            if (WebPFile.exists) {
                try {
                    var os = $.os.toLowerCase().indexOf("mac") >= 0 ? "mac" : "windows";
                    if (os === "mac") {
                        logFileLF = "Unix"; // Legacy = "Macintosh"
                    } else {
                        logFileLF = "Windows";
                    }
                    var logFile = new File(outputFolder + "/" + "Batch-WebP-to-1000px.log");
                    if (logFile.exists)
                        logFile.remove();
                    logFile.open("a");
                    logFile.encoding = "UTF-8";
                    logFile.lineFeed = logFileLF;
                    logFile.write("The following input file creates a duplicate output file and was skipped:" + "\r" + app.activeDocument.path.fsName + "/" + app.activeDocument.name + "\r");
                    logFile.close();
                } catch (e) {
                    alert(e + ': Line ' + e.line);
                }
            } else {
                function s2t(s) {
                    return app.stringIDToTypeID(s);
                }
                var descriptor = new ActionDescriptor();
                var descriptor2 = new ActionDescriptor();

                // Compression parameters = "compressionLossless" | "compressionLossy"
                descriptor2.putEnumerated(s2t("compression"), s2t("WebPCompression"), s2t(compType)); // string variable
                var WebPCompIsLossless = false; // set the default flag for compression
                if (WebPCompIsLossless == false) {
                    // 0 (lowest lossy quality) - 100 (highest lossy quality)
                    descriptor2.putInteger(s2t("quality"), compValue); //  number variable
                }

                // Metadata options
                descriptor2.putBoolean(s2t("includeXMPData"), xmpData); // Boolean param moved to function call
                descriptor2.putBoolean(s2t("includeEXIFData"), exifData); // Boolean param moved to function call
                descriptor2.putBoolean(s2t("includePsExtras"), psData); // Boolean param moved to function call

                // WebP format and save path
                descriptor.putObject(s2t("as"), s2t("WebPFormat"), descriptor2);
                descriptor.putPath(s2t("in"), WebPFile); // Save path variable

                // Save As = false | Save As a Copy = true
                descriptor.putBoolean(s2t("copy"), asCopy); // Boolean param moved to function call

                // The extension
                descriptor.putBoolean(s2t("lowerCase"), true);

                // Execute the save
                executeAction(s2t("save"), descriptor, DialogModes.NO); // Change NO to ALL for dialog

                // Increment the file saving counter
                fileCounter++;
            }
        }
    }());

 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines