Photoshop CS 2 Make only one layer visible

Photoshop Script Snippets - Note: Full Scripts go in the Photoshop Scripts Forum

Moderators: Tom, Kukurykus

Larry Ligon

Photoshop CS 2 Make only one layer visible

Post by Larry Ligon »

This function will make only one layer visible. All other layers will be made invisible regardless of whether or not they are visible. This is the JavaScript for using the alt + click on visible button on the layer.

Save this code into a file named: MakeLayerOnlyVisible.jsxinc

Code: Select allfunction MakeLayerOnlyVisible(layerName) {

var id3 = charIDToTypeID( "Shw " );
    var desc2 = new ActionDescriptor();
    var id4 = charIDToTypeID( "null" );
        var list1 = new ActionList();
            var ref1 = new ActionReference();
            var id5 = charIDToTypeID( "Lyr " );
            ref1.putName( id5, layerName);
        list1.putReference( ref1 );
    desc2.putList( id4, list1 );
    var id6 = charIDToTypeID( "TglO" );
    desc2.putBoolean( id6, true );
executeAction( id3, desc2, DialogModes.NO )
};

Save this file in the scripts folder. To call it pass the layer name to it.
For example: MakeLayerOnlyVisible(docRef.layers[selectedlayer].name);

Here is some test code:

Code: Select all// You must have a document open
if (documents.length > 0)
 {
// No dialogs
displayDialogs = DialogModes.NO;

var docRef = activeDocument;
var layerCount = docRef.layers.length;

//Include the function.  It is in the scripts folder
#include "MakeLayerOnlyVisible.jsxinc"

var strMessage = "";
for(var layer=0; layer<layerCount  ; layer++)
 {
  strMessage = strMessage   + "Index is " + layer + "  Layer name is " + docRef.layers[layer].name + " visible = " +docRef.layers[layer].visible+"\n";
};
alert(strMessage );

//This is the layer that will be made visible
var selectedlayer = 0;
alert("This layer will be made visible. Index is " + selectedlayer  + "  Layer name is " + docRef.layers[selectedlayer].name );

MakeLayerOnlyVisible(docRef.layers[selectedlayer].name);

var strMessage = "";
for(var layer=0; layer<layerCount  ; layer++)
 {
  strMessage = strMessage   +"Index is " + layer + "  Layer name is " + docRef.layers[layer].name + " visible = " +docRef.layers[layer].visible+"\n";
};
alert(strMessage );
}
else
 { alert("No document open.\nYou must have a document with layers open");
 };

It uses an #include statement to include the function file.

Larry