Skip to main content
Participant
April 18, 2023
Answered

Batch process many PSD files to "Quick export as PNG"

  • April 18, 2023
  • 3 replies
  • 4116 views

Hi all, 

 

I've been racking my brains and the forums/Google et al for a day to try and find a working solution for my problem. I'm trying to have a script run through either a folder of psd files, or, a bunch of open psd files and quick export each of them to png. I'm part way there,  I have a working script that successfully does them one at once; 

 

 

var document = app.activeDocument;

var Name = decodeURI(app.activeDocument.name).replace(/\.[^\.]+$/, '');

var saveIn = File("myLocalPathHere" + Name + ".png");

document.exportDocument(saveIn, ExportType.SAVEFORWEB);

 

 

Where myLocalPathHere is my actual physical path to the output folder. 

If I open this script in Photoshop it will run fine on the currently displayed document but no more.

My skills are basic at best so I'm trying to find if someone's done this before and could point me in the right direction? 

Thanks! D

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

I adapted the following scripts back in 2019 from another script, I'd probably do things differently now, but they should serve the intended purpose. Check the code you may wish to disable the sRGB conversion and or enable the flatten option.

 

Using Save:

 

#target photoshop

// Batch Save As sRGB PNG.jsx

displayDialogs = DialogModes.NO

// raw.githubusercontent.com/jonahvsweb/Photoshop-Automated-Resize-to-Web.jsx/master/Automated%20Resize%20To%20Web.jsx

if (BridgeTalk.appName == "photoshop") {
    app.bringToFront;
    
    var inputFolder = Folder.selectDialog("Select the source folder that contains the files to save as PNG:");

    if (inputFolder != null) {
        // Add/remove file types as necessary
        var fileList = inputFolder.getFiles(/\.(psd|psb|tif?f|jpe?g|png)$/i);
        var outputFolder = inputFolder;

        for (var i = 0; i < fileList.length; i++) {
            if (fileList[i] instanceof File) {
                var document = open(fileList[i]);
                var documentName = fileList[i].name.replace(/\.[^\.]+$/, ''); // Regex remove filename extension

                while (app.documents.length) {
                    var newFile = new File(decodeURI(outputFolder) + "/" + documentName + ".png");

                    // document.flatten (); // Disable flatten image step

                    /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
                    function ConvertTosRGBProfile() {
                        app.activeDocument.convertProfile("sRGB IEC61966-2.1", Intent.RELATIVECOLORIMETRIC, true, false);
                    }
                    /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

                    var pngOptions = new PNGSaveOptions();
                    pngOptions.compression = 0
                    pngOptions.interlaced = false;
                    app.activeDocument.saveAs(newFile, pngOptions, true, Extension.LOWERCASE);
                    app.activeDocument.close();

                }
            }
            if (i == fileList.length - 1) {
                alert("The files have been saved as PNG files!");
            }
        }
    }
}

 

 

 Using Export/Save for Web:

 

#target photoshop

// Batch Export SfW sRGB PNG.jsx

displayDialogs = DialogModes.NO

// raw.githubusercontent.com/jonahvsweb/Photoshop-Automated-Resize-to-Web.jsx/master/Automated%20Resize%20To%20Web.jsx

if (BridgeTalk.appName == "photoshop") {
    app.bringToFront;

    var inputFolder = Folder.selectDialog("Select the source folder that contains the files to export as PNG:");

    if (inputFolder != null) {
        // Add/remove file types as necessary
        var fileList = inputFolder.getFiles(/\.(psd|psb|tif?f|jpe?g|png)$/i);
        var outputFolder = inputFolder ;

        for (var i = 0; i < fileList.length; i++) {
            if (fileList[i] instanceof File) {
                var document = open (fileList [i]);
                var documentName = fileList [i].name.replace(/\.[^\.]+$/, ''); // Regex remove filename extension

                while (app.documents.length) {
                    var newFile = new File(decodeURI(outputFolder) + "/" + documentName + ".png");

                    // document.flatten (); // Disable flatten image step

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
                    function ConvertTosRGBProfile() {
                            app.activeDocument.convertProfile("sRGB IEC61966-2.1", Intent.RELATIVECOLORIMETRIC, true, false);
                        }
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

                    exportOptions = new ExportOptionsSaveForWeb();
                    exportOptions.format = SaveDocumentType.PNG;
                    exportOptions.PNG8 = false; // false = PNG-24
                    exportOptions.transparency = true; // true = transparent
                    exportOptions.interlaced = false; // true = interlacing on
                    exportOptions.includeProfile = true; // false = don't embedd ICC profile
                    document.exportDocument(newFile, ExportType.SAVEFORWEB, exportOptions);
                    document.close(SaveOptions.DONOTSAVECHANGES);
                }
            }
            if (i == fileList.length - 1) {
                alert("The files have been saved as PNG files!");
            }
        }
    }
}

 

 

3 replies

DanFish87Author
Participant
April 19, 2023

Thanks! I'll review these too! 🙂 

Participating Frequently
April 20, 2023

Thank.

Stephen Marsh
Community Expert
Stephen MarshCommunity ExpertCorrect answer
Community Expert
April 18, 2023

I adapted the following scripts back in 2019 from another script, I'd probably do things differently now, but they should serve the intended purpose. Check the code you may wish to disable the sRGB conversion and or enable the flatten option.

 

Using Save:

 

#target photoshop

// Batch Save As sRGB PNG.jsx

displayDialogs = DialogModes.NO

// raw.githubusercontent.com/jonahvsweb/Photoshop-Automated-Resize-to-Web.jsx/master/Automated%20Resize%20To%20Web.jsx

if (BridgeTalk.appName == "photoshop") {
    app.bringToFront;
    
    var inputFolder = Folder.selectDialog("Select the source folder that contains the files to save as PNG:");

    if (inputFolder != null) {
        // Add/remove file types as necessary
        var fileList = inputFolder.getFiles(/\.(psd|psb|tif?f|jpe?g|png)$/i);
        var outputFolder = inputFolder;

        for (var i = 0; i < fileList.length; i++) {
            if (fileList[i] instanceof File) {
                var document = open(fileList[i]);
                var documentName = fileList[i].name.replace(/\.[^\.]+$/, ''); // Regex remove filename extension

                while (app.documents.length) {
                    var newFile = new File(decodeURI(outputFolder) + "/" + documentName + ".png");

                    // document.flatten (); // Disable flatten image step

                    /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
                    function ConvertTosRGBProfile() {
                        app.activeDocument.convertProfile("sRGB IEC61966-2.1", Intent.RELATIVECOLORIMETRIC, true, false);
                    }
                    /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

                    var pngOptions = new PNGSaveOptions();
                    pngOptions.compression = 0
                    pngOptions.interlaced = false;
                    app.activeDocument.saveAs(newFile, pngOptions, true, Extension.LOWERCASE);
                    app.activeDocument.close();

                }
            }
            if (i == fileList.length - 1) {
                alert("The files have been saved as PNG files!");
            }
        }
    }
}

 

 

 Using Export/Save for Web:

 

#target photoshop

// Batch Export SfW sRGB PNG.jsx

displayDialogs = DialogModes.NO

// raw.githubusercontent.com/jonahvsweb/Photoshop-Automated-Resize-to-Web.jsx/master/Automated%20Resize%20To%20Web.jsx

if (BridgeTalk.appName == "photoshop") {
    app.bringToFront;

    var inputFolder = Folder.selectDialog("Select the source folder that contains the files to export as PNG:");

    if (inputFolder != null) {
        // Add/remove file types as necessary
        var fileList = inputFolder.getFiles(/\.(psd|psb|tif?f|jpe?g|png)$/i);
        var outputFolder = inputFolder ;

        for (var i = 0; i < fileList.length; i++) {
            if (fileList[i] instanceof File) {
                var document = open (fileList [i]);
                var documentName = fileList [i].name.replace(/\.[^\.]+$/, ''); // Regex remove filename extension

                while (app.documents.length) {
                    var newFile = new File(decodeURI(outputFolder) + "/" + documentName + ".png");

                    // document.flatten (); // Disable flatten image step

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
                    function ConvertTosRGBProfile() {
                            app.activeDocument.convertProfile("sRGB IEC61966-2.1", Intent.RELATIVECOLORIMETRIC, true, false);
                        }
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

                    exportOptions = new ExportOptionsSaveForWeb();
                    exportOptions.format = SaveDocumentType.PNG;
                    exportOptions.PNG8 = false; // false = PNG-24
                    exportOptions.transparency = true; // true = transparent
                    exportOptions.interlaced = false; // true = interlacing on
                    exportOptions.includeProfile = true; // false = don't embedd ICC profile
                    document.exportDocument(newFile, ExportType.SAVEFORWEB, exportOptions);
                    document.close(SaveOptions.DONOTSAVECHANGES);
                }
            }
            if (i == fileList.length - 1) {
                alert("The files have been saved as PNG files!");
            }
        }
    }
}

 

 

DanFish87Author
Participant
April 19, 2023

Amazing! Worked first time exactly as I needed, thank you for saving me a headache today! Can I ask, are you finding the pngSaveOptions (for example) in a specific resource somewhere? All I could find was documentation from 2020 at best on the CEP Resources github https://github.com/Adobe-CEP/CEP-Resources/tree/master/Documentation/Product%20specific%20Documentation/Photoshop%20Scripting?x-product=phsp/24.3.0 - is this still valid and the go-to for the info I need do you know? 

Thanks ever so much again, you've saved me so much time!

D

Stephen Marsh
Community Expert
Community Expert
April 19, 2023

Yes, the PDF scripting guides are still valid and useful for ExtendScript/JavaScript programming (but not for UXP).

 

An online version:

 

https://theiviaxx.github.io/photoshop-docs/index.html

Stephen Marsh
Community Expert
Community Expert
April 18, 2023

I'll post code for this later when I have more time.

DanFish87Author
Participant
April 18, 2023
Thank you! Much appreciated!