Skip to main content
Participant
December 30, 2010
Question

toggle layer visibility

  • December 30, 2010
  • 2 replies
  • 8231 views

Hello,

I need one keyboard shortcut that toggles visibility of one of layers.

I paint and need to toggle on and off one layer below (layer with a reference picture, not one i paint on). with a shortcut painting process will be not break as with case click at icon.

tried to make toggle layer visibility with actions unfortunately with no success.

it is possibility to toggle active layer visibility by add shortcut in keyboard shortcuts preferences  but here it going not about active layer.

I have no experience with scripting.

thank you!

This topic has been closed for replies.

2 replies

rcraighead
Legend
October 12, 2018

This thread has helped me. I only needed to toggle a named layer. This is the Javascript that works for me. Perhaps someone else will find it useful.

app.activeDocument.layers.getByName('LayerNameHere').visible ^= 1;
Kukurykus
Legend
October 12, 2018

Good to know '^=' toggles visibility.

Inspiring
December 30, 2010

If I understand what you are asking the script below will toggle the layer visibility of the layer below the active/selected layer. It works with layerSets. If the selected layer is at the bottom of the stack/set, the script does nothing.

var curentLayer = app.activeDocument.activeLayer;
var curentParent = curentLayer.parent;
var currentIndex = getLayerIndex( curentParent, curentLayer );
if(currentIndex != (curentParent.artLayers.length-1)){
    curentParent.artLayers[currentIndex+1].visible= !curentParent.artLayers[currentIndex+1].visible;
}
function getLayerIndex( parent, layer ){
    for(var i=0;i<parent.artLayers.length;i++){
        if(parent.artLayers==layer) return i;
    }
};

Participant
December 30, 2010

Exactly what i need! works great! Thank you!!

it is possible to have some additional functionality like that: script check if layer named "Ref" exist and if yes  toggles its visibility ?

many thanks!

Participant
December 30, 2010

For this to work as written there can only be one layer in the document named 'Ref' otherwise it will toggle the top most layer with that name.

var targetID = getLayerIDByName('Ref');
if(undefined != targetID){
    if(getLayerVisibilityByID( targetID ) ){
        hideByName('Ref');
    }else{
        showByName('Ref');
    }
}
function getLayerIDByName(name) {
    try{
    var ref = new ActionReference();
    ref.putProperty( charIDToTypeID("Prpr") , charIDToTypeID( "LyrI" ));
    ref.putName( charIDToTypeID( "Lyr " ), name );
    return executeActionGet(ref).getInteger(charIDToTypeID( "LyrI" ));
    }catch(e){}
};
function getLayerVisibilityByID( id ) {
    var ref = new ActionReference();
    ref.putProperty( charIDToTypeID("Prpr") , charIDToTypeID( "Vsbl" ));
    ref.putIdentifier( charIDToTypeID( "Lyr " ), id );
    return executeActionGet(ref).getBoolean(charIDToTypeID( "Vsbl" ));
};
function hideByName(name) {
    var desc = new ActionDescriptor();
        var list = new ActionList();
            var ref = new ActionReference();
            ref.putName( charIDToTypeID('Lyr '), name );
        list.putReference( ref );
    desc.putList( charIDToTypeID('null'), list );
    executeAction( charIDToTypeID('Hd  '), desc, DialogModes.NO );
};
function showByName(name) {
    var desc = new ActionDescriptor();
        var list = new ActionList();
            var ref = new ActionReference();
            ref.putName( charIDToTypeID('Lyr '), name );
        list.putReference( ref );
    desc.putList( charIDToTypeID('null'), list );
    executeAction( charIDToTypeID('Shw '), desc, DialogModes.NO );
};


Michael, you are my Santa!

Thank you very much!