8 Replies Latest reply on Jul 5, 2016 1:13 AM by Aquoibon

    XMP sub folders

    Aquoibon

      Hello,

       

      I could create a folder (container) to XMP File, and add nodes (values) :

       

      var XMPs = app.activeDocument.metadataPreferences; var SPACE = "http://ns.adobe.com/xap/1.0/"; var Container = "FOLDER-1"; XMPs.createContainerItem(SPACE, Container, undefined, ContainerType.BAG); XMPs.setProperty(SPACE,Container+"[1]", "First thing");

       

      But I don't know how to ad a sub-folder (ex: FOLDER-1/Sub-folder-1/) and adding nodes into this sub-folder

      As well for a sub-sub-folder ?

       

      Thank you for your help.

        • 1. Re: XMP sub folders
          Aquoibon Level 1

          Could I know if it possible ?

           

          Thank you.

          • 2. Re: XMP sub folders
            drerol74 Level 2

            Hello,

             

            you can try it with XMPFile Object. Her is an example:

             

            __main();
            
            
            function __main() {
                
                if (!(__loadXMPLibrary())) { return false; }
                
                __insertCustomMetadata();
                __unloadXMPLibrary();
              
                alert("Finished");
            } /* END function __main */
            
            
            
            function __insertCustomMetadata() {
                
                var _file;
                var _xmpFile;
                var _xmpData;
                var _namespace;
                var _propExist;
                
                _file = File.openDialog("Select your InDesign file");
                if(!_file) { return false; }
                
                _xmpFile = new XMPFile(_file.fsName, XMPConst.FILE_INDESIGN, XMPConst.OPEN_FOR_UPDATE);
              
                if(!_xmpFile || _xmpFile.getPacketInfo().length === -1 || _xmpFile.getPacketInfo().writeable === false) {
                alert("Auf die Metadaten der Datei konnte nicht zugegriffen werden.");
                return false;
              }
            
              _xmpData = _xmpFile.getXMP();
                _namespace = "http://ns.adobe.com/xap/1.0/";
                
                _xmpData.setProperty(_namespace,"xmp:Container",null,XMPConst.PROP_IS_STRUCT);
                _xmpData.setProperty(_namespace,"xmp:Container/xmp:Subfolder/xmp:ItemOne","Content");
                
                
                if (_xmpFile.canPutXMP(_xmpData)) {
                _xmpFile.putXMP(_xmpData);
              } else {
                alert("Fehler beim Speichern der XMP-Daten.");
              }
            
              _xmpFile.closeFile(XMPConst.CLOSE_UPDATE_SAFELY);
                    
            } /* END function __insertCustomMetadata */
            
            
            
            function __loadXMPLibrary(){
              if (!ExternalObject.AdobeXMPScript) {
                try{
                  ExternalObject.AdobeXMPScript = new ExternalObject('lib:AdobeXMPScript');
                } catch (e){
                  alert("AdobeXMPScript Library konnte nicht geladen werden.\n\nFehlermeldung:\n" + e); 
                  return false;
                }
              }
            
              return true;
            } /* END function __loadXMPLibrary */
            
            
            
            function __unloadXMPLibrary(){
              if(ExternalObject.AdobeXMPScript) {
                try{
                  ExternalObject.AdobeXMPScript.unload(); 
                  ExternalObject.AdobeXMPScript = undefined;
                } catch(e){
                  alert("AdobeXMPScript Library konnte nicht geschlossen werden.\n\nFehlermeldung:\n" + e);
                }
              }
            
              return;
            } /* END function __unloadXMPLibrary */
            

             

             

            Further information

            https://www.adobe.com/content/dam/Adobe/en/devnet/scripting/pdfs/javascript_tools_guide.pd f

             

            Roland

            • 3. Re: XMP sub folders
              Aquoibon Level 1

              Thanks a lot Roland,

               

              I could now create folders and subfoders, but i use it in a different way :

               

              var XMPs = app.activeDocument.metadataPreferences;
              var EspaceXMP = "http://ns.adobe.com/xap/1.0/";
              var ContainerXMP = "TEST";
              
              XMPs.setProperty(EspaceXMP,"xmp:TEST1/xmp:SousTest1/xmp:PART1","1-AA");
              
              XMPs.setProperty(EspaceXMP,"xmp:TEST1/xmp:SousTest2/xmp:ELEMENT1","2-AA");
              XMPs.setProperty(EspaceXMP,"xmp:TEST1/xmp:SousTest2/xmp:ELEMENT2","2-BB");
              

               

              But it seems i can't used the countContainer() function to count elements of "TEST"or "TEST1" folders or "SousTest1" or "SousTest2" subfolders ...

               

              Any idea ?

              Thank you

              • 4. Re: XMP sub folders
                drerol74 Level 2

                I think this only works with containers:

                 

                XMPs.createContainerItem(EspaceXMP, "xmp:TEST1/xmp:SousTest1", undefined, ContainerType.BAG); 
                XMPs.createContainerItem(EspaceXMP, "xmp:TEST1/xmp:SousTest2", undefined, ContainerType.BAG);
                XMPs.setProperty(EspaceXMP,"xmp:TEST1/xmp:SousTest1[1]","1-AA");
                XMPs.setProperty(EspaceXMP,"xmp:TEST1/xmp:SousTest1[2]","1-BB");
                XMPs.setProperty(EspaceXMP,"xmp:TEST1/xmp:SousTest2[1]","2-AA");
                XMPs.setProperty(EspaceXMP,"xmp:TEST1/xmp:SousTest2[2]","2-BB");
                XMPs.setProperty(EspaceXMP,"xmp:TEST1/xmp:SousTest2[3]","2-CC");
                
                $.writeln(XMPs.countContainer(EspaceXMP,"xmp:TEST1/xmp:SousTest1"));
                $.writeln(XMPs.countContainer(EspaceXMP,"xmp:TEST1/xmp:SousTest2"));
                

                 

                But that's not exactly what you want.

                With XMPMeta object you have more options. For example iterate through the properties:

                 

                ...
                
                _xmpData.setProperty(_namespace,"xmp:Container",null,XMPConst.PROP_IS_STRUCT);
                _xmpData.setProperty(_namespace,"xmp:Container/xmp:Subfolder1/xmp:ItemOne","Content");
                _xmpData.setProperty(_namespace,"xmp:Container/xmp:Subfolder1/xmp:ItemTwo","Content");
                _xmpData.setProperty(_namespace,"xmp:Container/xmp:Subfolder2/xmp:ItemOne","Content");
                _xmpData.setProperty(_namespace,"xmp:Container/xmp:Subfolder2/xmp:ItemTwo","Content");
                _xmpData.setProperty(_namespace,"xmp:Container/xmp:Subfolder2/xmp:ItemThree","Content");
                
                $.writeln(__countProp(_xmpData, _namespace, "xmp:Container/xmp:Subfolder2"));
                    
                
                function __countProp(_xmpObj, _ns, _propName) {
                  
                    var _propIterator = _xmpObj.iterator(XMPConst.ITERATOR_JUST_CHILDREN, _ns, _propName);  
                    var _nextProp = _propIterator.next();
                    var _numOfProps = 0;
                
                    while(_nextProp) {
                        _numOfProps += 1;
                        _nextProp = _propIterator.next();
                    }
                
                    return _numOfProps;
                } /* END function __countProp */
                
                ...
                

                 

                Roland

                • 5. Re: XMP sub folders
                  Aquoibon Level 1

                  Thanks Roland, i'll try your 2 solutions

                   

                  Alban

                  • 6. Re: XMP sub folders
                    Aquoibon Level 1

                    Another question (sorry) :

                     

                    Why in the code below the GO function works well when i click the "OK" button and works half when i click the "GO" button ...

                    I'd like to make it working well by the GO button … of course !

                     

                    Thank you

                     

                    var XMPs = app.activeDocument.metadataPreferences;
                    var EspaceXMP = "http://ns.adobe.com/xap/1.0/";
                    var ContainerXMP = "TEST";
                    
                    XMPs.createContainerItem(EspaceXMP, ContainerXMP, undefined, ContainerType.BAG);
                    XMPs.setProperty(EspaceXMP,ContainerXMP[1], "Ceci est le contenu");
                    
                    function Fenetre(){
                      var FENETRE = new Window("dialog", "");
                      var BTNS = FENETRE.add("group");
                      btnGO = BTNS.add("button", undefined, "GO");
                      btnANNULE = BTNS.add("button", undefined, "Cancel", {name:"cancel"});
                      btnOK = BTNS.add("button", undefined, "OK", {name:"ok"});
                    
                      btnGO.onClick = function(){
                      GO();
                      }
                    
                      btnOK.onClick = function(){
                      FENETRE.close(1);
                      }
                    
                      if (FENETRE.show()==1){
                      GO();
                      }
                    }
                    
                    function GO(){
                      alert("go")
                      alert(XMPs.getProperty(EspaceXMP,ContainerXMP[1]));
                    }
                    
                    Fenetre();
                    

                     

                    And it's not better if I do this :

                    btnGO.onClick = function(){
                      alert(XMPs.getProperty(EspaceXMP,ContainerXMP[1]));
                    }
                    
                    • 7. Re: XMP sub folders
                      drerol74 Level 2

                      It does not work, because the modal dialog (fenetre) is still open. Use »palette« instead of »dialog« + #targetengine

                       

                      Roland

                      • 8. Re: XMP sub folders
                        Aquoibon Level 1

                        Merci ….