Skip to main content
Participant
July 15, 2022
Answered

Script to toggle visibility of the layer below

  • July 15, 2022
  • 1 reply
  • 511 views

Hello!

I would like to know if anyone knows a script or another way to make the layer below the selected one to toggle its visibility, because I am running an action to many files and the last one needs to be duplicated constantly, but it has transparency so I need to manually toggle the layer all the time.

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

@KingOfCanvas 

 

Try the following script:

 

/*
Toggle Visibility of Previous Layer.jsx
https://community.adobe.com/t5/photoshop-ecosystem-discussions/script-to-toggle-visibility-of-the-layer-below/td-p/13072521
Stephen Marsh, 16th July 2022, Version 1
*/

if (activeDocument.layers.length > 1) {

    try {

        selectLayerByIndex(getActiveLayerIndex() - 1);
        activeDocument.activeLayer.visible = !activeDocument.activeLayer.visible
        selectLayerByIndex(getActiveLayerIndex() + 1);

        function selectLayerByIndex(index) {
            /* https://github.com/ES-Collection/Photoshop-Scripts/blob/master/Remove%20Unused%20Layers.jsx */
            var c2t = function (s) {
                return app.charIDToTypeID(s);
            };
            var s2t = function (s) {
                return app.stringIDToTypeID(s);
            };
            var descriptor = new ActionDescriptor();
            var reference = new ActionReference();
            reference.putIndex(s2t("layer"), index);
            descriptor.putReference(c2t("null"), reference);
            descriptor.putBoolean(s2t("makeVisible"), false);
            executeAction(s2t("select"), descriptor, DialogModes.NO);
        }

        function getActiveLayerIndex() {
            /* https://github.com/Paul-Riggott/PS-Scripts/blob/master/getLayersetLayerIDs.jsx */
            var ref = new ActionReference();
            ref.putEnumerated(charIDToTypeID('Lyr '), charIDToTypeID('Ordn'), charIDToTypeID('Trgt'));
            try {
                activeDocument.backgroundLayer;
                return executeActionGet(ref).getInteger(charIDToTypeID("ItmI")) - 1;
            } catch (e) {
                return executeActionGet(ref).getInteger(charIDToTypeID("ItmI"));
            }
        }

    } catch (e) { }
    
}

 

I'm sure that there is a more direct, less verbose approach...

1 reply

Stephen Marsh
Community Expert
Stephen MarshCommunity ExpertCorrect answer
Community Expert
July 16, 2022

@KingOfCanvas 

 

Try the following script:

 

/*
Toggle Visibility of Previous Layer.jsx
https://community.adobe.com/t5/photoshop-ecosystem-discussions/script-to-toggle-visibility-of-the-layer-below/td-p/13072521
Stephen Marsh, 16th July 2022, Version 1
*/

if (activeDocument.layers.length > 1) {

    try {

        selectLayerByIndex(getActiveLayerIndex() - 1);
        activeDocument.activeLayer.visible = !activeDocument.activeLayer.visible
        selectLayerByIndex(getActiveLayerIndex() + 1);

        function selectLayerByIndex(index) {
            /* https://github.com/ES-Collection/Photoshop-Scripts/blob/master/Remove%20Unused%20Layers.jsx */
            var c2t = function (s) {
                return app.charIDToTypeID(s);
            };
            var s2t = function (s) {
                return app.stringIDToTypeID(s);
            };
            var descriptor = new ActionDescriptor();
            var reference = new ActionReference();
            reference.putIndex(s2t("layer"), index);
            descriptor.putReference(c2t("null"), reference);
            descriptor.putBoolean(s2t("makeVisible"), false);
            executeAction(s2t("select"), descriptor, DialogModes.NO);
        }

        function getActiveLayerIndex() {
            /* https://github.com/Paul-Riggott/PS-Scripts/blob/master/getLayersetLayerIDs.jsx */
            var ref = new ActionReference();
            ref.putEnumerated(charIDToTypeID('Lyr '), charIDToTypeID('Ordn'), charIDToTypeID('Trgt'));
            try {
                activeDocument.backgroundLayer;
                return executeActionGet(ref).getInteger(charIDToTypeID("ItmI")) - 1;
            } catch (e) {
                return executeActionGet(ref).getInteger(charIDToTypeID("ItmI"));
            }
        }

    } catch (e) { }
    
}

 

I'm sure that there is a more direct, less verbose approach...

Participant
July 18, 2022

Thank you so much, it works perfectly!