Bridge script to save XMP file?

General Discussion of Scripting for Adobe Bridge

Moderators: Tom, Kukurykus

NScript

Bridge script to save XMP file?

Post by NScript »

Hi

I am trying to find a way to script Bridge to save the XMP file (Camera Raw adjustment metadata) for a DNG file.

I have tried using the serialize() command on the Metadata object and then xmp.dumpObject(). This gives an output with readable "metadata", but it strips off all the "<" and ">" characters etc. The resulting XMP file is then not recognised by Bridge or Lightroom due to the odd format.

I have also tried using the xmpMetadata.rawData function, Bridge does not seem to recognise this function (Photoshop only?).

Basically I am trying to extract the Camera RAW XMP file for a large number of files.

I would greatly appreciate any suggestions on how to tackle this!

Regards,

NScript
xbytor

Bridge script to save XMP file?

Post by xbytor »

If you can't figure anything else out, take a look at Chapter 10 in the JavaScript Tools Guide (Scripting Access to XMP Metadata).

-X
NScript

Bridge script to save XMP file?

Post by NScript »

Hi Xbytor

Thanks - I had read this chapter previously, but could not see a straightforward way of saving the XMP file. After much experimenting I have finally got something that works.

In case anyone else has the same requirement, this works for me on the PC platform using Bridge CS3.

Code: Select all#target bridge
// ========================================================================
ExtractXMPFile = {};                            // function protoype

ExtractXMPFile.execute = function() {    
   if( xmpLib == undefined ) {
      if( Folder.fs == "Windows" )
         var pathToLib = Folder.startup.fsName + "/AdobeXMPScript.dll";
      else
         var pathToLib = Folder.startup.fsName + "/AdobeXMPScript.framework";
      var libfile = new File( pathToLib );
      var xmpLib = new ExternalObject("lib:" + pathToLib );
   }

   var sels = app.document.selections;               // an array of all the selected thumbs

   for (var i = 0; i < sels.length; i++) {             // for each of the selected thumbs
      var thumb = sels;
      if(thumb.hasMetadata) {      
         var selectedFile = thumb.spec;

         // Create an XMPFile
         var myXmpFile = new XMPFile( selectedFile.fsName, XMPConst.UNKNOWN, XMPConst.OPEN_FOR_READ);
      
         // Get the XMP data
         var thumbXMP = myXmpFile.getXMP();

         // An empty XMP object
         var myXMP = new XMPMeta();

         // Create an iterator that will only visit CAMERA RAW properties
         iter = thumbXMP.iterator(0, XMPConst.NS_CAMERA_RAW);
         while((prop = iter.next()) != null) {
            if(prop.path != "")
               myXMP.setProperty(prop.namespace, prop.path, prop.value, prop.options);
         }

         var myXMPPacket = myXMP.serialize (XMPConst.SERIALIZE_OMIT_PACKET_WRAPPER);

         var OutFileName = thumb.spec.fsName;               // get the full path and name of the file
         OutFileName = OutFileName.substring(0, OutFileName.lastIndexOf("."));      // find the last "." in the filename
         var outFile = new File(OutFileName + ".XMP");      // create a new file with the .xmp extension
         outFile.open('w');                                       // open the file for writing
         outFile.write(myXMPPacket);                           // write the XMP data to the file
         outFile.close();                                          // close the file
      }
   }
}

// ========================================================================
if ( BridgeTalk.appName == "bridge" ) {      // only load into bridge

   if (MenuElement.find ('scripts') == null) {
      var ScriptsMenu = new MenuElement( "menu", "Scripts", "after Help", "scripts");
   }
   var menu = new MenuElement( "command", "Extract XMP File", "at the end of scripts");
   menu.onSelect = ExtractXMPFile.execute;
}



Regards,

NScript