12 Replies Latest reply on Feb 28, 2017 1:58 AM by Liphou

    Find firstword of paragraph Lines

    Ananth@desgin Level 2

      Hi All,

       

      How to find the continue 3 same words in firstLine?

      I tried the code 3 same words lastword of lines is working, but firstLine is not working.Please suggest friends

       

      Code is here:

      var myDoc = app.activeDocument;
      var myLines_first = app.activeDocument.stories.everyItem().lines.everyItem().words.firstItem().getElements();
      for (var i=0; i<myLines_first.length; i++)
      {
          if(myLines_first[i].contents==myLines_first[i+1].contents && myLines_first[i].contents==myLines_first[i+2].contents)
          {
             var myResult = app.select(myLines_first[i])
              alert("stack word there")
              break;
           }
      else
      {
           alert("No stackwords found");
           exit();
      }
      }

      Thanks,

      Prabu G

        • 1. Re: Find firstword of paragraph Lines
          Jump_Over Level 5

          Ananth@desgin  napisał(-a)

           

          ... but firstLine is not working ...

          be more specific what "not working" does mean...

          no result, wrong result, error?

           

          Jarek

          • 2. Re: Find firstword of paragraph Lines
            Obi-wan Kenobi Adobe Community Professional

            Hi,

             

            You could try this:

             

            var myDoc = app.activeDocument,
            myLines_first = myDoc.stories.everyItem().lines.everyItem().words.firstItem().getElements(),
            myCounter = 0;
            for (var i=0; i < myLines_first.length-2; i++) {
                if (myLines_first[i].contents == myLines_first[i+1].contents && myLines_first[i].contents == myLines_first[i+2].contents) {
                    var myResult = app.select(myLines_first[i]);
                    alert("Words Stack Here: \"" + myLines_first[i].contents + "\"\r(^/)  ;-)")
                    myCounter += 1;
                }
            }
            if (myCounter == 0) alert("No Words Stack Found!\r(^/)  :(")
            

             

             

            (^/) 

            1 person found this helpful
            • 3. Re: Find firstword of paragraph Lines
              Ananth@desgin Level 2

              Hi Obi-wan Kenobi,

               

              This worked perfectly! It was exactly the script I was looking for. Thanks so much, you're awesome!!!.

              I need one clarification:  "myLines_first.length-2", why we use "-2"??

               

              Thanks,

              Prabu

              • 4. Re: Find firstword of paragraph Lines
                Ananth@desgin Level 2

                Hi Jarek,

                Thanks for the reply. In above code show error message.

                 

                Thanks,

                Prabu G

                • 5. Re: Find firstword of paragraph Lines
                  Obi-wan Kenobi Adobe Community Professional

                  "-2" because you don't need to loop on "n-1" and "n" (targeted by "n-2")!

                  If you actually try, you'll get an error because "n+1" and "n+2" don't exist! 

                   

                  (^/)

                  • 6. Re: Find firstword of paragraph Lines
                    Ananth@desgin Level 2

                    Hi Obi,

                     

                    images.png

                    Thanks,

                    Prabu G

                    • 7. Re: Find firstword of paragraph Lines
                      [Jongware] Most Valuable Participant

                      What error? Why don't you tell us?

                      • 8. Re: Find firstword of paragraph Lines
                        Laubender Adobe Community Professional & MVP

                        Hi Prabu,

                        there is much to say about the logic of your script.

                         

                        1. Error message
                        "Undefined is not an object"

                         

                        With a document that contains only one story with two lines of text that would happen.

                        Or a document with two stories each conatining one line of text.

                        Or more stories, all empty with exception of one story with two lines or two stories with one line each.

                         

                        I hope you guess why. Your loop tries to reach out to elements in the array that do not exist:

                        i+1 and i+2 where i is length-1

                        At that moment the loop throws the error "Undefined is not an object".

                         

                        2. Error message

                        "Invalid Object".

                         

                        Your document conains only empty stories and the script is throwing an error in line 2 where the array should be built.

                        That would also be the case with Obi-wan's version.

                         

                        That error could be prevented.

                         

                        var wordsLength = myDoc.stories.everyItem().words.everyItem().length;
                        if(wordsLength == 0){alert("No elements in collection of words"); exit()};
                        

                         

                        3. Your script is alerting false positives as you can see below.

                        Why? Because you are doing an array of all stories with all lines and all first words.

                         

                        See the screenshot below.

                        The script checks the first word of the next story ( i+2 ) and compares with words i+1 and i of the first story.

                         

                        Prabu-FalsePositive.png

                         

                        Obi-wan's script is doing the same:

                         

                        Obi-wan-FalsePositive.png

                         

                        3. Your script is only doing one single iteration of the loop with every script run!

                         

                        Why?

                        A. If the first three lines of the first story contain a stack it will break the loop.

                        B. If the first three lines of the first story contain no stack:

                        you alert that nothing is found.
                        And exit the script!

                         

                        So it cannot detect a stack that begins after the first line of the first story.

                         

                        Regards,
                        Uwe

                        • 9. Re: Find firstword of paragraph Lines
                          Obi-wan Kenobi Adobe Community Professional

                          Hi Uwe!

                           

                          /*
                              Script written by Michel Allio [2017/02/26]
                              version B [2017/02/27]
                              See:  https://forums.adobe.com/message/9357481
                              Object: Find 3 consecutive first line words! …
                          */
                          
                          
                          app.doScript("main()", ScriptLanguage.javascript, undefined, UndoModes.ENTIRE_SCRIPT, "3-Words Stack! …");
                          
                          
                          function main() {
                              
                              var myDoc = app.activeDocument,
                              myStories = myDoc.stories.everyItem().getElements(),
                              S = myStories.length;
                              if (myDoc.conditions.item("Words Stack").isValid)  myDoc.conditions.item("Words Stack").remove();
                              myCondition = app.activeDocument.conditions.add ({name: "Words Stack", indicatorMethod: ConditionIndicatorMethod.USE_HIGHLIGHT});
                          
                          
                              while (S--)  {
                                  if (myStories[S].words.length == 0)  continue;  
                                  var myLines_first = myStories[S].lines.everyItem().words.firstItem().getElements();
                                  for (var i = 0; i < myLines_first.length-2; i++) {
                                      if (myLines_first[i].contents == myLines_first[i+1].contents && myLines_first[i].contents == myLines_first[i+2].contents) {
                                          myLines_first[i].appliedConditions = [myCondition]; 
                                          myLines_first[i+1].appliedConditions = [myCondition]; 
                                          myLines_first[i+2].appliedConditions = [myCondition]; 
                                      }
                                  }
                              }
                          
                          
                          }
                          

                           

                           

                          (^/)

                          • 10. Re: Find firstword of paragraph Lines
                            Liphou Level 1

                            Bonjour  Obi-wan,

                             

                            Est-il possible d'avoir une explication  pour la ligne suivent :

                             

                            app.doScript("main()", ScriptLanguage.javascript, undefined, UndoModes.ENTIRE_SCRIPT, "3-Words Stack! …");
                            

                             

                            Merci beaucoup

                             

                            Philippe

                            • 11. Re: Find firstword of paragraph Lines
                              Laubender Adobe Community Professional & MVP

                              Hi Philippe,

                               

                              the method app.doScript() is available in InDesign's DOM since a long time.

                              It was enhanced by the developers in InDesign CS4 where undo modes were added.

                               

                              In CS4, CS5 and CS5.5 ( I'm not sure what the version is where a special bug was fixed ) there were some problems with UndoModes.FAST_ENTIRE_SCRIPT. So for sake of compatibility best use: UndoModes.ENTIRE_SCRIPT

                               

                              See some details here from Jongware's DOM documentation:

                              Adobe InDesign CS4 (6.0) Object Model JS: Application

                               

                              Search the forum and read about how doScript() can be utilized.

                              You will find a lot of discussions and code samples.

                               

                              Back to Obi-wan's code line:

                               

                              1. The first argument of the method is the name of the function that is fed.

                              Could also be written without its string representation:

                              app.doScript ( main , …

                               

                              The first argument could also be a script file somewhere on the harddisk.
                              Or a string that is written from a text file…

                               

                              2. Argument two is the script language.

                              For example one could also execute AppleScript code if the script is executed on Mac OSX.

                              Or VB code when on Windows.

                               

                              3. Argument three is for Arguments that could be fed as array. E.g. if you want to inject preprocessed values from ExtendScript to an AppleScript that comes with argument one. Just one example. Search the forum for details. Also see app.scriptArgs.

                               

                              4. Argument four is the undo mode

                               

                              5. Argument five is the string that is presented to the user and is visible with InDesign's undo UI.

                               

                              Regards,
                              Uwe

                              2 people found this helpful
                              • 12. Re: Find firstword of paragraph Lines
                                Liphou Level 1

                                Merci Laubender,

                                 

                                Merci pour cette explication.

                                 

                                Philippe