Search code examples
photoshopphotoshop-script

Turning Multiple Layers On and Off in Photoshop with export image Script


I have 1 group in photoshop in which there are multiple images which are hide by default except the first one, I looking to turn show every image one by one except first one and then export as image with merged first layer

Note : Please see the screenshots Layer

Can someone point me in the direction whether it is possible with script or any action?

I've never scripted in Photoshop but trying to figure this out on my own.


Solution

  • Whole post edited, yet this script covers usability of the original script (that worked only on a single layer group) and also extends itself onto all layer groups.

    Script takes the layers from each layer groups in the file and iterates through all of them except the first one in the given group turning them on and off for saving. First layer in each group is always visible for a given group iteration. Exports as PNG but you may adjust anything you need - export options, path, file name etc. Created for CS6 but should work with any version, just check it out.

    Any more information you may need is in the reference manual. Just Google for Photoshop CS6/CC Javascript Reference.

    The code's below, just copy-paste it using Notepad and save as jsx file. You may test it using ExtendScript Toolkit (it has been installed together with anything from Adobe, you just need to find it). If it works as intended, place the file within Adobe\Adobe Photoshop CS6 (64 Bit)\Presets\Scripts folder, restart Photoshop and the script will be accessible through File > Scripts menu. You may also bind a keyboard shortcut to it if this action needs to be repeated over and over many times. Cheers!

    #target Photoshop
    if (documents.length == 0) {
        alert("nothing opened");
    } else {
        // start
    
        //setup
        var file = app.activeDocument;
        var groupsAmount = file.layerSets.length; // get the layer groups
    
        // iterate through groups
        for (var k=0; k < groupsAmount; k++) {
    
            hideAllLayers();
            var images = file.layerSets[k].layers; // get the layers from the given group
            file.layerSets[k].visible = true;
            images[0].visible = true; // show the first layer in this group
    
            // begin "i" from 1 to start from the 2nd layer
            for (var i=1; i < images.length; i++) {
                images[i].visible = true;
                exportimage(images[i].name);
                images[i].visible = false;                
            }
        }
    
    
       // function used to export image (adjust as you want accoring to the manual)
        function exportimage(name){
            var options = new ExportOptionsSaveForWeb();
    
                options.format = SaveDocumentType.PNG;
                options.PNG8 = false;
                options.transparency = true;
                options.optimized = true;
    
            // adjust path & name
            file.exportDocument(File(file.path+"/"+name+".png"),ExportType.SAVEFORWEB, options);
        }
    
        // function to hide all layers
        // simplified version of http://morris-photographics.com/photoshop/scripts/downloads/Hide%20All%20Layers%202-1-0.jsx
        function hideAllLayers() {
            var ref = new ActionReference();
            ref.putEnumerated(cTID('Lyr '), cTID('Ordn'), cTID('Trgt'));
            var desc = new ActionDescriptor();
            desc.putReference(cTID('null'), ref);
            executeAction(sTID('selectAllLayers'), desc, DialogModes.NO);
            var ref = new ActionReference();
            ref.putEnumerated(cTID('Lyr '), cTID('Ordn'), cTID('Trgt'));
            var list = new ActionList();
            list.putReference(ref);
            var desc = new ActionDescriptor();
            desc.putList(cTID('null'), list);
            executeAction(cTID('Hd  '), desc, DialogModes.NO);
    
        }
        function cTID(s) {return app.charIDToTypeID(s);}
        function sTID(s) {return app.stringIDToTypeID(s);}
    
        //end
    }