Skip to main content
mysysctrlm74982608
Known Participant
February 8, 2023
解決済み

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

  • February 8, 2023
  • 返信数 6.
  • 3645 ビュー

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

このトピックへの返信は締め切られました。
解決に役立った回答 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

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.

c.pfaffenbichler
Community Expert
Community Expert
February 10, 2023

Suppose there are three layers in a document: Layer A, Layer B, and Layer C; A is above them two, and C is at the bottom; I need to use shortcuts to turn A's visibility off and switch it back to on. And meanwhile, I can also turn off other layers' visibility through other shortcuts. Is that clear?



@mysysctrlm74982608 wrote:

Suppose there are three layers in a document: Layer A, Layer B, and Layer C; A is above them two, and C is at the bottom; I need to use shortcuts to turn A's visibility off and switch it back to on. And meanwhile, I can also turn off other layers' visibility through other shortcuts. Is that clear?


So you primarily want a shoftcut to hide/show the topmost Layer? 

Why have you refused to say so until now? 

 

What if there are more than three Layers?

Would you expect a shortcut for each one’s visibility? 

 

Please provide a meaningful explanation with screenshots. 

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.