Skip to main content
Participant
June 14, 2024
Answered

I want to export visible layers from a psd file using jsx as JPEG

  • June 14, 2024
  • 3 replies
  • 740 views

I want to make an automated script in jsx that reads my psd file in photoshop and saves the visible layers as jpeg in a output folder that i can choose when starting the process. 

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

I've been waiting for your reply, however, I'll post what I created a couple of days ago when you first posted:

 

/*
Save All Visible Top-level Layers as JPEG.jsx
v1.0 - 14th June 2024, Stephen Marsh
https://community.adobe.com/t5/photoshop-ecosystem-discussions/i-want-to-export-visible-layers-from-a-psd-file-using-jsx-as-jpeg/td-p/14681365
*/

#target photoshop

if (app.documents.length) {

    (function () {

        // Select the output folder
        var outputFolder = Folder.selectDialog("Select the folder to save the visible layers as JPEG files to:");
        if (outputFolder === null) {
            alert('Script cancelled!');
            return;
        }

        // Set the doc and layer variables
        var doc = app.activeDocument;
        var docName = doc.name.replace(/\.[^\.]+$/, ''); // Remove the extension
        var theLayers = doc.layers; // or artLayers to exclude layerSets
        var theLayerStates = []; // Set an empty array to hold the original visibility states of the layers

        // Set the counter
        var counter = 0;

        // Hide the Photoshop panels
        app.togglePalettes();

        // Store the original visibility states and hide all layers
        for (var i = 0; i < theLayers.length; i++) {
            theLayerStates[i] = theLayers[i].visible;
            theLayers[i].visible = false;
        }

        // Loop through all the root/top-level layers
        for (var j = 0; j < theLayers.length; j++) {
            var theLayer = theLayers[j];

            // Check if the layer was originally visible
            if (theLayerStates[j]) {
                // Make only the current layer visible
                //alert(theLayer.name);
                theLayer.visible = true;

                // Duplicate the document
                var tempDoc = doc.duplicate();

                // Ensure that the file conforms to JPEG spec
                tempDoc.flatten();
                tempDoc.bitsPerChannel = BitsPerChannelType.EIGHT;
                tempDoc.channels.removeAll();

                // Remove unnecessary metadata - //comment out if needed
                removeDocumentAncestorsMetadata();
                removeCRSmetadata();
                removeXMPmetadata();

                // Save as JPEG 10 using the doc name and sanitised layer name
                var layerName = theLayer.name.replace(/\.[^\.]+$/, '').replace(/[:\/\\*\?\"\<\>\|\\\r\\\n.]/g, "");
                var jpegFile = new File(outputFolder + "/" + docName + "_" + layerName + ".jpg");
                var jpegOptions = new JPEGSaveOptions();
                jpegOptions.formatOptions = FormatOptions.STANDARDBASELINE;
                jpegOptions.embedColorProfile = true;
                jpegOptions.matte = MatteType.NONE;
                jpegOptions.quality = 10;
                tempDoc.saveAs(jpegFile, jpegOptions, true, Extension.LOWERCASE);
                //alert("Layer name: '" + layerName + "' saved!");

                // Close the temporary document without saving
                tempDoc.close(SaveOptions.DONOTSAVECHANGES);

                // Hide the layer again
                theLayer.visible = false;

                // Increment the counter
                counter++;
            }
        }
        
        // Restore the original visibility states
        for (var k = 0; k < theLayers.length; k++) {
            theLayers[k].visible = theLayerStates[k];
        }

        // End of script notification
        app.beep();
        alert('Script completed!' + '\r' + counter + ' visible top-level layers saved as JPEG files to:' + '\r' + outputFolder);

        // Restore the Photoshop panels
        app.togglePalettes();


        // Functions

        function removeXMPmetadata() {
            //https://community.adobe.com/t5/photoshop/script-to-remove-all-meta-data-from-the-photo/td-p/10400906
            if (!documents.length) return;
            if (ExternalObject.AdobeXMPScript === undefined) ExternalObject.AdobeXMPScript = new ExternalObject("lib:AdobeXMPScript");
            var xmp = new XMPMeta(activeDocument.xmpMetadata.rawData);
            XMPUtils.removeProperties(xmp, "", "", XMPConst.REMOVE_ALL_PROPERTIES);
            app.activeDocument.xmpMetadata.rawData = xmp.serialize();
        }

        function removeCRSmetadata() {
            //https://community.adobe.com/t5/photoshop/remove-crs-metadata/td-p/10306935
            if (!documents.length) return;
            if (ExternalObject.AdobeXMPScript === undefined) ExternalObject.AdobeXMPScript = new ExternalObject("lib:AdobeXMPScript");
            var xmp = new XMPMeta(app.activeDocument.xmpMetadata.rawData);
            XMPUtils.removeProperties(xmp, XMPConst.NS_CAMERA_RAW, "", XMPConst.REMOVE_ALL_PROPERTIES);
            app.activeDocument.xmpMetadata.rawData = xmp.serialize();
        }

        function removeDocumentAncestorsMetadata() {
            //https://community.adobe.com/t5/photoshop-ecosystem-discussions/photoshop-saving-issue-files-too-large/td-p/7279443/page/3
            if (!documents.length) return;
            whatApp = String(app.name); //String version of the app name
            if (whatApp.search("Photoshop") > 0) { //Check for photoshop specifically, or this will cause errors
                if (ExternalObject.AdobeXMPScript === undefined) ExternalObject.AdobeXMPScript = new ExternalObject("lib:AdobeXMPScript");
                var xmp = new XMPMeta(activeDocument.xmpMetadata.rawData);
                // Begone foul Document Ancestors!
                xmp.deleteProperty(XMPConst.NS_PHOTOSHOP, "DocumentAncestors");
                app.activeDocument.xmpMetadata.rawData = xmp.serialize();
            }
        }

    })();

} else {
    alert('A document must be open to run this script!');
}

 

I presumed that you wanted each separate visible layer as a separate file, however, you might have meant something else.

3 replies

c.pfaffenbichler
Community Expert
Community Expert
June 16, 2024

@Prithvi Singh38041280pksf , please provide a sample file or at the very least meaningful screenshots (including the pertinent Panels) of the layered image and an overview of the resulting files you want the Script to create. 

Participant
June 18, 2024

there wasn't much to that,i just wanted to be able to export all the layers of a psd into png, we managed to do it in python but had issues with jsx since javascript is not our forte.

 

Stephen Marsh
Community Expert
Stephen MarshCommunity ExpertCorrect answer
Community Expert
June 16, 2024

I've been waiting for your reply, however, I'll post what I created a couple of days ago when you first posted:

 

/*
Save All Visible Top-level Layers as JPEG.jsx
v1.0 - 14th June 2024, Stephen Marsh
https://community.adobe.com/t5/photoshop-ecosystem-discussions/i-want-to-export-visible-layers-from-a-psd-file-using-jsx-as-jpeg/td-p/14681365
*/

#target photoshop

if (app.documents.length) {

    (function () {

        // Select the output folder
        var outputFolder = Folder.selectDialog("Select the folder to save the visible layers as JPEG files to:");
        if (outputFolder === null) {
            alert('Script cancelled!');
            return;
        }

        // Set the doc and layer variables
        var doc = app.activeDocument;
        var docName = doc.name.replace(/\.[^\.]+$/, ''); // Remove the extension
        var theLayers = doc.layers; // or artLayers to exclude layerSets
        var theLayerStates = []; // Set an empty array to hold the original visibility states of the layers

        // Set the counter
        var counter = 0;

        // Hide the Photoshop panels
        app.togglePalettes();

        // Store the original visibility states and hide all layers
        for (var i = 0; i < theLayers.length; i++) {
            theLayerStates[i] = theLayers[i].visible;
            theLayers[i].visible = false;
        }

        // Loop through all the root/top-level layers
        for (var j = 0; j < theLayers.length; j++) {
            var theLayer = theLayers[j];

            // Check if the layer was originally visible
            if (theLayerStates[j]) {
                // Make only the current layer visible
                //alert(theLayer.name);
                theLayer.visible = true;

                // Duplicate the document
                var tempDoc = doc.duplicate();

                // Ensure that the file conforms to JPEG spec
                tempDoc.flatten();
                tempDoc.bitsPerChannel = BitsPerChannelType.EIGHT;
                tempDoc.channels.removeAll();

                // Remove unnecessary metadata - //comment out if needed
                removeDocumentAncestorsMetadata();
                removeCRSmetadata();
                removeXMPmetadata();

                // Save as JPEG 10 using the doc name and sanitised layer name
                var layerName = theLayer.name.replace(/\.[^\.]+$/, '').replace(/[:\/\\*\?\"\<\>\|\\\r\\\n.]/g, "");
                var jpegFile = new File(outputFolder + "/" + docName + "_" + layerName + ".jpg");
                var jpegOptions = new JPEGSaveOptions();
                jpegOptions.formatOptions = FormatOptions.STANDARDBASELINE;
                jpegOptions.embedColorProfile = true;
                jpegOptions.matte = MatteType.NONE;
                jpegOptions.quality = 10;
                tempDoc.saveAs(jpegFile, jpegOptions, true, Extension.LOWERCASE);
                //alert("Layer name: '" + layerName + "' saved!");

                // Close the temporary document without saving
                tempDoc.close(SaveOptions.DONOTSAVECHANGES);

                // Hide the layer again
                theLayer.visible = false;

                // Increment the counter
                counter++;
            }
        }
        
        // Restore the original visibility states
        for (var k = 0; k < theLayers.length; k++) {
            theLayers[k].visible = theLayerStates[k];
        }

        // End of script notification
        app.beep();
        alert('Script completed!' + '\r' + counter + ' visible top-level layers saved as JPEG files to:' + '\r' + outputFolder);

        // Restore the Photoshop panels
        app.togglePalettes();


        // Functions

        function removeXMPmetadata() {
            //https://community.adobe.com/t5/photoshop/script-to-remove-all-meta-data-from-the-photo/td-p/10400906
            if (!documents.length) return;
            if (ExternalObject.AdobeXMPScript === undefined) ExternalObject.AdobeXMPScript = new ExternalObject("lib:AdobeXMPScript");
            var xmp = new XMPMeta(activeDocument.xmpMetadata.rawData);
            XMPUtils.removeProperties(xmp, "", "", XMPConst.REMOVE_ALL_PROPERTIES);
            app.activeDocument.xmpMetadata.rawData = xmp.serialize();
        }

        function removeCRSmetadata() {
            //https://community.adobe.com/t5/photoshop/remove-crs-metadata/td-p/10306935
            if (!documents.length) return;
            if (ExternalObject.AdobeXMPScript === undefined) ExternalObject.AdobeXMPScript = new ExternalObject("lib:AdobeXMPScript");
            var xmp = new XMPMeta(app.activeDocument.xmpMetadata.rawData);
            XMPUtils.removeProperties(xmp, XMPConst.NS_CAMERA_RAW, "", XMPConst.REMOVE_ALL_PROPERTIES);
            app.activeDocument.xmpMetadata.rawData = xmp.serialize();
        }

        function removeDocumentAncestorsMetadata() {
            //https://community.adobe.com/t5/photoshop-ecosystem-discussions/photoshop-saving-issue-files-too-large/td-p/7279443/page/3
            if (!documents.length) return;
            whatApp = String(app.name); //String version of the app name
            if (whatApp.search("Photoshop") > 0) { //Check for photoshop specifically, or this will cause errors
                if (ExternalObject.AdobeXMPScript === undefined) ExternalObject.AdobeXMPScript = new ExternalObject("lib:AdobeXMPScript");
                var xmp = new XMPMeta(activeDocument.xmpMetadata.rawData);
                // Begone foul Document Ancestors!
                xmp.deleteProperty(XMPConst.NS_PHOTOSHOP, "DocumentAncestors");
                app.activeDocument.xmpMetadata.rawData = xmp.serialize();
            }
        }

    })();

} else {
    alert('A document must be open to run this script!');
}

 

I presumed that you wanted each separate visible layer as a separate file, however, you might have meant something else.

Participant
June 18, 2024

omg thankyou soo much for this, we had a problem in our code with the parser not being able to correctly read data into the json format but we managed to fix that. However i have another issue that is to recreate the psd back from this json file.

 

Stephen Marsh
Community Expert
Community Expert
June 18, 2024
quote

omg thankyou soo much for this


By @Prithvi Singh38041280pksf

 

You're welcome.

Stephen Marsh
Community Expert
Community Expert
June 14, 2024
quote

I want to make an automated script in jsx


By @Prithvi Singh38041280pksf

 

So what's the problem? Where are you stuck?