10 Replies Latest reply on May 19, 2014 11:56 PM by hasvi

    widows orphan line script

    hasvi Level 1

      Hi,

       

      I want to find the 'widows & orphan' lines in my active document, if its (window orphan) appeared, I need the log report. Is this possible by script.

       

       

      by

      hasvi

        • 1. Re: widows orphan line script
          Chinnadk Level 4

          Hi Hasvi,

           

          Use the below code to get the widow and orphan report.

           

          var pages = app.activeDocument.pages;

          var reportwidow = "Following pages have widow line \r\r", reportorphan = "Following pages have orphan line \r\r", last, lastbutone, first, second;

          for(var i=0;i<pages.length;i++)

          {

              try{

                  last = pages[i].textFrames[0].paragraphs[-1].lines[-1].parentTextFrames[0].parentPage.name;

                  lastbutone = pages[i].textFrames[0].paragraphs[-1].lines[-2].parentTextFrames[0].parentPage.name;

                  first = pages[i].textFrames[0].paragraphs[-1].lines[0].parentTextFrames[0].parentPage.name;

                  second = pages[i].textFrames[0].paragraphs[-1].lines[1].parentTextFrames[0].parentPage.name;

              }

              catch(e){}

              if(last !=lastbutone)

              {

                      reportwidow += last +", ";

                  }

              if(first !=second)

              {

                      reportorphan += first + ", ";

                  }

          }

          alert(reportwidow);

          alert(reportorphan);

           

          Regards,

          Chinna

          • 2. Re: Re: widows orphan line script
            Jump_Over Level 5

            Hi,

             

            @Chinna,

             

            What about more textFrames on one page and more columns in a specific textFrame?

             

            I think the code should compare each textColumns:

            var
                 firstParas = app.activeDocument.pages.everyItem().textFrames.everyItem().textColumns.everyItem().paragraphs[0].getElements(),
                 lastParas =  app.activeDocument.pages.everyItem().textFrames.everyItem().textColumns.everyItem().paragraphs[-1].getElements(),
                 len = firstParas.length, cnt = -1,
                 widRep = "", orpRep = "";
            
            while (++cnt < len)
              if (firstParas[cnt].lines[-1].textColumns[0] != firstParas[cnt].lines[-2].textColumns[0])
              widRep += "Page: " + firstParas[cnt].lines[-1].parentTextFrames[0].parentPage.name +
                        "\tcheck the line: " + firstParas[cnt].lines[-1].contents;
            len = lastParas.length;
            cnt = -1;
            while (++cnt < len)
              if (lastParas[cnt].lines[0].textColumns[0] != lastParas[cnt].lines[1].textColumns[0])
              orpRep += "\rPage: " + lastParas[cnt].lines[0].parentTextFrames[0].parentPage.name +
                        "\tcheck the line: " + lastParas[cnt].lines[0].contents;
            
            if (orpRep.length == 0) orpRep = "\r\tnot found";
            if (widRep.length == 0) widRep = "\r\tnot found";
            alert ("Orphans:" + orpRep + "\rWidows:\r" + widRep);
            
            
            

             

            Jarek

            • 3. Re: widows orphan line script
              hasvi Level 1

              Hi thanks for your valuable answer

               

              Sent from Yahoo Mail on Android

              • 4. Re: widows orphan line script
                Chinnadk Level 4

                Thanks Jarek

                • 5. Re: widows orphan line script
                  hasvi Level 1

                  Hi,

                   

                  I try your script, but it shows error 'Object is invalid', on line 8, can you help me for this.

                   

                  by

                  hasvi

                  • 6. Re: widows orphan line script
                    hasvi Level 1

                    Hi Chinna,

                     

                    I modify your script slightly, I have added 1st 2-lines for condition, that is my paragraph line must be greater than 1 line (myPara > 1), because it shows report for single line also, that's why I write this. But its shows the error on 1st line, can you help me?

                    var myPara=app.activeDocument.pages[i].textFrames[0].paragraphs[0].lines.length;

                    if (myPara > 1)

                    {

                    var pages = app.activeDocument.pages;

                    var reportwidow = "Following pages have widow line \r\r", reportorphan = "Following pages have orphan line \r\r", last, lastbutone, first, second;

                    for(var i=0;i<pages.length;i++)

                    {

                        try{

                            last = pages[i].textFrames[0].paragraphs[-1].lines[-1].parentTextFrames[0].parentPage.name;

                            lastbutone = pages[i].textFrames[0].paragraphs[-1].lines[-2].parentTextFrames[0].parentPage.name;

                            first = pages[i].textFrames[0].paragraphs[-1].lines[0].parentTextFrames[0].parentPage.name;

                            second = pages[i].textFrames[0].paragraphs[-1].lines[1].parentTextFrames[0].parentPage.name;

                        }

                        catch(e){}

                        if(last !=lastbutone)

                        {

                                reportwidow += last +", ";

                            }

                        if(first !=second)

                        {

                                reportorphan += first + ", ";

                            }

                    }

                     

                     

                     

                     

                    var w=app.activeDocument;

                    var myTextFile = new File("~/Desktop/widow orphan.csv")

                    myTextFile.open("w")

                    myTextFile.write("\nreportwidow:")

                    myTextFile.write(reportwidow)

                    myTextFile.write("\n\nreportorphan:")

                    myTextFile.write(reportorphan)

                    myTextFile.close();

                    }

                     

                    by

                    hasvi

                    • 7. Re: widows orphan line script
                      Chinnadk Level 4

                      Hi Hasvi,

                       

                      Here is the modified one. If this fits your requirement then mark Jarek's answer is correct.

                       

                      var reportwidow = "Following pages have widow line \r\r", reportorphan = "Following pages have orphan line \r\r";

                      var firstParas = app.activeDocument.pages.everyItem().textFrames.everyItem().textColumns.everyItem().parag raphs[0].getElements(); 

                      var lastParas =  app.activeDocument.pages.everyItem().textFrames.everyItem().textColumns.everyItem().parag raphs[-1].getElements();

                       

                       

                      for(var i=0;i<firstParas.length;i++)

                      {

                          try{

                              var fline1 = firstParas[i].lines[-1].textColumns[0];

                              var fline2 = firstParas[i].lines[-2].textColumns[0];

                          }

                          catch(e){}

                          if(firstParas[i].lines.length>=2)

                          {

                              if(fline1 !=fline2)

                              {

                                  reportwidow += firstParas[i].lines[-1].parentTextFrames[0].parentPage.name +", ";

                              }

                          }

                      }

                       

                       

                      for(var i=0;i<lastParas.length;i++)

                      {

                          try{

                              var Lline1 =lastParas[i].lines[0].textColumns[0];

                              var Lline2 = lastParas[i].lines[1].textColumns[0];

                          }

                          catch(e){}

                          if(lastParas[i].lines.length>=2)

                          {

                              if(Lline1 !=Lline2)

                              {

                                  reportorphan += lastParas[i].lines[0].parentTextFrames[0].parentPage.name+", ";

                              }

                          }

                      }

                      var w=app.activeDocument;

                      var myTextFile = new File("~/Desktop/widow orphan.csv")

                      myTextFile.open("w")

                      myTextFile.write("\nreportwidow:")

                      myTextFile.write(reportwidow)

                      myTextFile.write("\n\nreportorphan:")

                      myTextFile.write(reportorphan)

                      myTextFile.close();

                       

                      Regards,

                      Chinna

                      • 8. Re: Re: widows orphan line script
                        Jump_Over Level 5

                        Hi,

                         

                        Yes. Sorry. This error can be thrown when 1-liner comes or text frame is overflown.

                        So modify both if statement conditions alike:

                        ...

                        if (firstParas[cnt] &&

                            firstParas[cnt].lines.length > 1 &&

                            firstParas[cnt].lines[-1].textColumns[0] != firstParas[cnt].lines[-2].textColumns[0]);

                        ...

                         

                        Jarek

                        • 9. Re: Re: widows orphan line script
                          hasvi Level 1

                          Thanks Jarek

                          • 10. Re: widows orphan line script
                            hasvi Level 1

                            Thank you chinna