Skip to main content
Known Participant
October 4, 2025
Answered

Script for turning off all layers except the background layer

  • October 4, 2025
  • 2 replies
  • 250 views

Hi, i am having a numerous comp and now my boss wants to put a demoreel showing the input and output. But it is very tedious as i have to open each and every document hide all layers except the last one below(original) and then save it as a jpeg in a seperate folder named input and  to close the file without saving . Is there a script that could to do this exact job for me.

Thanks

Correct answer Stephen Marsh

@raagav_9457 / @rajsriguide 

 

Try this script:

/* 
Batch Save Back Layer As JPEG to Sub-Folder.jsx
Stephen Marsh
v1.0 - 6th October 2025: Initial release
https://community.adobe.com/t5/photoshop-ecosystem-discussions/script-for-turning-off-all-layers-except-the-background-layer/td-p/15533386
*/

#target photoshop

(function () {

    var inputFolder = Folder.selectDialog("Select an input folder containing PSD/PSB files");
    if (inputFolder == null) return;

    // Collect all PSD/PSB input files
    var processFiles = inputFolder.getFiles(function (theFiles) {
        return theFiles instanceof File && theFiles.name.match(/\.(psd|psb)$/i);
    });

    if (processFiles.length === 0) {
        alert("No PSD or PSB files found in the selected folder.");
        return;
    }

    // Create the output directory named "Input"
    var outputFolder = new Folder(inputFolder.fsName + "/" + "Input"); // Change the name from "Input" as required
    if (!outputFolder.exists) outputFolder.create();

    app.togglePalettes();

    // Create the scriptUI progress bar
    var progressWin = new Window("palette", "Processing Files", undefined, { closeButton: false });
    progressWin.orientation = "column";
    progressWin.alignChildren = ["fill", "top"];
    progressWin.margins = 15;
    var infoText = progressWin.add("statictext", undefined, "Starting...");
    infoText.characters = 50;
    var progressBar = progressWin.add("progressbar", undefined, 0, processFiles.length);
    progressBar.preferredSize = [400, 20];
    var countText = progressWin.add("statictext", undefined, "0 of " + processFiles.length);
    progressWin.show();

    // Process the files
    for (var i = 0; i < processFiles.length; i++) {
        var currentFile = processFiles[i];
        try {

            // Progress bar processing
            progressBar.value = i;
            countText.text = (i + 1) + " of " + processFiles.length;
            infoText.text = "Processing: " + decodeURI(currentFile.name);
            progressWin.update();

            var theDoc = app.open(currentFile);
            var baseName = decodeURI(theDoc.name).replace(/\.[^\.]+$/, "");

            // Layer visibility
            backLayerVisible();

            // JPEG save path
            var jpegFile = new File(outputFolder.fsName + "/" + baseName + ".jpg");

            // Set the JPEG save options
            var jpegOptions = new JPEGSaveOptions();
            jpegOptions.quality = 12;
            jpegOptions.embedColorProfile = true;
            jpegOptions.formatOptions = FormatOptions.STANDARDBASELINE;

            // Save the JPEG copy
            theDoc.saveAs(jpegFile, jpegOptions, true, Extension.LOWERCASE);

            // Close the PSD/PSB without saving changes
            theDoc.close(SaveOptions.DONOTSAVECHANGES);

        } catch (e) {
            alert("Error processing " + currentFile.name + ":\n" + e);
        }
    }

    app.beep()

    // End of progress bar processing
    progressBar.value = processFiles.length;
    infoText.text = "Done... All PSD/PSB files saved to JPEGs!";
    countText.text = processFiles.length + " of " + processFiles.length;
    progressWin.update();
    $.sleep(1000);
    app.bringToFront();
    progressWin.close();

    // End of script notification;
    //alert("All PSD/PSB files saved to JPEGs!");
    app.togglePalettes();

    function backLayerVisible() {
        if (!app.documents.length) return;
        var doc = app.activeDocument;
        var layers = doc.layers;
        var total = layers.length;
        if (total < 1) return;
        for (var i = 0; i < total; i++) {
            try {
                // Turn off visibility for all top-level/root layers
                layers[i].visible = false;
            } catch (e) { }
        }
        try {
            // Turn on the visibility of the back layer
            layers[total - 1].visible = true;
        } catch (e) { }
    }

})();

 

  1. Copy the code text to the clipboard
  2. Open a new blank file in a plain-text editor (not in a word processor)
  3. Paste the code in
  4. Save as a plain text format file – .txt
  5. Rename the saved file extension from .txt to .jsx
  6. Install or browse to the .jsx file to run (see below)

https://prepression.blogspot.com/2017/11/downloading-and-installing-adobe-scripts.html

 

2 replies

Stephen Marsh
Community Expert
Stephen MarshCommunity ExpertCorrect answer
Community Expert
October 6, 2025

@raagav_9457 / @rajsriguide 

 

Try this script:

/* 
Batch Save Back Layer As JPEG to Sub-Folder.jsx
Stephen Marsh
v1.0 - 6th October 2025: Initial release
https://community.adobe.com/t5/photoshop-ecosystem-discussions/script-for-turning-off-all-layers-except-the-background-layer/td-p/15533386
*/

#target photoshop

(function () {

    var inputFolder = Folder.selectDialog("Select an input folder containing PSD/PSB files");
    if (inputFolder == null) return;

    // Collect all PSD/PSB input files
    var processFiles = inputFolder.getFiles(function (theFiles) {
        return theFiles instanceof File && theFiles.name.match(/\.(psd|psb)$/i);
    });

    if (processFiles.length === 0) {
        alert("No PSD or PSB files found in the selected folder.");
        return;
    }

    // Create the output directory named "Input"
    var outputFolder = new Folder(inputFolder.fsName + "/" + "Input"); // Change the name from "Input" as required
    if (!outputFolder.exists) outputFolder.create();

    app.togglePalettes();

    // Create the scriptUI progress bar
    var progressWin = new Window("palette", "Processing Files", undefined, { closeButton: false });
    progressWin.orientation = "column";
    progressWin.alignChildren = ["fill", "top"];
    progressWin.margins = 15;
    var infoText = progressWin.add("statictext", undefined, "Starting...");
    infoText.characters = 50;
    var progressBar = progressWin.add("progressbar", undefined, 0, processFiles.length);
    progressBar.preferredSize = [400, 20];
    var countText = progressWin.add("statictext", undefined, "0 of " + processFiles.length);
    progressWin.show();

    // Process the files
    for (var i = 0; i < processFiles.length; i++) {
        var currentFile = processFiles[i];
        try {

            // Progress bar processing
            progressBar.value = i;
            countText.text = (i + 1) + " of " + processFiles.length;
            infoText.text = "Processing: " + decodeURI(currentFile.name);
            progressWin.update();

            var theDoc = app.open(currentFile);
            var baseName = decodeURI(theDoc.name).replace(/\.[^\.]+$/, "");

            // Layer visibility
            backLayerVisible();

            // JPEG save path
            var jpegFile = new File(outputFolder.fsName + "/" + baseName + ".jpg");

            // Set the JPEG save options
            var jpegOptions = new JPEGSaveOptions();
            jpegOptions.quality = 12;
            jpegOptions.embedColorProfile = true;
            jpegOptions.formatOptions = FormatOptions.STANDARDBASELINE;

            // Save the JPEG copy
            theDoc.saveAs(jpegFile, jpegOptions, true, Extension.LOWERCASE);

            // Close the PSD/PSB without saving changes
            theDoc.close(SaveOptions.DONOTSAVECHANGES);

        } catch (e) {
            alert("Error processing " + currentFile.name + ":\n" + e);
        }
    }

    app.beep()

    // End of progress bar processing
    progressBar.value = processFiles.length;
    infoText.text = "Done... All PSD/PSB files saved to JPEGs!";
    countText.text = processFiles.length + " of " + processFiles.length;
    progressWin.update();
    $.sleep(1000);
    app.bringToFront();
    progressWin.close();

    // End of script notification;
    //alert("All PSD/PSB files saved to JPEGs!");
    app.togglePalettes();

    function backLayerVisible() {
        if (!app.documents.length) return;
        var doc = app.activeDocument;
        var layers = doc.layers;
        var total = layers.length;
        if (total < 1) return;
        for (var i = 0; i < total; i++) {
            try {
                // Turn off visibility for all top-level/root layers
                layers[i].visible = false;
            } catch (e) { }
        }
        try {
            // Turn on the visibility of the back layer
            layers[total - 1].visible = true;
        } catch (e) { }
    }

})();

 

  1. Copy the code text to the clipboard
  2. Open a new blank file in a plain-text editor (not in a word processor)
  3. Paste the code in
  4. Save as a plain text format file – .txt
  5. Rename the saved file extension from .txt to .jsx
  6. Install or browse to the .jsx file to run (see below)

https://prepression.blogspot.com/2017/11/downloading-and-installing-adobe-scripts.html

 

Participating Frequently
October 6, 2025

Perfect.. Thanks Steph.

Stephen Marsh
Community Expert
Community Expert
October 4, 2025

I'm not aware of a generic script for this, it would need to created.

 

Edit: This code will turn off the visibility for all layers except the very back layer, whether a true Background layer or just a floating back layer.:

 

#target photoshop

app.activeDocument.suspendHistory("Show only back layer", "main()");

function main() {
    if (!app.documents.length) return;
    var doc = app.activeDocument;
    var layers = doc.layers;

    var total = layers.length;
    if (total < 1) return;

    for (var i = 0; i < total; i++) {
        try {
            // Turn off visibility for all top-level/root layers
            layers[i].visible = false;
        } catch (e) {}
    }

    try {
        // Turn on the visibility of the back layer
        layers[total - 1].visible = true;
    } catch (e) {}
}

 

This code would need to be incorporated into a larger script... On that point, it's not clear if there is a single output directory named "input" and where this is located, or if there are multiple folders named "input" which are alongside the current open document.

 

Then there are the JPEG settings, which are unknown.

 

Finally, would the filename be exactly the same as the original document? Is there a possibility of multiple documents from different source folders having the same name and clashing if saved into a single folder?

Participating Frequently
October 6, 2025

Hi stephen , thanks for the response. What i am actually looking for is if i have a set of Photoshop files inside a folder named PSD and If i drag and drop the script, the script should prompt me for the source folder which is itself the PSD folder in 😧 drive for example, the script should open the PSD file solo the last layer which is the background layer save the file as a .jpeg file at max resolution in a folder named input created by the script(or any other name) and then close the psd file without saving. The process repeats until all the files inside the folder. PFA. This is just to show before and after to client. 

Thanks.