Welcome Dialog

Welcome to the Community!

We have a brand new look! Take a tour with us and explore the latest updates on Adobe Support Community.


Photoshop script to select all sub layer-set

Participant ,
Oct 18, 2021 Oct 18, 2021

Copy link to clipboard

Copied

Hi All,

I have code to select the all art layers and layer sets in the psd files, but the code will not select the sublayer sets in the psd file. Is there any direct way to select the sub layersets or through AM code?

var doc = app.activeDocument;   // set active document
var allLayers  = [];
var allLayerset = [];

var allLayers  = collectAllLayers(doc, allLayers); //Consider all layers 
var allLayerset = collectAllLayersets (doc, allLayerset); //Consider all layersets
alert(allLayerset)
function collectAllLayers (doc, allLayers){
    for (var m = 0; m < doc.layers.length; m++){
        var theLayer = doc.layers[m];
        if (theLayer.typename === "ArtLayer"){
            allLayers.push(theLayer);
        }else{
            collectAllLayers(theLayer, allLayers);
        }
    }
    return allLayers;
}
function collectAllLayersets (doc, allLayerset)
{
    for (var z = 0; z < doc.layerSets.length; z++)
    {
        var theLayerset = doc.layerSets[z];
        if (theLayerset.typename === "LayerSet")
        {
            allLayerset.push(theLayerset);
        }
        else
        {
            collectAllLayersets(theLayerset, allLayerset);
        }
    }
    return allLayerset;
}  

 

TOPICS
Actions and scripting, macOS

Views

104

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 ,
Oct 18, 2021 Oct 18, 2021

Copy link to clipboard

Copied

What do you mean by "select"?
As far as I can see your current script is not selecting anything.

 

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 ,
Oct 18, 2021 Oct 18, 2021

Copy link to clipboard

Copied

In the else where you process the layerset before you use the function try adding  push thelayer into the allLayerset Array. delete the collectAllLayersets function.  You made both arrays global you do not have to pass the arrays to the function and the function  not have the return the global allLayers  just  add the allLayerset.push(theLayer);

JJMack

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 ,
Oct 18, 2021 Oct 18, 2021

Copy link to clipboard

Copied

Rocky@, the best way to select layers is to reference their id, i.e. theLayerset.id

Your code only collects layerSets at the top level of the document. If you need to select onle nested layerSets, then change it like this:

 

function collectAllLayersets (doc, allLayerset)
{
    for (var z = 0; z < doc.layerSets.length; z++)
    {
        var theLayerset = doc.layerSets[z];
        if (theLayerset.typename === "LayerSet")
        {
            if (theLayerset.parent.typename != 'Document') allLayerset.push(theLayerset.id);
            collectAllLayersets(theLayerset, allLayerset);
        }
    }
    return allLayerset;
}  

 

If you need all layerSets at all, then remove the condition

 if (theLayerset.parent.typename != 'Document') 

 

Having an array of layerSets id, we can select them with this function:

selectLayerByIDList(allLayerset)

function selectLayerByIDList(IDList) {
    s2t = stringIDToTypeID;
    var r = new ActionReference()
    do { r.putIdentifier(s2t('layer'), IDList.shift()) } while (IDList.length)
    (d = new ActionDescriptor()).putReference(s2t('target'), r)
    executeAction(s2t('select'), d, DialogModes.NO)
}

 

 

 

 

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 ,
Oct 19, 2021 Oct 19, 2021

Copy link to clipboard

Copied

Your script was passing a global array  to the function that parameter is not needed the function can set the global array.  Here is you function setting three arrays and also processing all the artlayers.

// declare Global variables
var layerList = new Array;
var setList = new Array;
var allList = new Array;
var allLayers = ""; 

main();

//for( var i = 0; i < allList.length; i++) {alert(allList[i]);}
//alert("Layerset: " + setList.length + " Layers: "  + layerList.length + " Total: " + allList.length + "\n" +allLayers);
logInfo("Layerset: " + setList.length + " Layers: "  + layerList.length + " Total: " + allList.length + "\n" +allLayers);

//////////////////////////////////////////////////////////////////////////////////////////////
function main() {
	processLayers(activeDocument) 
}
function processLayers(obj) {  
    for( var i = obj.layers.length-1; 0 <= i; i--) {
		if (obj.layers[i].kind!=undefined) {
			layerList.push(obj.layers[i]);
			allList.push(obj.layers[i]);			
			processLayer(obj.layers[i]);
		}
		else {
			setList.push(obj.layers[i]);
			allList.push(obj.layers[i]);			
			processLayers(obj.layers[i]);
		}
	} 
} 
function processLayer(layer) { 
	//alert('Layer Name = "' + layer.name + '" Layer Kind ' + layer.kind );
	allLayers = allLayers + layer.name + " , ID " + layer.id + ", " + layer.kind + "\n";
	//allLayers = allLayers + obj_to_str(layer) + "\n";
}
function obj_to_str(obj){
    var str = ""; 
    for (var p in obj) {	
        if(obj.hasOwnProperty(p)) {
            try{str+=p+"::"+obj[p]+"\n";}
            catch(e){};
        }
    }		
    return str;
}   
function logInfo(Txt){
    try {	
       var file = new File(Folder.desktop + "/LayersBotToTop.txt"); 
       file.open("w", "TEXT", "????"); 
       //alert(file.encoding);
       file.encoding = "UTF8";
       file.seek(0,2);   
       $.os.search(/windows/i)  != -1 ? file.lineFeed = 'windows'  : file.lineFeed = 'macintosh';
       file.writeln(Txt); 
       if (file.error) alert(file.error);
       file.close();
       file.execute(); 
    }
    catch(e) { alert(e + ': on line ' + e.line, 'Script Error', true); }
};

 

JJMack

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 ,
Oct 19, 2021 Oct 19, 2021

Copy link to clipboard

Copied

Thanks Mack!

Is it possible to filter and match the font in the text layer(layerset & artlayer)  and apply the convert to shape?

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 ,
Oct 19, 2021 Oct 19, 2021

Copy link to clipboard

Copied

LATEST

I do not know Javascript and particularly know little at all about Photoshop Action Manager coding. To get information like that you will need to use Action Manager code.  Text is very complex and a single text layer can have many fonts used in it.  I think you should start by search this forum for scripts dealing with editing text layers. I'm sure your not the first one to want the process text layer via Photoshop scripting.

JJMack

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