Skip to main content
mysysctrlm74982608
Known Participant
February 8, 2023
Answered

How to turn off the visibility of a specific layer by a shortcut and switching it.

  • February 8, 2023
  • 6 replies
  • 3624 views

How to turn off the visibility of a specific layer by a shortcut and switching it.

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

I understand the two cases you provided, and I executed them successfully; both can not set a keyboard shortcut from the shortcuts menu panel, and without the linkage of shortcuts, it is helpless.



@mysysctrlm74982608 wrote:

I understand the two cases you provided, and I executed them successfully; both can not set a keyboard shortcut from the shortcuts menu panel, and without the linkage of shortcuts, it is helpless.


 

In order to set a keyboard shortcut against a script, the script has to be installed (not run via File > Scripts > Browse...) – Once installed, the script will be available in the File > Scripts menu (or possibly in another menu such as Automate if the script has extra code added). An installed script will also be visible in Edit > Keyboard Shortcuts (Application Menus, File, Scripts>)

 

Scripts are installed in the /Presets/Scripts folder.

Mac OS Example:

  • /Applications⁩/Adobe Photoshop CC 2019⁩/Presets⁩/Scripts
  • /Applications/Adobe Photoshop 2021/Presets/Scripts

Win OS Example:
  • C:\Program Files\Adobe\Adobe Photoshop CC 2019\Presets\Scripts
  • C:\Program Files\Adobe\Adobe Photoshop 2021\Presets\Scripts

 

(If the path does not match your version, it should be a simple enough process to find the correct folder using this guide)

 

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

 

6 replies

Stephen Marsh
Community Expert
Community Expert
February 10, 2023

The following script will toggle the visibility of the previous layer relative to the currently selected layer (if no layer is selected, nothing happens):

 

/*
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
*/

#target photoshop

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) { }
    
}

 

 

The following script will toggle the visibility of the next layer relative to the currently selected layer (if no layer is selected, nothing happens):

 

/*
Toggle Visibility of Next 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
*/

#target photoshop

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) { }
    
}

 

Stephen Marsh
Community Expert
Community Expert
February 10, 2023

The following script will toggle the visibility of the back/bottom layer (including the Background layer) without selecting it, regardless of whether a layer is selected:

 

var backLayer = activeDocument.layers[activeDocument.layers.length - 1];
backLayer.visible = !backLayer.visible;

 

or:

 

var backLayer = [].slice.call(activeDocument.layers).reverse()[0];
backLayer.visible ^= 1;

 

Install the script in the Presets/Scripts folder in your Photoshop application folder. Use Edit > Keyboard Shortcuts to define a custom keyboard shortcut for the installed script. Alternatively, record the script execution into an action, then use an action F-key shortcut.

Stephen Marsh
Community Expert
Community Expert
February 10, 2023

The following script will toggle the visibility of the front/top layer without selecting it, regardless of whether a layer is selected:

 

activeDocument.layers[0].visible = !activeDocument.layers[0].visible;

 

Install the script in the Presets/Scripts folder in your Photoshop application folder. Use Edit > Keyboard Shortcuts to define a custom keyboard shortcut for the installed script. Alternatively, record the script execution into an action, then use an action F-key shortcut.

 

mysysctrlm74982608
Known Participant
February 11, 2023

Thanks, but to use Actions have to record two different ones; one is to show Layer X, and the other is to hide. Correspondingly I have to set another excess shortcut.

Stephen Marsh
Community Expert
Community Expert
February 11, 2023
quote

Thanks, but to use Actions have to record two different ones; one is to show Layer X, and the other is to hide. Correspondingly I have to set another excess shortcut.


By @mysysctrlm74982608

 

No, I was suggesting that a script to toggle on/off visibility can either be run by keyboard shortcut against the installed script, or by recording the script into an action F-key. In either case, only one keyboard shortcut is required as the script is performing the toggle/switch of visibility (one script, two outcomes, whether executed from script keyboard shortcut or action F-key shortcut).

 

Have you actually tried the four scripts that I have provided?

c.pfaffenbichler
Community Expert
Community Expert
February 8, 2023

How is the »specific layer« defined? By name, by position in the Layer Stack, …? 

mysysctrlm74982608
Known Participant
February 9, 2023

It means an unselected layer that can be executed by shortcut to select.

Stephen Marsh
Community Expert
Community Expert
February 9, 2023

@mysysctrlm74982608 – Photoshop doesn't have extrasensory perception or the ability to read the users mind. How do you expect Photoshop to "know" (be instructed) as to which unselected layer/s need to have their visibility modified? 

 

What @c.pfaffenbichler is asking for is information that can be programmatically applied through scripting, i.e. is the layer name consistent (layer name = "template"), or is the layer position consistent in the stack (3rd layer from the bottom) or is the layer above or below the currently selected layer etc. Is this simply a toggle of current visibility (if on, turn off - if off,  turn on) or is it just set to set to off regardless of the current visibility state.

 

 

Kevin Stohlmeyer
Community Expert
Community Expert
February 8, 2023

Cmd+comma toggles layer visibility for your selected layer.

You can edit the shortcut under Edit Keyboard Shortcuts/Applications Menu/Layer/Show Layers

 

mysysctrlm74982608
Known Participant
February 9, 2023

Thx, I know this solution, but I need an unselected layer to be executed.

Mylenium
Legend
February 8, 2023

You can't. It's as simple as that.

 

Mylenium

Kevin Stohlmeyer
Community Expert
Community Expert
February 8, 2023

@Mylenium yes you can.