Can't duplicate document with DOM method nor Alchemist

I tried duplicating the active document with

app.activeDocument.duplicate("Name")

and with batchplay

async function duplicateDoc() {
      let docID = app.activeDocument._id
      let docName = "name"
      await exeModal(async function () {
         await batchPlay(
            [
               {
                  _obj: "newDocument",
                  documentID: docID,
                  _options: {}
               },
               {
                  _obj: "duplicate",
                  _target: [
                     {
                        _ref: "document",
                        _enum: "ordinal",
                        _value: "first"
                     }
                  ],
                  name: docName,
                  documentID: docID,
                  _options: {}
               }
            ],{});
         });   
   }

what i’m getting out of Alchemist does not appear logical to me as i would expect the new document not to have the ID of the source document. But i may be wrong here.
However, when i let batchplay display dialogs, it would throw a “command not available”. A try/catch(e) somehow gives me nothing.
Maybe i’m missing something?

This should work fine:

await batchPlay(
    [
        {
        _obj: "duplicate",
        _target: [
            {
                _ref: "document",
                _enum: "ordinal",
                _value: "first",
            },
        ],
        name: "name"
        }
    ],
    {}
 )

You don’t need to define the document ID.

1 Like

Awesome! Thank you very much. That did the trick!

Btw, when duplicating (or creating etc.) a layer or document the documentID or layerID returned by batchPlay is the new ID assigned to that object.
That can be helpful if your script creates new layers and you want to select everything afterwards, so you can select by IDs and don’t have to mess with indices.

1 Like