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?
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
...Copy link to clipboard
Copied
I have some example code here:
Copy link to clipboard
Copied
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.2 version!
saveWebP("compressionLossy", 75, true, true, true, false);
function saveWebP(compType, compValue, xmpData, exifData, psData, asCopy) {
/*
v1.2 - 14th March 2023, 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);
}
// 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
}
}
Copy link to clipboard
Copied
A batch WebP script here:
Copy link to clipboard
Copied
Yes! It works! It needs a version 2022... Thank you so much! Petr
Copy link to clipboard
Copied
Thanks! If only this piece of code could be implemented in Image Processor Pro, that would be magic.