Copy link to clipboard
Copied
Hello, I have a simple save-as-png script which I've been using without issue since the CS5 days. More recently, my studio has upgraded our computers and software, which resulted in upgrading from CC 2015.x to 19.1.6. The old script still functions, however, PS ignores the explicit compression setting within the script and simply uses whatever compression setting I last used when saving as, through the UI. E.g. if my script specifies compression = 0 (no compression) or if the property isn't specified at all (default is no compression), it will still use maximum compression if that happened to be the option chosen on my last trip through the UI's Save As function.
This is costing me a lot of time due to repeated failures, long save times, and troubleshooting, and I'd like to understand how fix my script. My intention is to quickly save PNGs with no compression (large file) and no dialogue boxes or menus. Any help or insight would be appreciated.
Thanks,
Derek
app.displayDialogs = DialogModes.NO;
var saveOptions = new PNGSaveOptions();
saveOptions.compression = 0;
saveOptions.interlaced = false;
fileCheck();
// Functions
function fileCheck(){
if (documents.length == 0) {
alert("No documents open - Aborted");
return;
}
else if (isDocumentNew()){
alert("Unsaved file - Aborted");
return;
}
else {
runSave();
}
}
function isDocumentNew(doc){
// assumes doc is the activeDocument
cTID = function(s) { return app.charIDToTypeID(s); }
var ref = new ActionReference();
ref.putEnumerated( cTID("Dcmn"),
cTID("Ordn"),
cTID("Trgt") ); //activeDoc
var desc = executeActionGet(ref);
var rc = true;
if (desc.hasKey(cTID("FilR"))) { //FileReference
var path = desc.getPath(cTID("FilR"));
if (path) {
rc = (path.absoluteURI.length == 0);
}
}
return rc;
};
function runSave(){
var filePath = app.activeDocument.path;
var fileName = app.activeDocument.name.replace(".psd", "");
fileName = app.activeDocument.name.replace(".png", "");
var fileFolder = Folder(filePath);
if(!fileFolder.exists) fileFolder.create();
savePNG(new File(filePath + "/" + fileName));
}
function savePNG(saveFile) {
app.activeDocument.saveAs(saveFile, saveOptions, true);
//app.activeDocument.close(SaveOptions.DONOTSAVECHANGES);
}
Looks like you found a bug in at least CC2018 and higher. It is not in CS6.
They entered a new enumerable parameter "PNGMethod". This does not exist at least in CS6 and below.
Now the "compression" parameter is valid only with PNGMethod == stringIDToTypeID ("quick");
In the DOM model, the code for doc.saveAs () most likely remains old and does not take this parameter into account. Therefore, if you manually save to PNG with a method other than "quick", then doc.saveAs() will not work correctly.
The
Copy link to clipboard
Copied
Looks like you found a bug in at least CC2018 and higher. It is not in CS6.
They entered a new enumerable parameter "PNGMethod". This does not exist at least in CS6 and below.
Now the "compression" parameter is valid only with PNGMethod == stringIDToTypeID ("quick");
In the DOM model, the code for doc.saveAs () most likely remains old and does not take this parameter into account. Therefore, if you manually save to PNG with a method other than "quick", then doc.saveAs() will not work correctly.
The solution may be to use the function with the AM code.
function save_as_png(doc, file, copy, comp, filter, none)
{
try {
if (copy == undefined) copy = true;
if (comp == undefined) comp = 9;
if (none == undefined) none = true;
if (filter == undefined) filter = "PNGFilterAdaptive";
var doc0 = app.activeDocument;
app.activeDocument = doc;
var d = new ActionDescriptor();
var d1 = new ActionDescriptor();
d1.putEnumerated(stringIDToTypeID("method"), stringIDToTypeID("PNGMethod"), stringIDToTypeID("quick"));
if (none)
d1.putEnumerated(stringIDToTypeID("PNGInterlaceType"), stringIDToTypeID("PNGInterlaceType"), stringIDToTypeID("PNGInterlaceNone"));
else
d1.putEnumerated(stringIDToTypeID("PNGInterlaceType"), stringIDToTypeID("PNGInterlaceType"), stringIDToTypeID("PNGInterlaceAdam7"));
d1.putEnumerated(stringIDToTypeID("PNGFilter"), stringIDToTypeID("PNGFilter"), stringIDToTypeID(filter));
d1.putInteger(stringIDToTypeID("compression"), comp);
d.putObject(stringIDToTypeID("as"), stringIDToTypeID("PNGFormat"), d1);
d.putPath(stringIDToTypeID("in"), file);
d.putBoolean(stringIDToTypeID("copy"), copy);
executeAction(stringIDToTypeID("save"), d, DialogModes.NO);
app.activeDocument = doc0;
return true;
}
catch (e) { alert(e); return false; }
}
P.S. Freaking adobe )
Copy link to clipboard
Copied
You're a life saver. I've never worked with the action manager before, so I would never have figured that out.
Thanks so much!