Skip to main content
Known Participant
July 3, 2021
Answered

Help with script that select Layers based on string then move selected layers to specific folder.

  • July 3, 2021
  • 2 replies
  • 3138 views

Hi,

I don't know Java so your help is greatly appreciated!

I want to sort my layers into folders based on naming convention.

 

The closest script I found was this from stackoverflow, but it is missing selecting layers, and moving them.

JavaScript string wildcards for photoshop

 

 

// group layer vegetables
var allLayers = new Array();
var artLayers = new Array();
var theLayers = collectAllLayers(app.activeDocument, 0);


var artLayerNames = "";
// loop over art layers backwards
for (var i = artLayers.length -1; i >= 0  ; i--)
{
  var temp = artLayers[i];
  var regEx = new RegExp(/t/gim);
  if (temp.match(regEx))
  {
    // if the layer contains the letter "t" show it!
    alert(temp);
  }
  artLayerNames+= artLayers[i] + "\n";
}

// print out all layers
alert(artLayerNames);



// function collect all layers
function collectAllLayers (theParent, level)
{
  for (var m = theParent.layers.length - 1; m >= 0; m--)
  {
    var theLayer = theParent.layers[m];
    // apply the function to layersets;
    if (theLayer.typename == "ArtLayer")
    {
      // get the art layers
      artLayers.push(theLayer.name);
    }
    else
    {
      allLayers.push(level + theLayer.name);
      collectAllLayers(theLayer, level + 1)
    }
  }
}

 

 

This topic has been closed for replies.
Correct answer Kukurykus

 

sTT = stringIDToTypeID, obj = {ELEMENTS: [], MASKS: [], LIGHTS: []}
lrs = [].slice.call((aD = activeDocument).artLayers); while(lrs.length)
	(mtch = (shft = lrs.shift()).name.match(/(ELEMENT|MASK|LIGHT)/i))
	&& obj[mtch[1] + 'S'].push(shft.id); while(obj.__count__) {
	if ((itm = obj[frst = obj.reflect.properties[0]]).length) {
		ref1 = new ActionReference(); while(itm.length)
			ref1.putIdentifier(sTT('layer'), itm.shift())

		iI = aD.layerSets[0].layerSets.getByName(frst.name).itemIndex - 1;
		(dsc = new ActionDescriptor()).putReference(sTT('null'), ref1);
		(ref2 = new ActionReference()).putIndex(sTT('layer'), iI)
		dsc.putBoolean(sTT('adjustment'), false)
		dsc.putReference(sTT('to'), ref2)
		executeAction(sTT('move'), dsc)
	}
	delete obj[frst]
}

 

2 replies

Kukurykus
KukurykusCorrect answer
Legend
July 3, 2021

 

sTT = stringIDToTypeID, obj = {ELEMENTS: [], MASKS: [], LIGHTS: []}
lrs = [].slice.call((aD = activeDocument).artLayers); while(lrs.length)
	(mtch = (shft = lrs.shift()).name.match(/(ELEMENT|MASK|LIGHT)/i))
	&& obj[mtch[1] + 'S'].push(shft.id); while(obj.__count__) {
	if ((itm = obj[frst = obj.reflect.properties[0]]).length) {
		ref1 = new ActionReference(); while(itm.length)
			ref1.putIdentifier(sTT('layer'), itm.shift())

		iI = aD.layerSets[0].layerSets.getByName(frst.name).itemIndex - 1;
		(dsc = new ActionDescriptor()).putReference(sTT('null'), ref1);
		(ref2 = new ActionReference()).putIndex(sTT('layer'), iI)
		dsc.putBoolean(sTT('adjustment'), false)
		dsc.putReference(sTT('to'), ref2)
		executeAction(sTT('move'), dsc)
	}
	delete obj[frst]
}

 

Tsu-teyAuthor
Known Participant
July 5, 2021

Hey Kukurykus,

 

I get this error when I try your script:

 

 

Kukurykus
Legend
July 5, 2021

This script is made due to the layer(Sets) structure you showed on the screenshot. There is folder with 4 other subfolders inside, and then next to main folder there are many artLayers.

 

Do you use the same structure of folders and do you have layers column in the level zero, so under IMPORTS folder? I tried it again and it works for me. Please attach your .psd file so it will be easier for me to see if everything is like presented.

Legend
July 3, 2021
The task is not clearly defined. For example, you have a LightMix and Alpha layer. Where to put them? The rest is also not very clear. Folder and layer names don't match very well.
Tsu-teyAuthor
Known Participant
July 3, 2021

Is is only necessary for me to select and move "ELEMENT", "MASK" And "LIGHT".

Legend
July 3, 2021
Now there is no time. If you wait, maybe there will be a script in the evening.