4 Replies Latest reply on Jul 3, 2014 1:17 AM by hasvi

    Apply character style for particular word only by script

    hasvi Level 1

      Hi,

       

      I have to find the particular word in indesign file. That has to change italic by character style apply.

      For instance my heading levels 'Ind-H1', 'Ind-H2', 'Ind-H3' have '(continued)' text, now my continued text only get character style 'Italic' for declared heading levels.Is this possible by script?

       

      by

      hasvi

        • 1. Re: Apply character style for particular word only by script
          Kai Rübsamen Adobe Community Professional

          Try this one and correct before your stylenames:

           

          var curDoc = app.activeDocument;
          
          app.findTextPreferences = app.changeTextPreferences = null;   
              
          app.findTextPreferences.findWhat = "continue";   
          var allFounds = curDoc.findText();   
          
          app.findTextPreferences = app.changeTextPreferences = null;
              
          for( var n = 0; n < allFounds.length; n++ ) {   
              var curFound = allFounds[n];
              var curPara = curFound.paragraphs[0];
              if ( curPara.appliedParagraphStyle.name == "h1" || curPara.appliedParagraphStyle.name == "h2" ||curPara.appliedParagraphStyle.name == "h3" ) {
                  curFound.appliedCharacterStyle = "italic";
              }
          } 
              
          
          
          • 2. Re: Apply character style for particular word only by script
            hasvi Level 1

            Hi Kei,

             

            Working nice, but my text may be fall in another file "see: (continued)". So I have to change this as like below:

            1.png

            Is this possible by script?

             

            by

            hasvi

            • 3. Re: Re: Apply character style for particular word only by script
              Kai Rübsamen Adobe Community Professional

              Maybe this can be written shorter. I have two ideas:

               

              1.

              var curDoc = app.activeDocument;  
                
              app.findGrepPreferences = app.changeTextPreferences = null;     
                    
              app.findGrepPreferences.findWhat = "see(?=:\\x20\\(continued\\))";     
              var firstFound = curDoc.findGrep();
              app.findGrepPreferences.findWhat = "(?<=see:\\x20\\()continued";
              var secFound = curDoc.findGrep();
              var allFounds = firstFound.concat(secFound);
                      
              for( var n = 0; n < allFounds.length; n++ ) {     
                  var curFound = allFounds[n];  
                  var curPara = curFound.paragraphs[0];  
                  if ( curPara.appliedParagraphStyle.name == "h1" || curPara.appliedParagraphStyle.name == "h2" ||curPara.appliedParagraphStyle.name == "h3" ) {  
                      curFound.appliedCharacterStyle = "italic";  
                  }  
              } 
              
              
              app.findGrepPreferences.findWhat = "(?<=see):(?=\\x20\\(continued)";     
              allFounds = curDoc.findGrep();
                
              app.findGrepPreferences = app.changeGrepPreferences = null;  
                    
              for( var n = 0; n < allFounds.length; n++ ) {     
                  var curFound = allFounds[n];  
                  var curPara = curFound.paragraphs[0];  
                  if ( curPara.appliedParagraphStyle.name == "h1" || curPara.appliedParagraphStyle.name == "h2" ||curPara.appliedParagraphStyle.name == "h3" ) {  
                      curFound.appliedCharacterStyle = "bold";  
                  }  
              } 
              

               

               

              2.

              var curDoc = app.activeDocument;  
                
              app.findGrepPreferences = app.changeTextPreferences = null;     
                    
              app.findGrepPreferences.findWhat = "see:\\x20\\(continued";     
              var allFounds = curDoc.findGrep();
                      
              for( var n = 0; n < allFounds.length; n++ ) {     
                  var curFound = allFounds[n];  
                  var curPara = curFound.paragraphs[0];  
                  if ( curPara.appliedParagraphStyle.name == "h1" || curPara.appliedParagraphStyle.name == "h2" ||curPara.appliedParagraphStyle.name == "h3" ) {  
                app.findGrepPreferences.findWhat = "see|continued";     
                app.changeGrepPreferences.appliedCharacterStyle = "italic";
                curFound.changeGrep();
                app.findGrepPreferences.findWhat = ":";     
                app.changeGrepPreferences.appliedCharacterStyle = "bold";
                curFound.changeGrep();
                  }  
              } 
              
              
              app.findGrepPreferences = app.changeTextPreferences = null;
              
              • 4. Re: Apply character style for particular word only by script
                hasvi Level 1

                Hi kai,

                 

                You done excellent job, both scripts are working fine. Thanks a lot.


                by

                hasvi