5 Replies Latest reply on Aug 8, 2013 11:36 AM by Alysson_Andrade

    [JS][CS6] ParagraphStyles to XML Tags

    Alysson_Andrade

      Hello!

       

      I need to create a XML structure based on Paragraph Styles of the entire document.

       

       

      I'm trying to create this structure:

      <Root>
      <hlhead>
      <hlhead>Example Paragraph</hlhead>
      <hlbullet>Bullet Paragraph 1</hlbullet>
      <hlbullet>Bullet Paragraph 2</hlbullet>
      So on...
      </hlhead>
      So on...
      </Root>
      

       

      Notes:

      - The tags needs to be different from paragraph style, so I'm reading the name of style to apply/create the tag.

      - The paragraph needs to be linked to tag too, for future XML import.

       

       

      Here's a example code of I'm trying to do:

       

      for(var i=0; i<=myPageItems.length; i++) { // Loop for the all document items
      
           var myTextFrame = myPageItems[i];
           if(myTextFrame instanceof TextFrame){ // Works on textframes only
                 var myParagraphs = myTextFrame.paragraphs; // Get the textframe paragraphs
      
                 for(var j=0; j<myParagraphs.length; j++) { // Loop for textframe paragraphs
      
                     // Debug
                     myParagraphs[j].select();
                     alert("Item: "+ myPageItems[i] +"\n"
                          + "Paragraphs: "+ myParagraphs.length +"\n"
                          + "ParagraphStyle: "+ myParagraphs[j].appliedParagraphStyle.name +"\n"
                          + "Text: "+ myParagraphs[j].contents +"\n"
                          + "xmlElem: "+ xmlElem +"\n"
                          );
                     // EndDebug
      
                     if(myParagraphs[j].appliedParagraphStyle.name == "3A hl head"){ // Check for ParagraphStyle
                          var xmlBody = xmlRoot.xmlElements.add({markupTag:"hlhead"}); // Add the parent element
                          var xmlElem = myParagraphs[j].markup( xmlBody.xmlElements.add({markupTag:"hlhead"}) ); // <- Error
      
                          //xmlElem.contents = myParagraphs[j].contents; // Put the content, but without links... unused
                     }
                     else if(myParagraphs[j].appliedParagraphStyle.name == "3D hl bullet"){
                          myParagraphs[j].markup( xmlBody.xmlElements.add({markupTag:"hlbullet"}) );
                     }
                     // Another else ifs
      
                }
           }
      }
      

       

       

      I'm getting error in this part:

      var xmlElem = myParagraphs[j].markup( xmlBody.xmlElements.add({markupTag:"hlhead"}) );
      

       

      Apparently the error is in "myParagraphs[j].markup" (because it "works" if removed), but the Paragraph Object have the markup method... I guess.

       

       

      What do I need to do?

       

      Can anybody help me on this, please?

       

       

      Thanks!

        • 1. Re: [JS][CS6] ParagraphStyles to XML Tags
          Techi Panda Level 2

          Hi

           

          the workflow is something like this

           

          1. Create your won XML tags via script or import tags

          2. Map your styles to tags to create XML

           

          var myDocument= app.activeDocument;

          myDocument.xmlTags.add("custom_tag");

          myDocument.xmlExportMaps.add(myDocument.paragraphStyles.item('para_style_name'),myDocument .xmlTags.item('custom_tag'));

          myDocument.mapStylesToXMLTags();

           

          Thank you

           

          arul

          http://in.linkedin.com/pub/arul-raja/14/672/681

          1 person found this helpful
          • 2. Re: [JS][CS6] ParagraphStyles to XML Tags
            Vamitul Level 4

            Teci solution is more elegant/efficient etc.

             

            but to fix yours:

            var xmlElem = myParagraphs[j].markup( xmlBody) );

            • 3. Re: [JS][CS6] ParagraphStyles to XML Tags
              Alysson_Andrade Level 1

              @Vamitul

              Hmm, I didn't understand...

              .markup( xmlBody) )  ?

              • 4. Re: [JS][CS6] ParagraphStyles to XML Tags
                Alysson_Andrade Level 1

                Hi Techi,

                 

                Thanks, it's too efficient and elegant as Vamitul said.

                 

                But in my case, I need to make a specific structure, with specific parents, etc...

                 

                 

                I found this topics:

                http://forums.adobe.com/message/2879929#2879929

                http://forums.adobe.com/message/1113890#1113890

                 

                They're saying the paragraph needs to be marked with the parent story/textframe.

                 

                So, I tried this:

                var xmlParent = myParagraphs[j].parent.markup(xmlRoot.xmlElements.add({markupTag:"highlighthead"}));
                var xmlElem = myParagraphs[j].markup(xmlParent.add({markupTag:"highlighthead"}));
                

                 

                But no result...

                 

                What I'm doing wrong?

                • 5. Re: [JS][CS6] ParagraphStyles to XML Tags
                  Alysson_Andrade Level 1

                  Sorry for my... triple post, haha.

                   

                   

                  Yay, I did it!

                   

                  Here's the code for future references:

                   

                  #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 myParagraphs = myTextFrame.paragraphs;
                  
                                                for(var j=0; j<myParagraphs.length; j++) {
                  
                                                          // Debug
                                                          /*
                                                          alert("Item: "+ myPageItems[i] +"\n"
                                                                    + "Paragraphs: "+ myParagraphs.length +"\n"
                                                                    + "ParagraphStyle: "+ myParagraphs[j].appliedParagraphStyle.name +"\n"
                                                                    + "Text: "+ myParagraphs[j].contents +"\n"
                                                                    + "parent: "+ myParagraphs[j].parent +"\n"
                                                                    );
                                                          //*/
                                                          // EndDebug
                  
                                                          myParagraphs[j].select();
                                                          selection = app.selection[0];
                  
                                                          if(myParagraphs[j].appliedParagraphStyle.name == "3A hl head"){
                                                                    xmlRoot.xmlElements.add({markupTag:"hlhead", xmlContent:selection});
                                                                    myParagraphs[j].parent.markup( xmlRoot.xmlElements.add({markupTag:"hlhead"}) );
                                                          }
                                                          else if(myParagraphs[j].appliedParagraphStyle.name == "3D hl bullet")
                                                                    xmlRoot.xmlElements.add({markupTag:"hlbullet", xmlContent:selection});
                                                          else if(myParagraphs[j].appliedParagraphStyle.name == "3D hl bullet(last)")
                                                                    xmlRoot.xmlElements.add({markupTag:"hlbulletlast", xmlContent:selection});
                                                          // So on...
                                                }
                                      }
                            }
                  }
                  

                   

                  Thanks!

                   

                  PS: My first js code!