7 Replies Latest reply on Jul 2, 2015 12:13 AM by Laubender

    XML elements with scriptlabel

    beh_gras_beh Level 1

      Dear Scripters,

       

      I'm adding XML with a selection to an container with scriptlabel (CS5) but the next frame with another label giving me the error
      "this content is already tagged".

       

      What am I doing wrong?

       

       

      var mydocument = app.activeDocument
      var myXMLroot = mydocument.xmlElements.item(0);  // The root always exists
      var b = myXMLroot.xmlElements.add("FirstXMLObject");
      
      
      
      
      selection = app.selection[0];  
      
      
      for (var i=0; i<document.allPageItems.length; i++)
      {
      
      
      var pageItem = mydocument.allPageItems[i];
      if (pageItem.label == "test")
      
      
      { 
      b.xmlElements.add({markupTag:"test", xmlContent:selection});
      }
      
      
      else if (pageItem.label == "test1")
      b.xmlElements.add({markupTag:"test1", xmlContent:selection});
      }
      
      

       

       

       

       

       

       

      Greetings.

        • 1. Re: XML elements with scriptlabel
          BSKTCreation Level 2

          Hi,

           

          I wrote this a while back and I think it does what you want. Give it a go.

           

          // Because this is using the scrip label we need to force the DOM version to 6.0
          app.scriptPreferences.version = 6.0;
          var myDoc = app.activeDocument;
          for (var i = 0; i < myDoc.pageItems.length; i++) {
              try {
                  if (myDoc.pageItems.item(i).associatedXMLElement.markupTag.name != myDoc.pageItems.item(i).label) {
                      myDoc.pageItems.item(i).associatedXMLElement.markupTag.name = myDoc.pageItems.item(i).label;
                      }
                  }
              catch (error) {}
              }
          app.scriptPreferences.version = 10.064;
          

           

          The script looks at all page items and changes the tag name to match. However, it does have a shortfall if you are using tags on more than one page item and the page items are named different. It won't crash but it will use the last instance of the item name so you'll end up with different tag names for the previous items in those cases.

           

          Brett

          • 2. Re: XML elements with scriptlabel
            beh_gras_beh Level 1

            Hi Brett,

             

            That's also an possibility only I have test and  test1 and more script labels. This script names al the tags as test.

             

            Thank you though for the reply, I'm going to search further!

            Have this now but now he is doing very strange with my selection.

             

             

            var mydocument = app.activeDocument  
            var myXMLroot = mydocument.xmlElements.item(0);  // The root always exists  
            var b = myXMLroot.xmlElements.add("FirstXMLObject");  
              
              
              
              
            for (var i=0; i<app.selection.length; i++)  
            {  
              
              
            var pageItem = mydocument.allPageItems[i];  
            if (pageItem.label == "test")  
            { 
            var c = b.xmlElements.add({markupTag:"test", xmlContent:pageItem});
            }  
              
              
            else if (pageItem.label == "test1")  
            var d = b.xmlElements.add({markupTag:"test1", xmlContent:pageItem});  
            }  
            
            • 3. Re: XML elements with scriptlabel
              Vamitul Level 4
              var mydocument = app.activeDocument;
              var myXMLroot = mydocument.xmlElements.item(0); // The root always exists    
              var b = myXMLroot.xmlElements.add("FirstXMLObject");
              
              
              //allItems returns pages items nested inside groups, and those afaik can't be tagged.
              var pageItems=mydocument.pageItems.everyItem().getElements(); 
              
              
              for (var i = 0; i < pageItems.length; i++) {
              
              
              
              
                var pageItem = pageItems[i];
                if (pageItem.label == "test") {
                var c = b.xmlElements.add({
                markupTag: "test",
                xmlContent: pageItem
                });
                } else if (pageItem.label == "test1")
                var d = b.xmlElements.add({
                markupTag: "test1",
                xmlContent: pageItem
                });
              }
              
              • 4. Re: XML elements with scriptlabel
                beh_gras_beh Level 1

                So much to learn, thanks Vamitul.

                Is it possible that its only adding tags on an selection and not the whole page?

                • 5. Re: XML elements with scriptlabel
                  Vamitul Level 4
                  var mydocument = app.activeDocument;
                  var myXMLroot = mydocument.xmlElements.item(0); // The root always exists      
                  var b = myXMLroot.xmlElements.add("FirstXMLObject");
                  var pageItems = app.selection;
                  for (var i = 0; i < pageItems.length; i++) {
                    var pageItem = pageItems[i];
                    if (!pageItem.hasOwnProperty('label')){ 
                    //this assumes the user knows what he is doing, 
                    //and the labels are correctly applied to elements that can be tagged
                      continue;
                    }
                    if (pageItem.label == "test") {
                      var c = b.xmlElements.add({
                        markupTag: "test",
                        xmlContent: pageItem
                      });
                    } else if (pageItem.label == "test1")
                      var d = b.xmlElements.add({
                        markupTag: "test1",
                        xmlContent: pageItem
                      });
                  }
                  
                  • 6. Re: XML elements with scriptlabel
                    beh_gras_beh Level 1

                    Thanks Vamitul,

                     

                    Very helpful to, didn't know this one: pageItem.hasOwnProperty('label')

                     

                    Greetings

                    • 7. Re: XML elements with scriptlabel
                      Laubender Adobe Community Professional & MVP

                      Hm. I wonder in what case pageItem.hasOwnProperty("label") would return false.

                      Ah. It's the variable with that name, not the object. If the var contains a text object, then hasOwnProperty("label") would return false…

                       

                      Uwe