batchPlay does not behave as expeted

I can not find an answer for my problem in the documentation or on internet. I hope I can got help here.
My problem is this. I will duplicate the background layer, then I will create a new layer and fill it with the foreground color. I will also do this for all open documents and here start the problem.
When I stick to the new syntax, everything works perfect. Bus, then you can not fill a layer with the new syntax I have to use batchPlay, and here everything went wrong.
I test with two open files and depending on which file I start with I got two different results.

  1. If I start with the first open file, everything look ok, I got an copy of the background layer and a new layer filled with the forground color. BUT, the second file has the copy and an empty new layer.
  2. This is wired, If I start with the file I open last, I will got a copy and a new layer, buyt all layers is filled with the foreground color. The other file, has a copy of the background and an empty new layer.
    The batchPlay-code is the one in the first example in the documentation, UXP Scripting in Photoshop (adobe.com)

My code is this, the consolo.log is for my debuggning purpose.

async function start(){
console.log('Programet startar');
await require('photoshop').core.executeAsModal(fitToMedia);
console.log('Programmet slutat');
}

async function fitToMedia() {
   const app = window.require("photoshop").app;
    for (const doc of app.documents) {
console.log(`I loopen för ${doc.name}`);
        await createNeededLayers(doc);
 //     await resizeForExport(doc); 
 //     await saveToExport(doc);
console.log(`Lämnar loopen för ${doc.name}`)
   };
console.log(`Exit fitToMedia`);
}

async function createNeededLayers(nDoc) {
console.log(`Kommer in i createNeededLayers för ${nDoc.name}`);
   const al = nDoc.activeLayers[0];
console.log(al.name, nDoc.name);
   const newBL = await nDoc.duplicateLayers([al]);
   const newL = await nDoc.createLayer();
   await require('photoshop').action.batchPlay([{"_obj":"fill", "using":{"_enum":"fillContents","_value":"foregroundColor"}}], {});
   const nal = nDoc.activeLayers[0];

console.log(`Lämnar createNeededLayers för ${nDoc.name}`);
}

Try defining target layer and doc IDs in the BatchPlay command

P. S. It’s quite difficult to read your example with messed up indentation :frowning:

Thanks,
But it does not work, or I have not understand the documentaion, I added _target but then does the function not work.

async function start(){
console.log('Programet startar');
   await require('photoshop').core.executeAsModal(fitToMedia);
console.log('Programmet slutat');
}

async function fitToMedia() {
   const app = window.require("photoshop").app;
    for (const doc of app.documents) {
        await createNeededLayers(doc);
   };
}

async function createNeededLayers(nDoc) {
   const al = nDoc.activeLayers[0];
   const newBL = await nDoc.duplicateLayers([al]);
   const newL = await nDoc.createLayer();
   var target= {"_target": [{"_ref": "layer", "_id": newL.id}, {"_ref": "document", "_id": nDoc.id}]};
   await require('photoshop').action.batchPlay([{"_obj":"fill", "using":{"_enum":"fillContents","_value":"foregroundColor"},
      "_target": [{"_ref": "layer", "_id": newL.id}, {"_ref": "document", "_id": nDoc.id}]}], {});
   const nal = nDoc.activeLayers[0];
}