Skip to main content
Stephen Marsh
Community Expert
Community Expert
October 28, 2023
Answered

Script to Select ’N' Quantity of Consecutive Layers?

  • October 28, 2023
  • 1 reply
  • 748 views

I have been trying to find a script to select a consecutive range of variable layers, but as this appears to only be possible using esoteric AM code I haven't had any luck hacking something together.

 

So, starting from the currently selected layer, select the next 10 (or 5 or 20 etc) consecutive layers up or down the layer stack.

 

Or starting from the front layer or back layer etc.

 

This is only for top-level layers, it would be even more complex to drill into layer sets.

 

There is also the logic of what if a user selected 10 layers to select, but there were only say 8 above or below?

 

So although I believe that this would be very useful, I can see the possible complexities in this.

This topic has been closed for replies.
Correct answer jugenjury

I have a quickly written script here. The issue comes when you do have layer groups because they are not indexed in order from bottom to top when you expand the group. All the top level layers (and groups) are indexed first, then the next level of groupings are indexed, and so on. So if you have some like.....from bottom to top.....Layer 0, Layer 1, Group 0 (Layer 8, Layer 9, Layer 10), Layer 2, Group 1 (Layer 4, Layer 5, Layer 6), Layer 3 then the document layers array will index them [Layer 0, Layer 1, Group 0, Layer 2, Group 1, Layer 3, Layer 8, Layer 9, Layer 10, Layer 4, Layer 5, Layer 6] so it does get complicated when you try to involve levels of the layer groupings. I can be done, though.

 

function chrToTID(c) {
return app.charIDToTypeID(c);
}
function strToTID(s) {
return app.stringIDToTypeID(s);
}

function mLaySelect() {
lays=prompt("Number of layers to add",10,"Layer Selections");
add=Number(lays)
cid=app.activeDocument.activeLayer.itemIndex; //gets the index number of the current layer.
tid=app.activeDocument.layers.length; //gets the layer length within the document
tdl=cid+add; //calculates the highest desired layer to select.
tsl=Math.min(tdl,tid); //sets the highest layer to either the one desired if possible of the highest layer if not.
el=tid-(cid+add); //gets layers[i] index number so we can get the name of the last layer.
topLayer=app.activeDocument.layers[el]; //top most layer. We need the name for the continuous selection.
$.writeln(topLayer.name+"el="+el+" lays="+lays+" cid="+cid+" tid="+tid+" tdl="+tdl+" tsl="+tsl);
var AD = new ActionDescriptor();
var REF = new ActionReference();
REF.putName(chrToTID('Lyr '), topLayer.name);
AD.putReference(chrToTID('null'), REF);
AD.putEnumerated(strToTID("selectionModifier"), strToTID("selectionModifierType"), strToTID("addToSelectionContinuous"));
AD.putBoolean(chrToTID('MkVs'), false);
var AL = new ActionList();
for (i=cid;i<tsl;i++) {
AL.putInteger(i);
$.writeln("i="+i);
if (i>100) break;
}
AD.putList(chrToTID('LyrI'), AL);
executeAction(chrToTID('slct'), AD, DialogModes.NO);
}

mLaySelect();

1 reply

Inspiring
October 28, 2023

Would this include all types of layers or only specific layer types?

Stephen Marsh
Community Expert
Community Expert
October 28, 2023
quote

Would this include all types of layers or only specific layer types?


By @jugenjury


Good question!

 

It shouldn't matter, but I was thinking about top-level layers. This being any content and not initially limited by the script. If adjustment layers or other layers were in use, then further conditional code could be added or the user would use layer groups where needed.

 

One possible use case is selecting groups of layers for use with Export As from the layers panel, then repeating again etc. until all top level layers were exported. This is because it may be too slow to simply select and export all layers in one batch.

jugenjuryCorrect answer
Inspiring
October 28, 2023

I have a quickly written script here. The issue comes when you do have layer groups because they are not indexed in order from bottom to top when you expand the group. All the top level layers (and groups) are indexed first, then the next level of groupings are indexed, and so on. So if you have some like.....from bottom to top.....Layer 0, Layer 1, Group 0 (Layer 8, Layer 9, Layer 10), Layer 2, Group 1 (Layer 4, Layer 5, Layer 6), Layer 3 then the document layers array will index them [Layer 0, Layer 1, Group 0, Layer 2, Group 1, Layer 3, Layer 8, Layer 9, Layer 10, Layer 4, Layer 5, Layer 6] so it does get complicated when you try to involve levels of the layer groupings. I can be done, though.

 

function chrToTID(c) {
return app.charIDToTypeID(c);
}
function strToTID(s) {
return app.stringIDToTypeID(s);
}

function mLaySelect() {
lays=prompt("Number of layers to add",10,"Layer Selections");
add=Number(lays)
cid=app.activeDocument.activeLayer.itemIndex; //gets the index number of the current layer.
tid=app.activeDocument.layers.length; //gets the layer length within the document
tdl=cid+add; //calculates the highest desired layer to select.
tsl=Math.min(tdl,tid); //sets the highest layer to either the one desired if possible of the highest layer if not.
el=tid-(cid+add); //gets layers[i] index number so we can get the name of the last layer.
topLayer=app.activeDocument.layers[el]; //top most layer. We need the name for the continuous selection.
$.writeln(topLayer.name+"el="+el+" lays="+lays+" cid="+cid+" tid="+tid+" tdl="+tdl+" tsl="+tsl);
var AD = new ActionDescriptor();
var REF = new ActionReference();
REF.putName(chrToTID('Lyr '), topLayer.name);
AD.putReference(chrToTID('null'), REF);
AD.putEnumerated(strToTID("selectionModifier"), strToTID("selectionModifierType"), strToTID("addToSelectionContinuous"));
AD.putBoolean(chrToTID('MkVs'), false);
var AL = new ActionList();
for (i=cid;i<tsl;i++) {
AL.putInteger(i);
$.writeln("i="+i);
if (i>100) break;
}
AD.putList(chrToTID('LyrI'), AL);
executeAction(chrToTID('slct'), AD, DialogModes.NO);
}

mLaySelect();