Need to copy meta data from image and add it to another document

I have a program that creates composite images using cutout images of a person and puts them into layered PSD files during a batch process. The plugin needs to be able to copy the meta data from the cutout images and add it to the composited files. The photographers and print labs use the meta data to track orders.

This was easy to do in JSX.

//JSX - get meta data from the image
var metaData = app.activeDocument.xmpMetadata.rawData;

//JSX - assign meta data to the new document
app.activeDocument.xmpMetadata.rawData = metaData;

In UXP, I don’t see a quick one liner to do this. I figured out in batchPlay that I can get an object containing the meta data. However, I’m not sure how to add this to the new document… or even if it can be done. This is the UXP function to get the meta data which seems to work to get the data.

var metaData = await getMetaData();
console.log(metaData);  

async function getMetaData(){

const result = await require("photoshop").action.batchPlay(
[
   {
      "_obj": "get",
      "_target": [
         {
            "_property": "XMPMetadataAsUTF8"
         },
         {
            "_ref": "document",
            "_enum": "ordinal",
            "_value": "targetEnum"
         }
      ],
      "_options": {
         "dialogOptions": "dontDisplay"
      }
   }
],{
   "synchronousExecution": false,
   "modalBehavior": "fail"
});
    
return result;   
    
}

Maybe this post can give you some inspiration. To set the metadata you probably need to include a library, like xmldom.

The code I wrote is for setting layer metadata though, which is a bit less complicated I assume since I’m omitting the correct XMP format/structure. Layer metadata is just undefined or an empty object (I don’t remember), so I can simply set it to any stringified json.

The document metadata has a fixed structure though, so you should be careful to respect that when setting metadata. It’s definitely possible with the mentioned library though.

Thanks, I’ll look into it. I’m wanting to copy all meta data, not pick and choose specific items. I just want the new composited file to have identical meta data to the cutout image that was used in the composite. It was so easy with JSX, just 2 lines of code. I really hope Adobe plans to add this capability back into UXP.

Can’t you just get and set the metadata then?

Probably. I just wasn’t sure how to configure the batchPlay to set the meta data to the file. I’m still somewhat illiterate with creating batchPlay descriptors. Based of the example from you post I can maybe try that.