4 Replies Latest reply on Mar 26, 2015 1:20 PM by beh_gras_beh

    XML from CharacterStyles

    beh_gras_beh Level 1

      Dear Indesign users,

       

      There is an script in this forum to add XML from ParagraphStyles.

      Is this possible with CharacterStyles?

       

      Greetings from Holland

        • 1. Re: XML from CharacterStyles
          Loic.Aigon Adobe Community Professional

          If your script can do it with ParagraphStyle, it will be certainly feasible with characterStyles. Have you a link towards the script code ?

           

          Loic

          http://www.ozalto.com

          • 2. Re: XML from CharacterStyles
            beh_gras_beh Level 1

            Well,

             

            I got this so far, but its's definitely not working at all.

            #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=0; j<myCharacters.length; j++) {  
              // Debug  
              /* 
              alert("Item: "+ myPageItems[i] +"\n" 
                                            + "Characters: "+ myCharacters.length +"\n" 
                                            + "CharacterStyle: "+ myCharacters[j].appliedCharacterStyle.name +"\n" 
                                            + "Text: "+ myCharacters[j].contents +"\n" 
                                            + "parent: "+ myCharacters[j].parent +"\n" 
                                            ); 
                                            //*/  
                                            // EndDebug  
              myCharacters[i].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}); 
                                          }  
                                }  
                      }  
            }  
            

             

            Thank you very much for the help.

            • 3. Re: XML from CharacterStyles
              Loic.Aigon Adobe Community Professional

              Hi,

               

              You should consider using mapStylesTOXMLTag…

               

              var tag = function(tagName) {
                var doc, st, tg;
              
                //Exit if no documents open
                if ( !app.documents.length ) return;
              
                //Referencing the doc
                doc = app.activeDocument;
              
                //Referencing the style
                st = doc.characterStyles.item ( tagName );
                !st.isValid && st  = doc.characterStyles.add ({name:tagName} );
              
                //Referencing the tag
                tg = doc.xmlTags.item (tagName );
                !tg.isValid && tg = doc.xmlTags.add({name:tagName});
              
                //Creating links between styles and xml tags as a xmlExportMap
                em = doc.xmlExportMaps.add(st, tg);
              
                //Apply Tag to styles
                doc.mapStylesToXMLTags();
              }
              
              
              tag("TAG-01");
              

               

              HTH

               

              Loïc

              http://www.ozalto.com

              • 4. Re: XML from CharacterStyles
                beh_gras_beh Level 1

                Stupid me..... Got it working....

                 

                /***********************************************************************/
                /* XML maken */
                /***********************************************************************/
                
                
                #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].appliedCharacterStyle.name == "TAG-01"){ xmlRoot.xmlElements.add({markupTag:"TAG-01", xmlContent:selection}); myParagraphs[j].parent.markup( xmlRoot.xmlElements.add({markupTag:"TAG-01"}) );} else if(myParagraphs[j].appliedCharacterStyle.name == "TAG-02") xmlRoot.xmlElements.add({markupTag:"TAG-02", xmlContent:selection}); 
                                             }  
                                    }  
                          }  
                }