Skip to main content
Chuck Uebele
Community Expert
Community Expert
October 11, 2019
Answered

Script to select multiple layers

  • October 11, 2019
  • 5 replies
  • 5808 views

I used to have a script that would select multiple layers by ID, but can't find it, and the old forum post are gone. Does anyone have a script that will do that? Thanks

This topic has been closed for replies.
Correct answer Chuck Uebele

Thanks, everyone. I actually finally found what I was looking for. It takes an array of layer ids and selects the layer.:

function doesIdExists( id ){// function to check if the id exists
   var res = true;
   var ref = new ActionReference();
   ref.putIdentifier(charIDToTypeID('Lyr '), id);
    try{var desc = executeActionGet(ref)}catch(err){res = false};
    return res;
}

function multiSelectByIDs(ids) {
  if( ids.constructor != Array ) ids = [ ids ];
    var layers = new Array();
    var id54 = charIDToTypeID( "slct" );
    var desc12 = new ActionDescriptor();
    var id55 = charIDToTypeID( "null" );
    var ref9 = new ActionReference();
    for (var i = 0; i < ids.length; i++) {
       if(doesIdExists(ids[i]) == true){// a check to see if the id stil exists
           layers[i] = charIDToTypeID( "Lyr " );
           ref9.putIdentifier(layers[i], ids[i]);
       }
    }
    desc12.putReference( id55, ref9 );
    var id58 = charIDToTypeID( "MkVs" );
    desc12.putBoolean( id58, false );
    executeAction( id54, desc12, DialogModes.NO );
}

.

5 replies

Chuck Uebele
Community Expert
Chuck UebeleCommunity ExpertAuthorCorrect answer
Community Expert
October 11, 2019

Thanks, everyone. I actually finally found what I was looking for. It takes an array of layer ids and selects the layer.:

function doesIdExists( id ){// function to check if the id exists
   var res = true;
   var ref = new ActionReference();
   ref.putIdentifier(charIDToTypeID('Lyr '), id);
    try{var desc = executeActionGet(ref)}catch(err){res = false};
    return res;
}

function multiSelectByIDs(ids) {
  if( ids.constructor != Array ) ids = [ ids ];
    var layers = new Array();
    var id54 = charIDToTypeID( "slct" );
    var desc12 = new ActionDescriptor();
    var id55 = charIDToTypeID( "null" );
    var ref9 = new ActionReference();
    for (var i = 0; i < ids.length; i++) {
       if(doesIdExists(ids[i]) == true){// a check to see if the id stil exists
           layers[i] = charIDToTypeID( "Lyr " );
           ref9.putIdentifier(layers[i], ids[i]);
       }
    }
    desc12.putReference( id55, ref9 );
    var id58 = charIDToTypeID( "MkVs" );
    desc12.putBoolean( id58, false );
    executeAction( id54, desc12, DialogModes.NO );
}

.

c.pfaffenbichler
Community Expert
Community Expert
October 11, 2019

////// based on code by mike hale and paul riggott //////

function selectLayerByID(index,add){ 

add = undefined ? add = false:add 

var ref = new ActionReference();

    ref.putIdentifier(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); 

}

};

Geppetto Luis
Legend
October 11, 2019

See if this is good for you

the code was entered by r-bin

 

select_layer("layer 2");

select_layer("layer 1", true);

function select_layer(nm, add)

    {   

    try {

        var r = new ActionReference();

        r.putName(stringIDToTypeID("layer"), nm);

        var d = new ActionDescriptor();

        d.putReference(stringIDToTypeID("null"), r);

        if (add == true) d.putEnumerated(stringIDToTypeID("selectionModifier"), stringIDToTypeID("selectionModifierType"), stringIDToTypeID("addToSelection"));

        executeAction(stringIDToTypeID("select"), d, DialogModes.NO);

        }

    catch (e) { alert(e); throw(e); }

    }

 

rob day
Community Expert
Community Expert
October 11, 2019

I thought by ID you meant InDesign, sorry ignore my post.

Chuck Uebele
Community Expert
Community Expert
October 11, 2019
Thanks anyway!
rob day
Community Expert
Community Expert
October 11, 2019