8 Replies Latest reply on Oct 29, 2018 8:55 AM by Laubender
      • 1. Re: Add text if ParagraphStyle is found
        Manan Joshi Level 4

        Try the following code

        function addTextToPara(pStyleName, prefixText)
        {
               app.findGrepPreferences.appliedParagraphStyle = pStyleName
               var paraList = app.activeDocument.findGrep()
               for(var i = 0 ; i < paraList.length; i++)
               {
                    paraList[i].contents = prefixText + paraList[i].contents
               }
               app.findGrepPreferences = NothingEnum.nothing;
        }
        
        //Change the argument with the name of the paragraph style you want to search and the text you want to add before that para
        addTextToPara("Paragraph Style 2", "<1>") 
        

         

        -Manan

        • 2. Re: Add text if ParagraphStyle is found
          beh_gras_beh Level 1

          Hi Manan,

           

          That worked perfectly!

          Many thanks.

          • 3. Re: Add text if ParagraphStyle is found
            beh_gras_beh Level 1

            Hi Manan and others,

             

            Is there also an way the text would be at the end of an paragraph?

            Like <1>here is some text<1>

             

            Again thanks for you'r help!

             

            Greetings.

            • 4. Re: Add text if ParagraphStyle is found
              Manan Joshi Level 4

              In place of the below line in the code mentioned before in the thread use the line that follows it

              //Replace this
              paraList[i].contents = prefixText + paraList[i].contents
              
              //With this
              paraList[i].contents = prefixText + paraList[i].contents + prefixText
              

               

              -Manan

              • 5. Re: Add text if ParagraphStyle is found
                beh_gras_beh Level 1

                Wouw that's an extremely fast answer! Thanks!

                 

                It is placing the text alright but at the beginning of an new paragraph.

                 

                This

                Schermafbeelding 2018-10-28 om 17.12.28.png

                Becomes this

                Schermafbeelding 2018-10-28 om 17.12.11.png

                I'm wondering if there is an way you can tell indesign that there is an end of an paragraph (without using /n or /r)?

                And if found add <2> so you get <2>TEST<2>.

                 

                Greetings.

                • 6. Re: Add text if ParagraphStyle is found
                  Manan Joshi Level 4

                  I am not sure why you don't wanna use \r or \n for check condition. The following works for me

                  function addTextToPara(pStyleName, prefixText)  
                  {  
                         app.findGrepPreferences.appliedParagraphStyle = pStyleName  
                         var paraList = app.activeDocument.findGrep(true)  
                         for(var i = 0 ; i < paraList.length; i++)  
                         {
                                 if(paraList[i].contents[paraList[i].contents.length - 1] == '\r')
                                      paraList[i].insertionPoints[-2].contents =  prefixText
                                 else
                                      paraList[i].insertionPoints[-1].contents = prefixText
                                 paraList[i].contents = prefixText + paraList[i].contents
                         }  
                         app.findGrepPreferences = NothingEnum.nothing;  
                  }  
                    
                  //Change the argument with the name of the paragraph style you want to search and the text you want to add before that para  
                  addTextToPara("Paragraph Style 2", "<1>") 
                  

                   

                  -Manan

                  1 person found this helpful
                  • 7. Re: Add text if ParagraphStyle is found
                    beh_gras_beh Level 1

                    Oh, but this works perfectly, can I double give you the credits?

                     

                    The reason I'm scared to use \r is that sometimes in the story there is an \r and sometimes an \n.

                    I thought when you use that variable the script puts in the middle of story 's an <1> and I only wanted it at the end of each lines of text with an specific paragraph style but that's not the case, I see now.

                    You look for an specific paragraph style and the end of each paragraph style it always ends with an \r. I was thinking wrong.

                     

                    Again for the second time thanks for you'r help!

                     

                    Greetings.

                    • 8. Re: Add text if ParagraphStyle is found
                      Laubender Adobe Community Professional & MVP

                      Hi beh_gras_beh ,

                      is this thread related to the one below? Guess, yes:

                      GREP style in paragraph with ~b \r or \n?

                       

                      Regards,
                      Uwe