Layer selection doesn't work

I need to convert the selected layers into a smart object, but I need them to remain selected.


How do I get the result of “Image C”? Thanks for the help
Here is my code:

core.executeAsModal(async () => {
   activeLayers = app.activeDocument.activeLayers; 
    for (let i = 0; i < activeLayers.length; i++) {
        await app.batchPlay( [{"_obj":"select","_target":[{"_name":activeLayers[i].name,"_ref":"layer"}]}])
        await app.batchPlay( [{"_obj":"newPlacedLayer"}]);
    }
    activeLayers.forEach(layer => layer.selected = true);
});
  1. Save selected layers
  2. Convert one by one
  3. Reselect saved layers

Don’t know how to select multiple layers with DOM API, but check this post how to do it with Batch Play

1 Like

Hi @Karmalakas Thanks for the help.
I tried almost all the selection methods by name and ID that would be more viable, I realized that the problem is that when making a layer an intelligent object, its id changes. One solution I tried was to select by index, but I get an error and the selection does not match the indexes obtained. Where am I going wrong? Follow the video.

Try adding a step to SELECT NO LAYERS in your for loop before you select and turn to smart layer

1 Like

Hi JasonM! Thanks for your tip, but I wasn’t successful.
Selecting by name worked perfectly using batchPlay, my mistake was not saving the “selected layers” before converting to smart object.
Here is the correct code to convert selected layers to smart object while keeping them selected.

activeLayersSel=[];
activeLayers = app.activeDocument.activeLayers; 

activeLayers.forEach(layer  => { 
  activeLayersSel.push(layer.name);
  app.batchPlay( [{"_obj":"select","_target":[{"_name":layer.name,"_ref":"layer"}]}]);
  app.batchPlay( [{"_obj":"newPlacedLayer"}]);
}) ;

activeLayersSel.forEach(asLayer  => { 
  app.batchPlay( [{"_obj":"select","_target":[{"_name":asLayer,"_ref":"layer"}], "selectionModifier":{"_enum":"selectionModifierType","_value":"addToSelection"}}]);
}) ;

I believe you can do each layer conversion in one go:

  app.batchPlay( [
    {"_obj":"select","_target":[{"_name":layer.name,"_ref":"layer"}]},
    {"_obj":"newPlacedLayer"}
  ]);

No point calling BP two times

1 Like

You’re absolutely right, calling BP twice was stupid of me, a comma would do the trick. Thanks for remember. :face_with_peeking_eye:

Actually, to further optimize your code, you should just create a single array of descriptors (including re-selection of layers at the end) and call BP only once in total (instead of 6 in 3 layers case)