Select group of layers by part of a name

Participant ,
Apr 07, 2021 Apr 07, 2021

Copy link to clipboard

Copied

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...)?

TOPICS
Actions and scripting, How to

Views

739

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct Answer

Adobe Community Professional , Apr 07, 2021 Apr 07, 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('selec...

Likes

Translate

Translate
Adobe Community Professional ,
Apr 07, 2021 Apr 07, 2021

Copy link to clipboard

Copied

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? 

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
Apr 07, 2021 Apr 07, 2021

Copy link to clipboard

Copied

// 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); 
}
};

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Apr 07, 2021 Apr 07, 2021

Copy link to clipboard

Copied

I will use it several times during the day.

In fact my needs is to find and select the layer with name PPSTDGRP (it is constant) which is in the TEST group of layers. As I mentioned TEST group has sufix which will be diferent by user.

 

Can you help me how to select the mentioned layer with the script?

 

Thanks in advanced!

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
Apr 07, 2021 Apr 07, 2021

Copy link to clipboard

Copied

You need to amend the line 

if(theName.match(/test/i)) {theArray.push([m, theName])};

to reflect the actual string where »test« stands now. 

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
Apr 07, 2021 Apr 07, 2021

Copy link to clipboard

Copied

 

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)
}

 

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
Apr 07, 2021 Apr 07, 2021

Copy link to clipboard

Copied

Much more elegant! 

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
Apr 07, 2021 Apr 07, 2021

Copy link to clipboard

Copied

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! 🙂

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Apr 08, 2021 Apr 08, 2021

Copy link to clipboard

Copied

Thanks a lot. Just one more thing... group somethimes have special characters TEST_1 or TEST-1 and this script doesnt works when those special characters are between TEST and NUMBERS. Could you improve this?

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
Apr 08, 2021 Apr 08, 2021

Copy link to clipboard

Copied

I inserted [_-]?

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Apr 15, 2021 Apr 15, 2021

Copy link to clipboard

Copied

I encountered a new problem. I wanted to add an alert if there is no matching folder...

 

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)
}
else {
alert("There is no TEST folder");}

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
Apr 15, 2021 Apr 15, 2021

Copy link to clipboard

Copied

Over last character of my code add extra line:

 

	dsc.count - 2 && alert('There is no TEST folder!')

 

and just in case there's only background layer in the document change opening line to:

 

if (test = documents.length && ((lrs = activeDocument.layers).length > 1 || !lrs[0].isBackgroundLayer)) {

 

...or actually for consistency, first 2 lines to:

 

if (test = documents.length && lngth = activeDocument.layerSets.length) {
	sTT = stringIDToTypeID; (ref  = new ActionReference()).putProperty(sTT('property'), json = sTT('json'))

 

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Apr 17, 2021 Apr 17, 2021

Copy link to clipboard

Copied

@Kukurykus you will kill me 🙂 i got an idea, i want instead of alert to create test folder and implement some fuctions in case there is not test folder, but i dont know how to contintinus your script. Please help, i promise i will stop after this 😀

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
Apr 17, 2021 Apr 17, 2021

Copy link to clipboard

Copied

LATEST
if (test = documents.length && (aD = activeDocument).layerSets.length) {
	sTT = stringIDToTypeID; (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 = test
	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)
	dsc.count - 2 && (aD.activeLayer = aD.layers[0], (ref = new ActionReference()).putClass(sTT('layerSection')), (dsc1 = new ActionDescriptor()).putReference(sTT('null'), ref),
	(dsc2 = new ActionDescriptor()).putString(sTT('name'), 'TEST'), dsc1.putObject(sTT('using'), sTT('layerSection'), dsc2), executeAction(sTT('make'), dsc1))
}

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Apr 07, 2021 Apr 07, 2021

Copy link to clipboard

Copied

Running  Adobe Photoshop Version 22.3 on Windows 10. When File > Export > Save for the Web,  is done it errors saying "The operation could not be completed."

Attempted Resolutions:

  1. The preferences were reset ctrl + alt + shift.
  2. The system was rebooted.
  3. A new 1 layer smaller test PSD file was created then it too errors when the above method was attempted.

Please help. Thanks I can't post this problem. Please post for me.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
Apr 07, 2021 Apr 07, 2021

Copy link to clipboard

Copied

You can't create new topic, so you post to unrelated thread, right? Mr. Moderator, do something 😉

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines