5 Replies Latest reply on May 6, 2014 3:21 AM by Sebbomatico

    Set XML-Attributes for multiply objects

    Sebbomatico

      Hi everybody,

       

      this is my situation. I have a document with several pages and several objects on each page.

      now I want to get the coordinates and the dimensions of a object and write down the datas as a XML-attribute.

      The script should do this for every object in my document.

       

      That's what i got so far:

      function creatAtt(){

       

          for (var i = 0; i < app.selection.length; i++){

       

          var myObject     = app.selection[i];

          var myXMLobject = myObject.associatedXMLElement;

       

          var ycoords     = myObject.geometricBounds[0];

          var xcoords     = myObject.geometricBounds[1];

          var width         = myObject.geometricBounds[3] - myObject.geometricBounds[1];

          var height         = myObject.geometricBounds[2] - myObject.geometricBounds[0];

       

          myXMLobject.xmlAttributes.add ("Y-Koordinate", ycoords.toString() + " px");

          myXMLobject.xmlAttributes.add ("X-Koordinate", xcoords.toString() + " px");

          myXMLobject.xmlAttributes.add ("Width", width.toString() + " px");

          myXMLobject.xmlAttributes.add ("Height", height.toString() + " px");

       

          }

      }

       

      creatAtt();

       

      When I select a object and run the script it only writes the data for the selected object, not for all objects. Somebody a tip how to fix it???

        • 1. Re: Set XML-Attributes for multiply objects
          Larry G. Schneider Adobe Community Professional & MVP

          Try selecting more than one object.

          • 2. Re: Set XML-Attributes for multiply objects
            csm_phil Level 4

            Hi Sebbomatico,

             

            Please try the below js code, You have to go page by page and after you get respective page elements using the pageItems.

             

            Can you please post your document snapshot. This code its generate the xmlAttributes in first element only.

             

            var myDoc = app.activeDocument;
            var myPages = myDoc.pages;
            for(i=0; i<myPages.length; i++){
                var myActualPage = myPages[i].pageItems;
                for(j=0; j<myActualPage.length; j++){
                    var myObject = myActualPage[j];
                    var myXMLobject = myDoc.xmlElements[0];
                    var ycoords = myObject.geometricBounds[0];
                    var xcoords = myObject.geometricBounds[1];
                    var width = myObject.geometricBounds[3] - myObject.geometricBounds[1];
                    var height = myObject.geometricBounds[2] - myObject.geometricBounds[0];
                    myXMLobject.xmlAttributes.add ("Y-Koordinate", ycoords.toString() + " px");
                    myXMLobject.xmlAttributes.add ("X-Koordinate", xcoords.toString() + " px");
                    myXMLobject.xmlAttributes.add ("Width", width.toString() + " px");
                    myXMLobject.xmlAttributes.add ("Height", height.toString() + " px");
                    }
                }
            
            

             

             

             

            thx,

            csm_phil

            • 3. Re: Set XML-Attributes for multiply objects
              Sebbomatico Level 1

              Hi,

               

              I have tried the script. And it only writes down the attributes for one object. Directly in the root. I have attached a screenshot. But i want to write the attributes in the hotspot Tags of the selected object.

              I hope you know what I mean .

               

              screenshot.jpg

               

              I dont know where he gets the datas bot that are not the datas of one of my three objects.

               

              I changed the scrip a little bit:

               

              var myDoc     = app.activeDocument; 

              var myPages = myDoc.pages;

               

              for (var i = 0; i < myPages.length; i++){

               

                  var myActualPage = myPages[i].pageItems;

               

                  for (var j = 0; j < myActualPage.length; j++){

               

                      var myObject     = myActualPage[j];

                      var myXMLobject = myObject.associatedXMLElement;

               

                      var ycoords     = myObject.geometricBounds[0];

                      var xcoords     = myObject.geometricBounds[1];

                      var width         = myObject.geometricBounds[3] - myObject.geometricBounds[1];

                      var height         = myObject.geometricBounds[2] - myObject.geometricBounds[0];

               

                      myXMLobject.xmlAttributes.add ("Y-Koordinate", ycoords.toString() + " px");

                      myXMLobject.xmlAttributes.add ("X-Koordinate", xcoords.toString() + " px");

                      myXMLobject.xmlAttributes.add ("Width", width.toString() + " px");

                      myXMLobject.xmlAttributes.add ("Height", height.toString() + " px");

                  }

              }

               

              But then I get a error message:

              Errornumber: 21

              null is not an object.

              Row 60 --> He means this part of the script: myXMLobject.xmlAttributes.add ("Y-Koordinate", ycoords.toString() + " px");

              • 4. Re: Re: Set XML-Attributes for multiply objects
                csm_phil Level 4

                Hi Sebbomatico,

                 

                Now, I remove the attributes initially and after I generated. Please check this code and snapshot.

                 

                var myDoc= app.activeDocument; 
                var myPages = myDoc.pages;
                for (var i = 0; i < myPages.length; i++){
                    var myActualPage = myPages[i].pageItems;
                    for (var j = 0; j < myActualPage.length; j++){
                       var myObject= myActualPage[j];
                       var myXMLobject = myObject.associatedXMLElement;
                       var ycoords= myObject.geometricBounds[0];
                       var xcoords= myObject.geometricBounds[1];
                       var width = myObject.geometricBounds[3] - myObject.geometricBounds[1];
                       var height = myObject.geometricBounds[2] - myObject.geometricBounds[0];
                       try{myXMLobject.xmlAttributes.everyItem().remove();}catch(e){};
                       myXMLobject.xmlAttributes.add ("Y-Koordinate", ycoords.toString() + " px");
                       myXMLobject.xmlAttributes.add ("X-Koordinate", xcoords.toString() + " px");
                       myXMLobject.xmlAttributes.add ("Width", width.toString() + " px");
                       myXMLobject.xmlAttributes.add ("Height", height.toString() + " px");
                       }
                    }
                
                

                 

                 

                 

                Untitled.png

                • 5. Re: Re: Set XML-Attributes for multiply objects
                  Sebbomatico Level 1

                  Hi csm_phil,

                   

                  thank you, it woks! your genius! Never hat the idea to use try and catch.

                  Thank you su much for your help!