Skip to main content
leol30
Known Participant
November 1, 2022
Answered

Script for saving JPGs is now presenting Save As dialog box

  • November 1, 2022
  • 3 replies
  • 1959 views

Hi

I have been using JJ Mack's script below as an action for saving JPGs for a couple of years on single open JPGs and batching JPGs using Bridge > Photosop > Batch without any problems. However, with the latest PS upgrade (24.0.0) when I try to batch JPGs using the script action I am presented with the Save As dialog box. I have tried the script with and without the app.displayDialogs lines but I'm still seeing the Save As dialog box.

 

If anyone can solve this problem for me I would be very grateful. Thanks in advance for any help I may receive. Cheers Leo

 

============================================

// app.displayDialogs = DialogModes.NO;
try {
var tmp = app.activeDocument.fullName.name;
ftype = decodeURI(tmp.substring(tmp.lastIndexOf("."),)).toLowerCase();
if (ftype==".nef" || ftype==".cr2" || ftype==".crw" || ftype==".dcs" || ftype==".raf" || ftype==".arw" || ftype==".orf") { throw "error1"; }
fname = app.activeDocument.name.replace(/\.[^\.]+$/, '');
SaveAsJPEG(activeDocument.path + "/" + fname, 10); // Quality Level 10
app.activeDocument.close(SaveOptions.DONOTSAVECHANGES); // or (SaveOptions.SAVECHANGES) or (SaveOptions.PROMPTTOSAVECHANGES)
// alert("The document has been saved as a JPEG!");
}
catch(e) {alert("The document has not been saved yet!")}

function SaveAsJPEG(saveFile, jpegQuality){
var doc = activeDocument;
jpgSaveOptions = new JPEGSaveOptions();
jpgSaveOptions.embedColorProfile = true;
jpgSaveOptions.formatOptions = FormatOptions.STANDARDBASELINE;
jpgSaveOptions.matte = MatteType.NONE;
jpgSaveOptions.quality = jpegQuality;
activeDocument.saveAs(File(saveFile+".jpg"), jpgSaveOptions, true,Extension.LOWERCASE);
}
// app.displayDialogs = DialogModes.YES;

 

====================================

This topic has been closed for replies.
Correct answer Stephen Marsh

I'll need to look into this deeper, JJMack's code already has the boolean true for copy set, so something unexpected is happening.


Before I dig any further, first try this action manager code version to see if there is any difference:

 

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

try {
    var tmp = app.activeDocument.fullName.name;
    ftype = decodeURI(tmp.substring(tmp.lastIndexOf("."), )).toLowerCase();
    if (ftype == ".nef" || ftype == ".cr2" || ftype == ".crw" || ftype == ".dcs" || ftype == ".raf" || ftype == ".arw" || ftype == ".orf") {
        throw "error1";
    }
    fname = app.activeDocument.name.replace(/\.[^\.]+$/, '');
    SaveAsJPEG(10, new File(activeDocument.path + "/" + fname), true, true);
    // or (SaveOptions.SAVECHANGES) or (SaveOptions.PROMPTTOSAVECHANGES)
    app.activeDocument.close(SaveOptions.DONOTSAVECHANGES);
    // alert("The document has been saved as a JPEG!");
} catch (e) {
    alert("The document has not been saved yet!")
}

app.displayDialogs = savedDisplayDialogs;


function SaveAsJPEG(extendedQuality, in2, copy, lowerCase) {
	function s2t(s) {
        return app.stringIDToTypeID(s);
    }
	var descriptor = new ActionDescriptor();
	var descriptor2 = new ActionDescriptor();
	descriptor2.putInteger( s2t( "extendedQuality" ), extendedQuality );
	descriptor2.putEnumerated( s2t( "matteColor" ), s2t( "matteColor" ), s2t( "none" ));
	descriptor.putObject( s2t( "as" ), s2t( "JPEG" ), descriptor2 );
	descriptor.putPath( s2t( "in" ), in2 );
	descriptor.putBoolean( s2t( "copy" ), copy );
	descriptor.putBoolean( s2t( "lowerCase" ), lowerCase );
	descriptor.putEnumerated( s2t( "saveStage" ), s2t( "saveStageType" ), s2t( "saveSucceeded" ));
	executeAction( s2t( "save" ), descriptor, DialogModes.NO );
}

 

3 replies

D Fosse
Community Expert
Community Expert
November 1, 2022

It should be fairly easy to include in the script steps that will make the file conform to the jpeg file format specification (flatten, convert to 8 bit, remove alpha channels). Then jpeg will be available in Save As.

 

In the last two PS versions, any file format that does not support all current properties of the file, is moved to Save A Copy. This was done because of policy changes in MacOS, which no longer allowed a copy to be passed off as a full original.

leol30
leol30Author
Known Participant
November 1, 2022

I've have read about this but I still don't understand why I can save a single file in Photoshop using the script action but it doesn't work for a batch of files.

Stephen Marsh
Community Expert
Community Expert
November 2, 2022

Try the following self contained batch code, the input is a top-level folder of images:

 

#target photoshop

app.bringToFront();

if (!documents.length) {

    var savedDisplayDialogs = app.displayDialogs;

    var inputFolder = Folder.selectDialog('Select the input folder', '');
    var inputFiles = inputFolder.getFiles();

    app.displayDialogs = DialogModes.NO;

    for (var a = 0; a < inputFiles.length; a++) {
        try {
            open(inputFiles[a]);

            ///// JJMACK SCRIPT /////

            try {
                var tmp = app.activeDocument.fullName.name;
                ftype = decodeURI(tmp.substring(tmp.lastIndexOf("."), )).toLowerCase();
                if (ftype == ".nef" || ftype == ".cr2" || ftype == ".crw" || ftype == ".dcs" || ftype == ".raf" || ftype == ".arw" || ftype == ".orf") {
                    throw "error1";
                }
                fname = app.activeDocument.name.replace(/\.[^\.]+$/, '');
                SaveAsJPEG(activeDocument.path + "/" + fname, 10); // Quality Level 10
                app.activeDocument.close(SaveOptions.DONOTSAVECHANGES); // or (SaveOptions.SAVECHANGES) or (SaveOptions.PROMPTTOSAVECHANGES)
                // alert("The document has been saved as a JPEG!");
            } catch (e) {
                alert("The document has not been saved yet!")
            }

            function SaveAsJPEG(saveFile, jpegQuality) {
                jpgSaveOptions = new JPEGSaveOptions();
                jpgSaveOptions.embedColorProfile = true;
                jpgSaveOptions.formatOptions = FormatOptions.STANDARDBASELINE;
                jpgSaveOptions.matte = MatteType.NONE;
                jpgSaveOptions.quality = jpegQuality;
                activeDocument.saveAs(File(saveFile + ".jpg"), jpgSaveOptions, true, Extension.LOWERCASE);
            }

            ///// JJMACK SCRIPT /////

            app.activeDocument.close(SaveOptions.SAVECHANGES);

        } catch (e) {}
    }

} else {
    alert('Please close all open files before running this script');
}
Stephen Marsh
Community Expert
Community Expert
November 1, 2022

Try enabling legacy save as in file handling preferences.

leol30
leol30Author
Known Participant
November 1, 2022

I tried the legacy Save As and it made no diffrence 🙂

leol30
leol30Author
Known Participant
November 1, 2022

For Save As dialog box read Save as Copy dialog box