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
  • 3645 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 10, 2023

Turn off the visibility of the specified layer by shortcut. Is that clear? It's not my first language but my second; why do you always emphasize the language problem after I say I understand what you said? What do you not get me? Are my sentences stiff?


quote

Turn off the visibility of the specified layer by shortcut. Is that clear?

 

Sadly no, as you have not mentioned how the layer is specified. You need to clearly state how the layer is specified, is that clear?

 

 

It's not my first language but my second; why do you always emphasize the language problem after I say I understand what you said? What do you not get me? Are my sentences stiff?


By @mysysctrlm74982608

 

I only have one language, so I admire those that know more than one as learning a foreign language is difficult for me. 

 

The reason that I raise the communication issue is that despite your assertions that you understand what I say, you are not providing the information that I am asking for to allow me to help you. So if there isn't a communication issue, then I can only assume that you have decided to ignore repeated requests from multiple people for the required information to help you.

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.