• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

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

Participant ,
Jul 03, 2021 Jul 03, 2021

Copy link to clipboard

Copied

Hi,

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

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

Tsutey_1-1625316386153.png

 

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

 

 

TOPICS
Actions and scripting

Views

1.9K

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 2 Correct answers

People's Champ , Jul 03, 2021 Jul 03, 2021
var r_idx = new ActionReference();
var do_it = 0;

var text = prompt("Layer name:", "", "Input");

if (text != null)
    {
        var d = new ActionDescriptor();
        var r = new ActionReference();
        r.putEnumerated(stringIDToTypeID("layer"), stringIDToTypeID("ordinal"), stringIDToTypeID("targetEnum"));
        d.putReference(stringIDToTypeID("null"), r);
        executeAction(stringIDToTypeID("selectNoLayers"), d, DialogModes.NO);

    for (var i = 0; i < activeDocument.artLayers.leng
...

Votes

Translate

Translate
LEGEND , Jul 03, 2021 Jul 03, 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;
...

Votes

Translate

Translate
Adobe
People's Champ ,
Jul 03, 2021 Jul 03, 2021

Copy link to clipboard

Copied

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.

Votes

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 ,
Jul 03, 2021 Jul 03, 2021

Copy link to clipboard

Copied

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

Votes

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
People's Champ ,
Jul 03, 2021 Jul 03, 2021

Copy link to clipboard

Copied

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

Votes

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 ,
Jul 03, 2021 Jul 03, 2021

Copy link to clipboard

Copied

If it is complicated, just selecting the layer is good enough.

Votes

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
People's Champ ,
Jul 03, 2021 Jul 03, 2021

Copy link to clipboard

Copied

var r_idx = new ActionReference();
var do_it = 0;

var text = prompt("Layer name:", "", "Input");

if (text != null)
    {
        var d = new ActionDescriptor();
        var r = new ActionReference();
        r.putEnumerated(stringIDToTypeID("layer"), stringIDToTypeID("ordinal"), stringIDToTypeID("targetEnum"));
        d.putReference(stringIDToTypeID("null"), r);
        executeAction(stringIDToTypeID("selectNoLayers"), d, DialogModes.NO);

    for (var i = 0; i < activeDocument.artLayers.length; i++) 
        if (activeDocument.artLayers[i].name.indexOf(text) >= 0) { r_idx.putIdentifier(stringIDToTypeID("layer"), activeDocument.artLayers[i].id); ++do_it; }

    if (do_it)
        {
        var d = new ActionDescriptor();
        d.putReference(stringIDToTypeID("null"), r_idx);
        executeAction(stringIDToTypeID("select"), d, DialogModes.NO);
        }

    alert(do_it + " layers were selected");
    }

Votes

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 ,
Jul 03, 2021 Jul 03, 2021

Copy link to clipboard

Copied

You are amazing! Thank you 🙂

Votes

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 ,
Jul 05, 2021 Jul 05, 2021

Copy link to clipboard

Copied

If a layer is nested inside groups, is it possible to find it by name?

Votes

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 ,
Jul 05, 2021 Jul 05, 2021

Copy link to clipboard

Copied

Example:

Tsutey_0-1625526918014.png

 

Votes

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
LEGEND ,
Jul 05, 2021 Jul 05, 2021

Copy link to clipboard

Copied

 

if (activeDocument.activeLayer.typename == 'LayerSet') {
	lrs=[].slice.call(activeDocument.activeLayer.artLayers); while(lrs.length)
		(shft = lrs.shift()) && /PASS$/.test(nme = shft.name) && alert(nme)
}

 

btw is this in relation to this thread or this is independent issue?

Votes

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 ,
Jul 06, 2021 Jul 06, 2021

Copy link to clipboard

Copied

relation to this thread, r-bins script selects only layers outside groups. It would be nice if it selected layers inside groups aswell.

Votes

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 ,
Jul 06, 2021 Jul 06, 2021

Copy link to clipboard

Copied

How can is use this Kukurykus?

Is it possible to modifiy r-bins script to incorporate layer within groups aswell?

Votes

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
LEGEND ,
Jul 06, 2021 Jul 06, 2021

Copy link to clipboard

Copied

Let him adapt his script to. You can select folder and use a code for names ending with 'PASS':

 

function selectionOfLayersEndingWithPASS() {
	sTT = stringIDToTypeID, ref = new ActionReference(); while(arr.length)
	ref.putIdentifier(sTT('layer'),arr.shift());(dsc=new ActionDescriptor())
	.putReference(sTT('null'), ref), executeAction(sTT('select'), dsc)
}

if (activeDocument.activeLayer.typename == 'LayerSet') {
	arr = [], lrs = [].slice.call(activeDocument.activeLayer.artLayers);
	while(lrs.length) (shft = lrs.shift()) && /PASS$/.test(nme = shft.name)
	&& arr.push(shft.id); arr.length && selectionOfLayersEndingWithPASS()
}

 

Votes

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 ,
Jul 06, 2021 Jul 06, 2021

Copy link to clipboard

Copied

Selects only the first layer ending with PASS. And only if it is in one group.

 

Tsutey_2-1625595843487.png

Tsutey_3-1625595878473.png

 

 

 

Votes

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
LEGEND ,
Jul 06, 2021 Jul 06, 2021

Copy link to clipboard

Copied

That's right, exacly like in posted earlier image.

Votes

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 ,
Jul 07, 2021 Jul 07, 2021

Copy link to clipboard

Copied

Ah, sorry for being unclear. Should I start a new thread for this?

Votes

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
LEGEND ,
Jul 07, 2021 Jul 07, 2021

Copy link to clipboard

Copied

LATEST

To make it works at least one layer(Set) must be selected:

 

arr = []; (function(v) {
	var lrs = [].slice.call(v); while(lrs.length) {
		if ((shft = lrs.shift()).typename == 'LayerSet')
			callee(shft.layers) else /PASS$/.test(shft.name) && arr.push(shft.id)
	}
})((aD = activeDocument).layerSets), sTT = stringIDToTypeID, arr.length && (function(){
	runMenuItem(sTT('selectNoLayers')), ref = new ActionReference(); while(arr.length)
		ref.putIdentifier(sTT('layer'), arr.shift()); (dsc = new ActionDescriptor())
	.putReference(sTT('null'), ref), executeAction(sTT('select'), dsc)
})()

 

Votes

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
LEGEND ,
Jul 03, 2021 Jul 03, 2021

Copy link to clipboard

Copied

 

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

 

Votes

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 ,
Jul 05, 2021 Jul 05, 2021

Copy link to clipboard

Copied

Hey Kukurykus,

 

I get this error when I try your script:

 

Tsutey_0-1625521378003.png

 

Votes

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
LEGEND ,
Jul 05, 2021 Jul 05, 2021

Copy link to clipboard

Copied

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.

Votes

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 ,
Jul 05, 2021 Jul 05, 2021

Copy link to clipboard

Copied

Ah, I forgot the IMPORTS folder!

Votes

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