Document.save(<new entry>) does not work (at least for PNG)

Hello! Despite it’s weekend I really hope someone is available to respond and help him me to overcome/workaround this urgent problem.

I need to save the activeDocument (PSD, which is saved on local disk) as PNG multiple times (I make different layers visible/invisible).

I tried the code below without success:

var output_file = await out_folder.createFile("test.png");
await psd_doc_ref.save(output_file);

Then I read in some ticket that the file should be unsaved, it was clarifed by @kerrishotts and it was suggested to create a new document, then copy the layers into it.
I tried that way but unfortunately it makes problems in my case as smart object contents were not being copied automatically.

I tried to duplicate the document (to create an unsaved doc) with the code below:

const result = await batchPlay(
    [
      {
          "_obj": "duplicate",
          "_target": [
            {
                "_ref": "document",
                "_enum": "ordinal",
                "_value": "first"
            }
          ],
          "documentID": doc_ref._id,
          "_isCommand": true,
          "_options": {
            "dialogOptions": "dontDisplay"
          }
      }
    ],{
      "synchronousExecution": false,
      "modalBehavior": "fail"
    });
}

It seems the duplication works perfect, then I am running this (no success, no error):

while (app.activeDocument._id === psd_doc_ref._id) {
  await sleep(500);
}
await app.activeDocument.save(output_file);

Please help to solve the problem it’s very urgent. @Jarda

P. S. I use PS 22.4.2 Release

Please note that ironically this function works without any problem - PNG file is created, unlike the above mentioned case when I don’t get any error but the file is not saved.

async function save_ai_label_file(label_file, out_folder, file_name) {
  try {
    var ai_doc_ref = await app.open(label_file);
    var output_file = await out_folder.createFile(file_name);
    await ai_doc_ref.save(output_file);
    await ai_doc_ref.closeWithoutSaving();
  } catch (err) {
    await error("Saving label AI file as png failed", err.message.toString());
  }
}

@DavideBarranca @simonhenke

My apologies, actually it works, just my document color mode was CMYK (instead of RGB).

2 Likes