Slow operation of the script
Hi. Once found a script and modified it to suit my needs. It works fine, but it's slow.
I use it for batch processing.
When I manually save for web, I don't notice such delays. Maybe the script itself is a bit crooked?
The goal is to save a file with an existing color profile, jpg, up to 500kb, in the same folder, with the same name.
The file height and width are already correct before saving, the script should only save with the requirements above.
(Analogous to the "save for web" function, which the action doesn't see)
var docRef = activeDocument;
var outputFolder = docRef.path;
var currentNameWithoutExtension = docRef.name.split(".")[0];
NamesaveRef = new File( outputFolder + "/" + currentNameWithoutExtension + ".jpg" );
var NewfileRef = new File( NamesaveRef )
// quality/size constraints
var MaxSz = 500000; // max. 500Kb
var Qlt = 100; // initial quality 100
var x = 1; // decreasing step
// Perform the first SaveForWeb Operation
ExpWeb(NewfileRef, Qlt);
// Keep trying to save the file with max. Qlt, but under MaxSz
while (NewfileRef.length > MaxSz)
{
Qlt = Qlt - x;
NewfileRef = new File( NewfileRef );
NewfileRef.remove();
ExpWeb(NewfileRef, Qlt); // Perform a new SaveForWeb Operation, with slightly lower Qlt
if (Qlt <= 10) {
alert("The file can't be saved with the desired size AND quality.");
break // break the loop whenever the quality is as low as 50 (this shouldn't need to happen)
}
}
var FileSz = NewfileRef.length/1024;
FileSz = Math.round(FileSz);
// SaveForWeb Export, with the desired constraints and parameters
function ExpWeb(FileNm, Qlt)
{
var options = new ExportOptionsSaveForWeb();
options.includeProfile = true;
options.quality = Qlt; // Start with highest quality (biggest file).
options.format = SaveDocumentType.JPEG; // Save Format for the file
docRef.exportDocument(File(FileNm), ExportType.SAVEFORWEB, options);
}