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

Saving WEBp image by script

New Here ,
Mar 10, 2023 Mar 10, 2023

Copy link to clipboard

Copied

I use this script images in *.jpg format:

 

var saveFolder = new Folder('/c/Users/petr/Desktop');
var fileName = doc.name.split('.')[0];

var jpgOptions = new JPEGSaveOptions();
jpgOptions.quality = 5;
jpgOptions.embedColorProfile = true;
jpgOptions.formatOptions = FormatOptions.PROGRESSIVE;
jpgOptions.scans = 5;
jpgOptions.matte = MatteType.NONE;

doc.saveAs (new File(saveFolder + '/' + fileName + '.jpg'), jpgOptions);

 

Now I need to use it for WEBp format and I'm not able to find a correct version of webp options to use them in doc.save command... Any idea, please?

TOPICS
Actions and scripting , Windows

Views

3.7K

Translate

Translate

Report

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 1 Correct answer

Community Expert , Mar 11, 2023 Mar 11, 2023

As far as I know, the DOM hasn't been updated since 2020 to include this v2022 feature, so Action Manager code is needed.

 

The code in the previously referenced topic was a quick one-off, perhaps not as "reusable" as it could be.

 

I have cleaned up the code, added it to a function and offered some features to help make it general-purpose. Double-check the comments in the code to change the various options, hopefully, this is clear enough, but if not just let me know.

 

Edit #2: Code updated to

...

Votes

Translate

Translate
Adobe
Community Expert ,
Mar 11, 2023 Mar 11, 2023

Copy link to clipboard

Copied

I have some example code here:

 

 
I am presuming that you are looking for the native save as WebP that was introduced in v2022 (and not an earlier released third-party WebP plug-in).

Votes

Translate

Translate

Report

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
New Here ,
Mar 13, 2023 Mar 13, 2023

Copy link to clipboard

Copied

Thank you so much guys, I will try it!

Votes

Translate

Translate

Report

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 ,
Mar 11, 2023 Mar 11, 2023

Copy link to clipboard

Copied

As far as I know, the DOM hasn't been updated since 2020 to include this v2022 feature, so Action Manager code is needed.

 

The code in the previously referenced topic was a quick one-off, perhaps not as "reusable" as it could be.

 

I have cleaned up the code, added it to a function and offered some features to help make it general-purpose. Double-check the comments in the code to change the various options, hopefully, this is clear enough, but if not just let me know.

 

Edit #2: Code updated to a 1.3 version!

 

 

saveWebP("compressionLossy", 75, true, true, true, false);

function saveWebP(compType, compValue, xmpData, exifData, psData, asCopy) {
    /*
    v1.3 - 10th February 2024, Stephen Marsh
    https://community.adobe.com/t5/photoshop-ecosystem-discussions/saving-webp-image-by-script/td-p/13642577
    */

    // 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...");
    // Pass
    } else {
        // Doc and path save variables
        var WebPDocName = activeDocument.name.replace(/\.[^\.]+$/, ''); // Remove file extension
        var WebPSavePath = "~/Desktop" + "/" + WebPDocName + ".webp" // Change path as needed
        var WebPFile = new File(WebPSavePath); // Create the file object
        
        /*
        // Check for existing file object
        if (WebPFile.exists) {
            // true = 'No' as default active button
            if (!confirm("File exists, overwrite: Yes or No?", true))
                // throw alert("Script cancelled!");
                throw null;
        }
        */

        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);

        // If the doc isn't in RGB mode, convert to sRGB
        if (activeDocument.mode !== DocumentMode.RGB) {
            activeDocument.convertProfile("sRGB IEC61966-2.1", Intent.RELATIVECOLORIMETRIC, true, false);
            activeDocument.changeMode(ChangeMode.RGB);
        }

        // If the doc is in RGB mode, convert to sRGB
        //activeDocument.convertProfile("sRGB IEC61966-2.1", Intent.RELATIVECOLORIMETRIC, true, false);

        // Convert to 8 bpc
        activeDocument.bitsPerChannel = BitsPerChannelType.EIGHT;
        
        // Execute the save
        executeAction(s2t("save"), descriptor, DialogModes.NO); // Change NO to ALL for dialog
    }
}

 

 

 

Votes

Translate

Translate

Report

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 ,
Mar 13, 2023 Mar 13, 2023

Copy link to clipboard

Copied

Votes

Translate

Translate

Report

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
New Here ,
Mar 21, 2023 Mar 21, 2023

Copy link to clipboard

Copied

Yes! It works! It needs a version 2022... Thank you so much! Petr

Votes

Translate

Translate

Report

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
Explorer ,
Oct 06, 2023 Oct 06, 2023

Copy link to clipboard

Copied

LATEST

Thanks! If only this piece of code could be implemented in Image Processor Pro, that would be magic.

Votes

Translate

Translate

Report

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