Skip to main content
Inspiring
April 7, 2021
Answered

Select group of layers by part of a name

  • April 7, 2021
  • 2 replies
  • 2007 views

Is it possible to select group with a script if i know that name of that group always contains text, for example "TEST" but other part of group name is always changing (TEST1, TEST2, TEST3...)?

This topic has been closed for replies.
Correct answer Kukurykus

 

if (test = documents.length) {
	sTT = stringIDToTypeID, lngth = activeDocument.layerSets.length; (ref  = new ActionReference()).putProperty(sTT('property'), json = sTT('json'))
	ref.putEnumerated(sTT('layer'), sTT('ordinal'), sTT('targetEnum')), (evl = eval('(' + (dsc = executeActionGet(ref)).getString(json) + ')')).layers.length = lngth
	function slct(v) {test = (ref = new ActionReference()).putIdentifier(sTT('layer'), layerSet.id), dsc.putReference(sTT('null'), ref), executeAction(sTT('select'), dsc)}
	(function(v){while(test && v && lngth = v.length) {var lst = lngth - 1; if (/TEST[_-]?\d+/.test((layerSet = v[lst]).name)) return slct(); callee(v[lst].layers), v.length = lst}})(evl.layers)
}

 

2 replies

Kukurykus
KukurykusCorrect answer
Legend
April 7, 2021

 

if (test = documents.length) {
	sTT = stringIDToTypeID, lngth = activeDocument.layerSets.length; (ref  = new ActionReference()).putProperty(sTT('property'), json = sTT('json'))
	ref.putEnumerated(sTT('layer'), sTT('ordinal'), sTT('targetEnum')), (evl = eval('(' + (dsc = executeActionGet(ref)).getString(json) + ')')).layers.length = lngth
	function slct(v) {test = (ref = new ActionReference()).putIdentifier(sTT('layer'), layerSet.id), dsc.putReference(sTT('null'), ref), executeAction(sTT('select'), dsc)}
	(function(v){while(test && v && lngth = v.length) {var lst = lngth - 1; if (/TEST[_-]?\d+/.test((layerSet = v[lst]).name)) return slct(); callee(v[lst].layers), v.length = lst}})(evl.layers)
}

 

c.pfaffenbichler
Community Expert
Community Expert
April 7, 2021

Much more elegant! 

Kukurykus
Legend
April 7, 2021

If you have topic name of Michael_L_Hale and Paul_Riggott they showed their tricks in, you can find changed link to, to reput that to the code in your earlier post 😉 They're restored! 🙂

c.pfaffenbichler
Community Expert
Community Expert
April 7, 2021

It is possible via Script. 

 

How frequent is the task for you? 

Enough so the Layers Panel’s name-Filer is not effivient enough? 

 

How does it need to be integrated in your workflow? 

c.pfaffenbichler
Community Expert
Community Expert
April 7, 2021
// get index of layer/s of specified name and select layers;
// 2021, use it at your own risk;
if (app.documents.length > 0) {
main ();
};
////////////////////////////////////
function main () {
var ref = new ActionReference();
ref.putEnumerated( charIDToTypeID("Dcmn"), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") ); 
var applicationDesc = executeActionGet(ref);
var theNumber = applicationDesc.getInteger(stringIDToTypeID("numberOfLayers"));
var theArray = new Array;
////// work through layers //////
for (var m = theNumber; m >= 0; m--) {
try {
var ref = new ActionReference();
ref.putIndex( charIDToTypeID( "Lyr " ), m);
var layerDesc = executeActionGet(ref);
var layerSet = typeIDToStringID(layerDesc.getEnumerationValue(stringIDToTypeID("layerSection")));
var theName = layerDesc.getString(stringIDToTypeID('name'));
var theID = layerDesc.getInteger(stringIDToTypeID('layerID'));
////////////////////////////////////
// if neither group start or end;
if (layerSet != "layerSectionEnd" /*&& layerSet != "layerSectionStart"*/) {
// if the name matches collect index;
if(theName.match(/test/i)) {theArray.push([m, theName])};
};
////////////////////////////////////
}
catch (e) {};
};
// select the results;
if (theArray.length > 0) {
selectLayerByIndex(theArray[0][0],false);
for (var x = 1; x < theArray.length; x++) {
selectLayerByIndex(theArray[x][0],true)
}
};
};
// by mike hale, via paul riggott;
// http://forums.adobe.com/message/1944754#1944754
function selectLayerByIndex(index,add){ 
add = undefined ? add = false:add 
var ref = new ActionReference();
    ref.putIndex(charIDToTypeID("Lyr "), index);
    var desc = new ActionDescriptor();
    desc.putReference(charIDToTypeID("null"), ref );
       if(add) desc.putEnumerated( stringIDToTypeID( "selectionModifier" ), stringIDToTypeID( "selectionModifierType" ), stringIDToTypeID( "addToSelection" ) ); 
      desc.putBoolean( charIDToTypeID( "MkVs" ), false ); 
   try{
    executeAction(charIDToTypeID("slct"), desc, DialogModes.NO );
}catch(e){
alert(e.message); 
}
};