Copy/Pate to active layer

I have this script that open file, copy first layer and paste to first document, the problem is that photoshop create new layer and dont paste to active layer:

await core.executeAsModal(async () => {
      var targetDoc = app.activeDocument;
      var sourceDoc = await app.open(file);
      await sourceDoc.layers[0].copy();
      app.activeDocument = targetDoc;
      var actl = await targetDoc.activeLayers.get();
      console.log(actl);
      targetDoc.paste();
      sourceDoc.close();
      app.activeDocument = targetDoc;
  });

I have found solution, before copy layer do selection:

await core.executeAsModal(async () => {
    let targetDoc = app.activeDocument;
    let sourceDoc = await app.open(file);
    await action.batchPlay(
        [
            {
                "_obj": "set",
                "_target": [
                    {
                        "_ref": "channel",
                        "_property": "selection"
                    }
                ],
                "to": {
                    "_enum": "ordinal",
                    "_value": "allEnum"
                },
                "_options": {
                    "dialogOptions": "dontDisplay"
                }
            }
        ], 
        {
            "synchronousExecution": true
        }
    );
    await sourceDoc.layers[0].copy();
    app.activeDocument = targetDoc;
    await targetDoc.paste();
    sourceDoc.close();
});
1 Like