3 Replies Latest reply on Feb 13, 2011 10:52 AM by Paul Riggott

    Adding/Reading XMP Metadata in a psd file

    Julien_BOLBACH Level 1

      Hi,

       

      I'm having some troubles to understand how to add and read XMP metadata on a psd file. I followed the Panel developer guide tutorial, and try to look on the internet for some answers, but I'm still stuck ...

       

      Here is the simple example I'm trying to make (using part of the code from the panel developer guide) :

       

      /**
           The function loads the XMP Script Library.
           @returns True if the XMP Script Library was loaded successfully.
           @type Boolean
      */
      function loadXMPLibrary(){
           if ( !ExternalObject.AdobeXMPScript ){
                try{
                     ExternalObject.AdobeXMPScript = new ExternalObject('lib:AdobeXMPScript');
                }catch (e){
                     alert("Can't load XMP Script Library");
                     return false;
                }
           }
           return true;
      }
      
      /**
           The function unloads the XMP Script Library.
      */
      function unloadXMPLibrary(){
           if( ExternalObject.AdobeXMPScript ) {
                try{
                     ExternalObject.AdobeXMPScript.unload();
                     ExternalObject.AdobeXMPScript = undefined;
                }catch (e){
                     alert("Can't unload XMP Script Library");
                }
           }
      }
      
      
      /**
          Try to put a new property into the metadata
      */
      
      if( app.activeDocument || !loadXMPLibrary()){
      
          var xmp = new XMPMeta(activeDocument.xmpMetadata.rawData);
          xmp.setProperty( XMPConst.NS_EXIF, "myProperty", "test" );
          activeDocument.xmpMetadata.rawData = xmp.serialize();
          unloadXMPLibrary();
          Window.alert(activeDocument.xmpMetadata.rawData);
      }
      
      

       

      I don't really know where I'm wrong, could you help me please ?

       

      Thanks,

       

      Julien

        • 1. Re: Adding/Reading XMP Metadata in a psd file
          Michael L Hale Level 5

          Document.xmpMetadata is read only. You can edit some metadata using Document.info. Otherwise you have to edit/add the metadata while the file is not open in Photoshop.

          • 2. Re: Adding/Reading XMP Metadata in a psd file
            Julien_BOLBACH Level 1

            If I use the appendArrayItem, it seems to work :

             

             

            if( app.activeDocument || !loadXMPLibrary()){
            
                var xmp = new XMPMeta(activeDocument.xmpMetadata.rawData);
                xmp.appendArrayItem(XMPConst.NS_DC, "myProperty", "test", 0,XMPConst.ARRAY_IS_ORDERED)
                activeDocument.xmpMetadata.rawData = xmp.serialize();
                unloadXMPLibrary();
                Window.alert(activeDocument.xmpMetadata.rawData);
            }
             
            

             

             

            To be more specific, I would be able to add a XML structure into the XMP metadata of the psd file, and be able to retrieve it "easily". Is it possible ?

             

            Thanks,

             

            Julien

            • 3. Re: Adding/Reading XMP Metadata in a psd file
              Paul Riggott Level 6

              Rather mess with EXIF and Dublin Core namespaces, wouldn't it be better to create your own?

              IE:

               

              #target photoshop
              addtoMeta();
              function addtoMeta(){
              if(!documents.length) return;
              if (ExternalObject.AdobeXMPScript == undefined) ExternalObject.AdobeXMPScript = new ExternalObject("lib:AdobeXMPScript");
              var xmp = new XMPMeta( activeDocument.xmpMetadata.rawData);
              var myNamespace = "http://my.fantastic.newspace/";
              var myPrefix = "nsfns:";
              XMPMeta.registerNamespace(myNamespace, myPrefix);
              xmp.setProperty(myNamespace, "myProperty", "A Test String");
              app.activeDocument.xmpMetadata.rawData = xmp.serialize();
              }