6

I am writing a script that will loop through layers, trim them and export. So far I have most of all the element I need to complete this script. The only thing I can't find is how to show/hide an individual layer. I've found functions to show/hide all layers but nothing for one single layer.

///////////////////////////////////////////////////////////////////////////////
// selectAllLayers - select all layers (Select > All Layers)
///////////////////////////////////////////////////////////////////////////////
function selectAllLayers() {
    var ref = new ActionReference();
    ref.putEnumerated(cTID('Lyr '), cTID('Ordn'), cTID('Trgt'));
    var desc = new ActionDescriptor();
    desc.putReference(cTID('null'), ref);

    executeAction(sTID('selectAllLayers'), desc, DialogModes.NO);
}

///////////////////////////////////////////////////////////////////////////////
// hideLayers - hide all selected layers (Layer > Hide Layers)
///////////////////////////////////////////////////////////////////////////////
function hideLayers() {
    var ref = new ActionReference();
    ref.putEnumerated(cTID('Lyr '), cTID('Ordn'), cTID('Trgt'));
    var list = new ActionList();
    list.putReference(ref);
    var desc = new ActionDescriptor();
    desc.putList(cTID('null'), list);
    executeAction(cTID('Hd  '), desc, DialogModes.NO);
}

function cTID(s) {return app.charIDToTypeID(s);}
function sTID(s) {return app.stringIDToTypeID(s);}

Any ideas?

1 Answer 1

9

The Layer object has a .visible boolean property which you can use to control visibility for each layer individually:

// make active layer invisible
app.activeDocument.activeLayer.visible = false;

or

// make active layer visible
app.activeDocument.activeLayer.visible = true;

or even toggle visibility for the selected/active layer:

app.activeDocument.activeLayer.visible = !app.activeDocument.activeLayer.visible;

or loop through what layers you need and toggle their visibility:

//example hides odd layers while showing even layers, based on their index
var doc = app.activeDocument;
for(var i = 0 ; i < doc.layers.length;i++){
    doc.layers[i].visible = (i % 2 == 0);
}

I suggest having a looks either in the Photoshop CS5 Javascript Reference (PDF link) or in ExtendScript Toolkit's Object Model Viewer.

You can access it via Help > Object Model Viewer and select the Adobe Photoshop CS5 Object Library from the browser combobox/list to the list of classes available in the Photoshop DOM.

Sign up to request clarification or add additional context in comments.

2 Comments

Also add app.activeDocument.activeLayer.visible = true to make the layer visible and ... = false to make the layer invisible
@BlackThunder thanks for that, it will be clearer seeing that first. I've updated the answer accordingly and fixed the PDF link while I was at it :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.