Any way to clear out all metadata from activeDocument?

I have a plugin that needs to copy metadata from a file and replace the metadata in another file with the exact metadata from the first file.

I can do this with batch play and it works. However, if the file that is receiving the metadata already has metadata items then some items that already exist such as camera model, date taken, etc won’t replace the existing data with the new data. Other items such as copyright and description will overwrite with the new data. For any items that don’t have data, it imports the values just fine.

The use case for this is I have a plugin that makes composite images in batch for youth school and sports photographers. The photographers will have cutout images of a player or student that are png or tif images. The software imports these images into PSD templates that contain different backgrounds and graphics. The photographers need the composited image to contain the original data from the cutout file because they are using either timestamps or scanned barcode data in metadata to link the images to students, track orders, etc. They have other systems that look for this metadata after they upload their images into the various systems.

If the PSD template they are using was created from a blank document to start with then the data can be imported OK. However, if the PSD template was created with another image file as a starting point that already contains metadata then the template has that metadata. In those cases, only certain metadata items from the cutout file will overwrite the existing metadata items. I have no control over how the templates are being created as that is done by the photographers or other graphics designers.

So what I want to attempt is to delete all metadata from the composited image before importing the new metadata that was retrieved from the cutout image. I can’t figure out if it is possible to clear al of the existing metadata or even know where to start.

When the cutout image file opens, it uses this function to get the meta data of that file.

async function getMetaData(){
    
    
const batchPlay = require("photoshop").action.batchPlay;

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

After bringing the cutout file into the PSD template file for the composited file, before saving as a new file, the following function is used to set the metadata from the metadata the was retrieved from the cutout file. Again, this works if the metadata items are empty to start with. However, only certain items will overwrite if data already exists.

async function setMetaData(xmpString){
    
    
const batchPlay = require("photoshop").action.batchPlay;

await batchPlay(
[
   {
      "_obj": "set",
      "_target": [
         {
            "_property": "XMPMetadataAsUTF8"
         },
        {
            "_ref": "document",
            "_enum": "ordinal",
            "_value": "targetEnum"
        }
      ],
    to: {
      _obj: "document",
      XMPMetadataAsUTF8: xmpString
    }
   }
],{
   "synchronousExecution": false,
   "modalBehavior": "execute"
}); 
     
    
}

It would be a reasonable specification since camera model, date taken, etc. should not be easily modified by a third party.

There has to be a way to do this.

In this case it makes sense to modify all of the values. These are composited images where an image file of a person person is merged into a new template/background, The photographers need the metadata from composited image to be the exact metadata from the image of the person that they took, not the metadata from the PSD template file. When the image of the person is brought into the template, the metadata stays as the metadata from the template file which doesn’t do the photographers any good.

Also, for some of the systems the photographers are using, they are using synced timestamps where they scan a barcode, take images of a student, then scan the next barcode before taking images of the next student. The timestamps from the barcode scanner go into a system that is then able to match images to the barcode and assign images to each student based on Date Taken.

However, in the case of creating composite images with a PSD template that already has a Date Taken metadata, every image then has the same Date Taken which is incorrect and is the Date Taken from the background image file that was used in the PSD Template creation process.

So this throws a wrench into the entire system.

For School and Sports Photography it is pretty standard now to be doing composited images. A lot of other systems out there that are not Photoshop based system are able to correctly keep the metadata of the composited image to be the metadata of the student image and not the metadata of the background image. There is no reason this should be restricted when working with metadata inside of Photoshop.

I often use ExifTool when I want more metadata editing capabilities than Adobe provides.

ExifTool is great.

However, it is too complicated for most of my users. My users need something very simple. Basically, they just need to be able to push a button, have the plugin make them 1,000s of composites, and have the metadata for the each student or athlete stay with the file. Anything beyond that just creates a ton of confusion on their end and a ton of tech support on my end.

I do have foolproof, but clunky workaround… but it can add significant amount of time to the batch processing. That is to copy and paste the final composited file back on top of the student or athlete image file that each composite belong too. Then saving from that file, it has the metadata for the student/athlete image. This only adds a minimal processing time if they choose to export as a flattened image because I can just have it pull over the final flattened layer. However, if the choose to save as full layered composites, it can add a significant amount of time. Some of my users will use huge PSD, or even PSB large format templates. So to copy all of those layers back into the student image file is a very clunky workaround to just get the metadata.

I have to think this is possible to do with just importing the data though instead of having to go through the clunky workaround steps.

1 Like

There are two solutions: either you create a New empty XMP file before adding the new metadata, but sometimes it does not clear all the fields, I am not sure why! Like this:

const deleteAllXMPMetadata = async () => {
  const xmp = new XMPMeta(); // Create an empty XMPMeta

  // Serialize the empty XMP metadata
  const emptyXmpString = xmp.serialize(XMPConst.SERIALIZE_USE_COMPACT_FORMAT);

  // Set  
  await setMetaData(emptyXmpString);

  console.log("Deleted all XMP metadata" + emptyXmpString);
};

The second solution works 100%:
on the active document:
— Create a new merged visible layer.
— Right-click on the new merged layer and duplicate the layer as a new document.
This new document will have zero metadata associated with it, then you can add any metadata you want.

Thanks.

I will try the first solution and see what happens with it.

The second solution wont work for mine because my plugin has the option to save as a layered PSD, layered PSB, or layered TIF. I didn’t know that duplicating the layer as a new document would lose the metadata. However, duplicating the entire document to keep all of the layer structure does keep the metadata.

I was actually doing something similar by copying the composited document and pasting back into the cutout document so it already has the metadata it needed and nothing needs added. That works too but slows it down for large layered files if they are selecting to save as layered files. Some of the users are using templates that are GBs in size. The templates are already slow to work with and copying all of the layers and pasting into another doc is slow.

Hopefully the first solution will work well enough.