2 Replies Latest reply on Mar 27, 2015 2:52 AM by beh_gras_beh

    Loop problem

    beh_gras_beh Level 1

      Dear scripters,

       

      Is it possible to add XML from character styles in the same text frame?

      XML.jpg

       

      I have this but the loop isn't correct.

       

      #target indesign  
        
      myDoc = app.activeDocument;  
      myPageItems = myDoc.allPageItems;  
        
      xmlRoot = myDoc.xmlElements.item("Root");  
        
      createXML();  
        
      function createXML() {  
        
        for(var i=0; i<=myPageItems.length; i++) {  
        
                          var myTextFrame = myPageItems[i];  
                          if(myTextFrame instanceof TextFrame){  
                                    var myCharacters = myTextFrame.characters;  
        
      
      
            for (var j= myCharacters.length - 1; j>=0; j--) {
      
      
                                             
                                              alert("Item: "+ myPageItems[i] +"\n" 
                                                        + "Characters: "+ myCharacters.length +"\n" 
                                                        + "CharacterStyle: "+ myCharacters[i].appliedCharacterStyle.name +"\n" 
                                                        + "Text: "+ myCharacters[j].contents +"\n" 
                                                        + "parent: "+ myCharacters[j].parent +"\n" 
                                                        ); 
                                             
                                              myCharacters[j].select();  
                                              selection = app.selection[0];  
        
      if(myCharacters[j].appliedCharacterStyle.name == "TAG-01"){ xmlRoot.xmlElements.add({markupTag:"TAG-01", xmlContent:selection}); myCharacters[j].parent.markup( xmlRoot.xmlElements.add({markupTag:"TAG-01"}) );} else if(myCharacters[j].appliedCharacterStyle.name == "TAG-02") xmlRoot.xmlElements.add({markupTag:"TAG-02", xmlContent:selection}); 
      
      
      
      
      
      
      
      
                                    }  
                          }  
                }  
      }  
      
        • 1. Re: Loop problem
          Vamitul Level 4

          You are overcomplicating things quite a bit. just use a simple findGrep:

          function createXML() {
            app.findGrepPreferences=null;
            app.findGrepPreferences.appliedCharacterStyle="TAG-01";
            var myFound=myDoc.findGrep(true);
            for (var i=0; i<myFound.length;i++){
              xmlRoot.xmlElements.add({
                markupTag: "TAG-01",
                xmlContent: myFound[i]
              });
            }
            //rinse and repeat fpr "TAG=02"
           }
          
          • 2. Re: Loop problem
            beh_gras_beh Level 1

            What was I thinking?

             

            Thanks a million!