15 Replies Latest reply on Sep 16, 2018 9:15 PM by yadavs92328

    Underline the last word found in a particular paragraph

    성재원64181229

      I have a problem.

      When I use Grep expression to find a specific word, the whole word is searched based on the story.

      But the word I'm looking for is a specific word at the end of the paragraph.

      I want to underline the character style only in the last word of the whole paragraph.

      I want to know how to implement with Grep expressions or scripts.

       

       

      For example,

       

       

      I have a dream.

      I have a dream. I have a dream.

      I have a dream. I have a dream. I have a dream, too.

      I have a dream. I have a dream. I have a dream, too. Dream come true.

        • 1. Re: Underline the last word found in a particular paragraph
          yadavs92328 Level 2

          you can try this without grep if you want to.

          var myDoc = app.documents[0];
          var myFrame = myDoc.pages[0].textFrames[0];
          var para = myFrame.paragraphs;
          for(var i = 0; i < para.length; i++){
              var myWord = null;
              var words = para[i].words;
              for(var j = 0; j < words.length; j++){
                  if(words[j].contents.toString().toLowerCase().indexOf("dream") != -1){
                      myWord = words[j];
                      }
                  }
              if(myWord != null){
                  myWord.underline = true;
                  }
              }
          
          • 2. Re: Underline the last word found in a particular paragraph
            성재원64181229 Level 1

            Very good.

            I appreciate your solution.

            However, the example above was made in English to help you understand.

            "dream" with examples in English apply well.

             

            But the language I use is East Asia.

            I want to find the words made in China, Korea, Japan language as the above question.

             

            For example, it might be expressed here,

            not | wrong | wrong

             

            In Korean, it is as follows.

            아닌 | 틀린 | 잘못된

             

            My aim is to underline the word that is located at the end of the paragraph when these words overlap in a paragraph.

             

            I want to know if this language case of CJK culture is possible.

             

            Thank you in advance.

            • 3. Re: Underline the last word found in a particular paragraph
              yadavs92328 Level 2

              So you can try this, You just Need to add your text in your array.

              var myDoc = app.documents[0];
              var myFrame = myDoc.pages[0].textFrames[0];
              var para = myFrame.paragraphs;
              var myFindingWords = ["dream", "test", "test1"];
              for(var m = 0; m < myFindingWords.length; m++){
                  for(var i = 0; i < para.length; i++){
                      var myWord = null;
                      var words = para[i].words;
                      for(var j = 0; j < words.length; j++){
                          var myWordFind = myFindingWords[m].toLowerCase();
                          if(words[j].contents.toString().toLowerCase().indexOf(myWordFind) != -1){
                              myWord = words[j];
                              }
                          }
                      if(myWord != null){
                          myWord.underline = true;
                          }
                      }
                  }
              
              • 4. Re: Underline the last word found in a particular paragraph
                성재원64181229 Level 1

                As you mentioned, I put "아닌" in test1 and "없는" in test2.

                But it did not work.

                I wonder if word object does not apply in 2byte language.

                 

                I also attach an image file of what I am about to replace. The red underline is the part you want to replace.image1.png

                 

                 

                var myDoc = app.documents[0];  
                var myFrame = myDoc.pages[0].textFrames[0];  
                var para = myFrame.paragraphs;  
                var myFindingWords = ["아닌", "없는"];  
                for(var m = 0; m < myFindingWords.length; m++){  
                    for(var i = 0; i < para.length; i++){  
                        var myWord = null;  
                        var words = para[i].words;  
                        for(var j = 0; j < words.length; j++){  
                            var myWordFind = myFindingWords[m].toLowerCase();  
                            if(words[j].contents.toString().toLowerCase().indexOf(myWordFind) != -1){  
                                myWord = words[j];  
                                }  
                            }  
                        if(myWord != null){  
                            myWord.underline = true;  
                            }  
                        }  
                    } 
                
                • 5. Re: Underline the last word found in a particular paragraph
                  yadavs92328 Level 2

                  This is working properly in my system.

                  • 6. Re: Underline the last word found in a particular paragraph
                    성재원64181229 Level 1

                    Really?

                    It is strange that I do not work on my computer.

                    On the ExtendScript Toolkit screen, the result is "Execution finished: Result: undefined".

                    Does my Indesign have a problem?

                     

                    image2.png

                    • 7. Re: Underline the last word found in a particular paragraph
                      yadavs92328 Level 2

                      "Execution:finished" doesn't mean that there is an error.

                      Can you share your indesign file with some of those string?

                      So that I can have a loot at it?

                      • 8. Re: Underline the last word found in a particular paragraph
                        성재원64181229 Level 1

                        I replaced it with another string and made it a sentence.

                        If you follow the link you will get the inDesign shared file.

                        Please solve my problem.

                        https://www.dropbox.com/s/p6xhgf8ralghtmb/test_modified.zip?dl=0

                        I added the used fonts. And inDesign was packaged.

                        • 9. Re: Underline the last word found in a particular paragraph
                          yadavs92328 Level 2

                          Code has worked in my system on your provided file.

                          I don't find any error!

                          Only one character has match and it is underlined!

                          Capture.JPG

                          • 10. Re: Underline the last word found in a particular paragraph
                            성재원64181229 Level 1

                            Thank you.

                            I'll have to reinstall my program once.

                            • 11. Re: Underline the last word found in a particular paragraph
                              성재원64181229 Level 1

                              I deleted the Indesign korean version and installed the English version.

                              Then it worked the same as you were successful.

                              The program may or may not be applied according to the language of each country.

                              This seems to be an official question to the Adobe company.

                              I am grateful for your efforts to solve the problem.

                              Thank you so much.

                               

                              ps.

                              In addition, I ask you one more question.

                              I wanted a way to underline only word in a particular paragraph.

                              So please let me know how to find this in a paragraph with a paragraph style named "AA" rather than an entire paragraph.

                              • 12. Re: Underline the last word found in a particular paragraph
                                yadavs92328 Level 2

                                Just on condition before finding text in that paragraph, Just like this,

                                var myDoc = app.documents[0];
                                var myFrame = myDoc.pages[0].textFrames[0];
                                var para = myFrame.paragraphs;
                                var myFindingWords = ["아닌", "없는"];
                                for(var m = 0; m < myFindingWords.length; m++){
                                    for(var i = 0; i < para.length; i++){
                                //Test Your para style here
                                        if(para[i].appliedParagraphStyle.name.toString() == "TestParagraphStyle"){
                                            var myWord = null;
                                            var words = para[i].words;
                                            for(var j = 0; j < words.length; j++){
                                                var myWordFind = myFindingWords[m].toLowerCase();
                                                if(words[j].contents.toString().toLowerCase().indexOf(myWordFind) != -1){
                                                    myWord = words[j];
                                                    }
                                                }
                                            if(myWord != null){
                                                myWord.underline = true;
                                                }
                                            }
                                        }
                                    }
                                
                                1 person found this helpful
                                • 13. Re: Underline the last word found in a particular paragraph
                                  성재원64181229 Level 1

                                  Your solution works very well.

                                  This is the best answer.

                                  Thank you once again for your sincerity!!

                                  • 14. Re: Underline the last word found in a particular paragraph
                                    Michel [FRIdNGE] Level 4

                                    Hi Yadav,

                                     

                                    Interested by your code, I've tested it! …

                                    [Why limit the research to one text frame on the first page?]

                                     

                                    Targets: "dream" and "cloud"

                                     

                                    Using your last code:

                                     

                                    Capture d’écran 2018-09-16 à 22.39.16.png

                                     

                                    Mine:

                                     

                                    Capture d’écran 2018-09-16 à 22.39.47.png

                                     

                                    Grep seems to me simpler to code and mostly more efficient! …

                                     

                                    // by FRIdNGE [September 2018]
                                    
                                    
                                    app.doScript("main()", ScriptLanguage.javascript, undefined, UndoModes.ENTIRE_SCRIPT, "Underlinings! …");
                                    
                                    
                                    function main()
                                        {
                                            // ---------------------------------------------------------------------------------------------------
                                            var myTargets = ["dream","cloud"], // To be completed! …
                                            myPStyle = "TestParagraphStyle", // Name of the Para Style
                                            myCStyle = "Underlining", // Name of the Char Style
                                            // ---------------------------------------------------------------------------------------------------
                                            T = myTargets.length,  t;
                                            app.findGrepPreferences = app.changeGrepPreferences = null;
                                            for ( t = 0; t < T; t++ ) {
                                                app.findGrepPreferences.findWhat = "(?i)(\\b" + myTargets[t] + "\\b)(?!.+\\b\\1\\b.*)";
                                                app.findGrepPreferences.appliedParagraphStyle = myPStyle;
                                                app.changeGrepPreferences.appliedCharacterStyle = myCStyle;
                                                app.activeDocument.changeGrep();
                                            }
                                            app.findGrepPreferences = app.changeGrepPreferences = null;
                                            alert("Done! …  ;-)\rby FRIdNGE [September 2018]")
                                        }
                                    

                                     

                                    Best,

                                    Michel, for FRIdNGE

                                    1 person found this helpful