Select layer by similar name -- Script

Community Beginner ,
Jan 10, 2017 Jan 10, 2017

Copy link to clipboard

Copied

Hello, i have a PSD document with multiple layers, i need a script that select all layers with the word "Horizontal" in it´s name, and after that, merge them in one layer.

I am using ExtendScript Toolkit CS6

Screen Shot 2017-01-10 at 10.55.44.png

TOPICS
Actions and scripting

Views

995

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

Engaged , Jan 10, 2017 Jan 10, 2017
Hi Ballestero,Hope this help you...cTID = function(s) {return app.charIDToTypeID(s);};sTID = function(s) {return app.stringIDToTypeID(s);};      if(app.documents.length>0){    var docRef = app.activeDocument;    docRef.activeLayer=docRef.backgroundLayer;    var myLyr = docRef.artLayers;        for (var i = 0; i < myLyr.length; i++) {            if(myLyr.name.indexOf("Layer")!=-1){                addtoSelection(myLyr.name);            }        }}function addtoSelection(lyName){    var desc71 = ne...

Likes

Translate

Translate
Engaged ,
Jan 10, 2017 Jan 10, 2017

Copy link to clipboard

Copied

Hi Ballestero,

Hope this help you...

cTID = function(s) {return app.charIDToTypeID(s);};

sTID = function(s) {return app.stringIDToTypeID(s);};     

if(app.documents.length>0){

    var docRef = app.activeDocument;

    docRef.activeLayer=docRef.backgroundLayer;

    var myLyr = docRef.artLayers;

        for (var i = 0; i < myLyr.length; i++) {

            if(myLyr.name.indexOf("Layer")!=-1){

                addtoSelection(myLyr.name);

            }

        }

}

function addtoSelection(lyName){

    var desc71 = new ActionDescriptor();

    var ref65 = new ActionReference();

    ref65.putName( cTID( "Lyr " ), lyName );

    desc71.putReference( cTID( "null" ), ref65 );

    desc71.putEnumerated( sTID( "selectionModifier" ), sTID( "selectionModifierType" ), sTID( "addToSelection" ));

    desc71.putBoolean( cTID( "MkVs" ), false );

    executeAction( cTID( "slct" ), desc71, DialogModes.NO );

}

-yajiv

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 ,
Jan 11, 2017 Jan 11, 2017

Copy link to clipboard

Copied

Thank you very much, even, using your answer, i finished the code in order to exclude "Background" layer from selection and merge the layers in one single layer named "Horizontal":

Here is the code:

cTID = function(s) {return app.charIDToTypeID(s);}; 
sTID = function(s) {return app.stringIDToTypeID(s);};  
if(app.documents.length>0){ 
    var docRef = app.activeDocument; 
    docRef.activeLayer=docRef.backgroundLayer; 
    var myLyr = docRef.artLayers; 
        for (var i = 0; i < myLyr.length; i++) { 
            if(myLyr.name.indexOf("Vertical")!=-1){ 
                addtoSelection(myLyr.name); 
           
       
function addtoSelection(lyName){ 
    var desc71 = new ActionDescriptor(); 
    var ref65 = new ActionReference(); 
    ref65.putName( cTID( "Lyr " ), lyName ); 
    desc71.putReference( cTID( "null" ), ref65 ); 
    desc71.putEnumerated( sTID( "selectionModifier" ), sTID( "selectionModifierType" ), sTID( "addToSelection" )); 
    desc71.putBoolean( cTID( "MkVs" ), false ); 
    executeAction( cTID( "slct" ), desc71, DialogModes.NO ); 

var idslct = charIDToTypeID( "slct" );

var desc14 = new ActionDescriptor();
var idnull = charIDToTypeID( "null" );
    var ref4 = new ActionReference();
    var idLyr = charIDToTypeID( "Lyr " );
    ref4.putName( idLyr, "Background" );
desc14.putReference( idnull, ref4 );
var idselectionModifier = stringIDToTypeID( "selectionModifier" );
var idselectionModifierType = stringIDToTypeID( "selectionModifierType" );
var idremoveFromSelection = stringIDToTypeID( "removeFromSelection" );
desc14.putEnumerated( idselectionModifier, idselectionModifierType, idremoveFromSelection );
var idMkVs = charIDToTypeID( "MkVs" );
desc14.putBoolean( idMkVs, false );

executeAction( idslct, desc14, DialogModes.NO );

var idMk = charIDToTypeID( "Mk  " );

var desc22 = new ActionDescriptor();
var idnull = charIDToTypeID( "null" );
    var ref7 = new ActionReference();
    var idlayerSection = stringIDToTypeID( "layerSection" );
    ref7.putClass( idlayerSection );
desc22.putReference( idnull, ref7 );
var idFrom = charIDToTypeID( "From" );
    var ref8 = new ActionReference();
    var idLyr = charIDToTypeID( "Lyr " );
    var idOrdn = charIDToTypeID( "Ordn" );
    var idTrgt = charIDToTypeID( "Trgt" );
    ref8.putEnumerated( idLyr, idOrdn, idTrgt );
desc22.putReference( idFrom, ref8 );

executeAction( idMk, desc22, DialogModes.NO );

// =======================================================

var idsetd = charIDToTypeID( "setd" );

var desc23 = new ActionDescriptor();
var idnull = charIDToTypeID( "null" );
    var ref9 = new ActionReference();
    var idLyr = charIDToTypeID( "Lyr " );
    var idOrdn = charIDToTypeID( "Ordn" );
    var idTrgt = charIDToTypeID( "Trgt" );
    ref9.putEnumerated( idLyr, idOrdn, idTrgt );
desc23.putReference( idnull, ref9 );
var idT = charIDToTypeID( "T   " );
    var desc24 = new ActionDescriptor();
    var idNm = charIDToTypeID( "Nm  " );
    desc24.putString( idNm, """Vertical""" );
var idLyr = charIDToTypeID( "Lyr " );
desc23.putObject( idT, idLyr, desc24 );

executeAction( idsetd, desc23, DialogModes.NO );

// =======================================================

var idMrgtwo = charIDToTypeID( "Mrg2" );

executeAction( idMrgtwo, undefined, DialogModes.NO );

///////////////////////////////////

cTID = function(s) {return app.charIDToTypeID(s);}; 
sTID = function(s) {return app.stringIDToTypeID(s);};  
if(app.documents.length>0){ 
    var docRef = app.activeDocument; 
    docRef.activeLayer=docRef.backgroundLayer; 
    var myLyr = docRef.artLayers; 
        for (var i = 0; i < myLyr.length; i++) { 
            if(myLyr.name.indexOf("Horizontal")!=-1){ 
                addtoSelection(myLyr.name); 
           
       
function addtoSelection(lyName){ 
    var desc71 = new ActionDescriptor(); 
    var ref65 = new ActionReference(); 
    ref65.putName( cTID( "Lyr " ), lyName ); 
    desc71.putReference( cTID( "null" ), ref65 ); 
    desc71.putEnumerated( sTID( "selectionModifier" ), sTID( "selectionModifierType" ), sTID( "addToSelection" )); 
    desc71.putBoolean( cTID( "MkVs" ), false ); 
    executeAction( cTID( "slct" ), desc71, DialogModes.NO ); 

var idslct = charIDToTypeID( "slct" );

var desc14 = new ActionDescriptor();
var idnull = charIDToTypeID( "null" );
    var ref4 = new ActionReference();
    var idLyr = charIDToTypeID( "Lyr " );
    ref4.putName( idLyr, "Background" );
desc14.putReference( idnull, ref4 );
var idselectionModifier = stringIDToTypeID( "selectionModifier" );
var idselectionModifierType = stringIDToTypeID( "selectionModifierType" );
var idremoveFromSelection = stringIDToTypeID( "removeFromSelection" );
desc14.putEnumerated( idselectionModifier, idselectionModifierType, idremoveFromSelection );
var idMkVs = charIDToTypeID( "MkVs" );
desc14.putBoolean( idMkVs, false );

executeAction( idslct, desc14, DialogModes.NO );

var idslct = charIDToTypeID( "slct" );

var desc14 = new ActionDescriptor();
var idnull = charIDToTypeID( "null" );
    var ref4 = new ActionReference();
    var idLyr = charIDToTypeID( "Lyr " );
    ref4.putName( idLyr, "Vertical" );
desc14.putReference( idnull, ref4 );
var idselectionModifier = stringIDToTypeID( "selectionModifier" );
var idselectionModifierType = stringIDToTypeID( "selectionModifierType" );
var idremoveFromSelection = stringIDToTypeID( "removeFromSelection" );
desc14.putEnumerated( idselectionModifier, idselectionModifierType, idremoveFromSelection );
var idMkVs = charIDToTypeID( "MkVs" );
desc14.putBoolean( idMkVs, false );

executeAction( idslct, desc14, DialogModes.NO );

var idMk = charIDToTypeID( "Mk  " );

var desc22 = new ActionDescriptor();
var idnull = charIDToTypeID( "null" );
    var ref7 = new ActionReference();
    var idlayerSection = stringIDToTypeID( "layerSection" );
    ref7.putClass( idlayerSection );
desc22.putReference( idnull, ref7 );
var idFrom = charIDToTypeID( "From" );
    var ref8 = new ActionReference();
    var idLyr = charIDToTypeID( "Lyr " );
    var idOrdn = charIDToTypeID( "Ordn" );
    var idTrgt = charIDToTypeID( "Trgt" );
    ref8.putEnumerated( idLyr, idOrdn, idTrgt );
desc22.putReference( idFrom, ref8 );

executeAction( idMk, desc22, DialogModes.NO );

// =======================================================

var idsetd = charIDToTypeID( "setd" );

var desc23 = new ActionDescriptor();
var idnull = charIDToTypeID( "null" );
    var ref9 = new ActionReference();
    var idLyr = charIDToTypeID( "Lyr " );
    var idOrdn = charIDToTypeID( "Ordn" );
    var idTrgt = charIDToTypeID( "Trgt" );
    ref9.putEnumerated( idLyr, idOrdn, idTrgt );
desc23.putReference( idnull, ref9 );
var idT = charIDToTypeID( "T   " );
    var desc24 = new ActionDescriptor();
    var idNm = charIDToTypeID( "Nm  " );
    desc24.putString( idNm, """Horizontal""" );
var idLyr = charIDToTypeID( "Lyr " );
desc23.putObject( idT, idLyr, desc24 );

executeAction( idsetd, desc23, DialogModes.NO );

// =======================================================

var idMrgtwo = charIDToTypeID( "Mrg2" );

executeAction( idMrgtwo, undefined, 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 ,
May 21, 2021 May 21, 2021

Copy link to clipboard

Copied

LATEST

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