How to select visible layers fast via script?

Discussion of Photoshop Scripting, Photoshop Actions and Photoshop Automation in General

Moderators: Tom, Kukurykus

qwerty123

How to select visible layers fast via script?

Post by qwerty123 »

If I have a psd file with more than 100 layers and almost all of them hidden except 2-5 layers is there a way to quickly select those layers via script. If I use standard approach and iterate over all layers, checking their visibility status then it takes the script too long, especially when such procedure is repeated several times per script...
I hope someone can help.

qwerty123

Professional AI Audio Generation within Adobe Premiere Pro - Download Free Plugin here

Paul MR

How to select visible layers fast via script?

Post by Paul MR »

This should be fast ....

Code: Select all#target photoshop
app.bringToFront();

main();
function main(){
if(!documents.length) return;
var Vis = getVisLayers();
deselectLayers();
for(var a in Vis){
    selectLayerById(Number(Vis[a]),true);
    }
}
function getVisLayers(){
   var ref = new ActionReference();
   ref.putEnumerated( charIDToTypeID('Dcmn'), charIDToTypeID('Ordn'), charIDToTypeID('Trgt') );
   var count = executeActionGet(ref).getInteger(charIDToTypeID('NmbL')) +1;
   var Names=[];
try{
    activeDocument.backgroundLayer;
var i = 0; }catch(e){ var i = 1; };
   for(i;i<count;i++){
       if(i == 0) continue;
        ref = new ActionReference();
        ref.putIndex( charIDToTypeID( 'Lyr ' ), i );
        var desc = executeActionGet(ref);
        var layerName = desc.getString(charIDToTypeID( 'Nm  ' ));
        var Id = desc.getInteger(stringIDToTypeID( 'layerID' ));
        if(layerName.match(/^<\/Layer group/) ) continue;
        var layerType = typeIDToStringID(desc.getEnumerationValue( stringIDToTypeID( 'layerSection' )));
        var isLayerSet =( layerType == 'layerSectionContent') ? false:true;
        var vis = desc.getBoolean(charIDToTypeID( "Vsbl" ));
        if(!isLayerSet && vis) Names.push(Id);
   };
return Names;
};
function selectLayerById(ID, add) {
    add = (add == undefined)  ? add = false : add;
   var ref = new ActionReference();
   ref.putIdentifier(charIDToTypeID('Lyr '), ID);
   var desc = new ActionDescriptor();
   desc.putReference(charIDToTypeID('null'), ref);
   if (add) {
      desc.putEnumerated(stringIDToTypeID('selectionModifier'), stringIDToTypeID('selectionModifierType'), stringIDToTypeID('addToSelection'));
   }
   desc.putBoolean(charIDToTypeID('MkVs'), false);
   executeAction(charIDToTypeID('slct'), desc, DialogModes.NO);
}
function deselectLayers() {
    var desc01 = new ActionDescriptor();
        var ref01 = new ActionReference();
        ref01.putEnumerated( charIDToTypeID('Lyr '), charIDToTypeID('Ordn'), charIDToTypeID('Trgt') );
    desc01.putReference( charIDToTypeID('null'), ref01 );
    executeAction( stringIDToTypeID('selectNoLayers'), desc01, DialogModes.NO );
};
qwerty123

How to select visible layers fast via script?

Post by qwerty123 »

Wow, Paul! Impressive.
Thank you!

Is there a repository of canned functions of this type somewhere?
Paul MR

How to select visible layers fast via script?

Post by Paul MR »

There are a number of functions based on Action Manager code on this site, maybe if you had a wish list of functions that list could be put together. Mike has done some fantastic work getting layerset layers, an example of using his code can be found here...
http://forums.adobe.com/thread/1101485?tstart=0
qwerty123

How to select visible layers fast via script?

Post by qwerty123 »

Paul, thank you for your help, the script and the reference. The script have already opened new approaches for my workflow. The code is a bit cryptic - can you give me some insight on why it works so much faster than regular comparison? I mean some sort of comparison has to happen one way or another for a layer to be selected, so why this approach is faster?
Paul MR

How to select visible layers fast via script?

Post by Paul MR »

I think the main problem using thr DOM to access the layer is that it gets all the layers properties including the document histogram. Using Action Manager you by-pass this.
larsen67

How to select visible layers fast via script?

Post by larsen67 »

Is there a repository of canned functions of this type somewhere?

Quite possibly the biggest and best is a file by xbytor called stdlib.js if you have his toolkit installed you should open this and have a look… gulp