6 Replies Latest reply on Jan 22, 2010 7:03 AM by seb400

    Script to determine number of lines of text in a paragraph?

    seb400 Level 1

      I'm revisiting an issue I first posted here last summer, thanks to all who offered help, but I've not been able to sort it out.

       

      http://forums.adobe.com/thread/455526?tstart=0

       

      What I'm after is  a 'search function' to insert into a FindChangeByList.

       

      Jongware suggested the following - but when inserted into my list I can't get it to do anything - it doesn't throw up a Javascript error.

       

      app.findTextPreferences = NothingEnum.nothing;
      app.findTextPreferences.appliedParagraphStyle = "Callout_3";
      foundItems = app.activeDocument.findText();
      for (var i=foundItems.length-1; i>=0; i--)
      {
        if (foundItems[i].paragraphs[0].lines == 2)
          foundItems[i].paragraphs[0].appliedParagraphStyle = "Callout_2";
        if (foundItems[i].paragraphs[0].lines == 1)
          foundItems[i].paragraphs[0].appliedParagraphStyle = "Callout_1";
      }

       

      I regularly receive a 'word' document on which I run a 'FindChangeByList' which cleans up and formats the text with a number of GREP/text searches.

      One of the search lines converts a particluar string of text to a paragraph style "Callout_3" (the most common occurence of Callouts).

      This style applies a paragraph rule, above and below the line, to create a coloured block/band out of which said text is reveresed.

      The problem that I have is that the style (because of the paragraph rules), only works correctly if the paragraph has x3 lines of text.

      I have styles set up for every other eventuality - Callout_1 (for single line of text), Callout_2 (for x2 lines of text) etc.

       

      What I'm looking for is a script that can count the number of lines in the paragraph (once the "Callout_3" style has been applied) and then change the style accordingly to the number of lines in each paragraph/Callout.

       

      Any further thoughts?

       

      Steve

        • 1. Re: Script to determine number of lines of text in a paragraph?
          [Jongware] Most Valuable Participant

          Hi Steve!

           

          Well, I said it was untested ... Found two major errors. First one is also visible when you manually search for the style "Callout_3". Rather than marking each and every paragraph on its own, InDesign gathers as much as possible. Hence, just testing paragraph[0] would miss out all other paragraphs. Explicitly looping over all paragraphs per "found item" does work.

          The other one is sort of a "syntax" error. Merely testing "lines == 2" does not work -- but it doesn't give an error either! Apparently, it's okay to compare an object ("lines") to a number, even though it doesn't work. The property to check is "lines.length" -- that returns the number of lines for a paragraph.

           

          Putting it all together results in this script.

           

          'And now I shall test it,' said Wen. This time he twisted it gently to and fro. 'That si-si-si That simple-ple, eh eheh simple, eh?'

           

          ... And I tested it.

           

          app.findTextPreferences = NothingEnum.nothing;
          app.findTextPreferences.appliedParagraphStyle = "Callout_3";
          foundItems = app.activeDocument.findText();
          for (var i=foundItems.length-1; i>=0; i--)
          {
           for (var j=0; j<foundItems[i].paragraphs.length; j++)
           {
            if (foundItems[i].paragraphs[j].lines.length == 2)
             foundItems[i].paragraphs[j].appliedParagraphStyle = "Callout_2";
            if (foundItems[i].paragraphs[j].lines.length == 1)
             foundItems[i].paragraphs[j].appliedParagraphStyle = "Callout_1";
           }
          }
          
          • 2. Re: Script to determine number of lines of text in a paragraph?
            seb400 Level 1

            Hi Jongware

            many thanks again - but I can't get this to work either

             

            There are a couple of things that I have had to alter which may have been the problem!

             

            my paragraph styles are called

             

            Callout_1line

            Callout_2line

            Callout_3line

             

            They are in a 'paragraph style group' called "Callouts" which needs the extra reference.

             

            I did make a copy of the styles and put them into the root  (then applied that style to the text), and ran your original file without sucess.

             

            Steve

            • 3. Re: Script to determine number of lines of text in a paragraph?
              [Jongware] Most Valuable Participant

              It's not fair! It worked when I tested it! It did!

              (seconds later ...) Okay, the problem appears to be referring to the correct paragraph styles. This next try uses a somewhat different approach.

              .. Tested again, and again it appears to work ..

               

               

              c1 = findStyle ("Callout_1");
              c2 = findStyle ("Callout_2");
              c3 = findStyle ("Callout_3");
              app.findTextPreferences = NothingEnum.nothing;
              app.findTextPreferences.appliedParagraphStyle = c3;
              foundItems = app.activeDocument.findText();
              for (var i=foundItems.length-1; i>=0; i--)
              {
               for (var j=0; j<foundItems[i].paragraphs.length; j++)
               {
                if (foundItems[i].paragraphs[j].lines.length == 2)
                 foundItems[i].paragraphs[j].appliedParagraphStyle = c2;
                if (foundItems[i].paragraphs[j].lines.length == 1)
                 foundItems[i].paragraphs[j].appliedParagraphStyle = c1;
               }
              }
              function findStyle (name)
              {
               for (var p=0; p<app.activeDocument.allParagraphStyles.length; p++)
               {
                if (app.activeDocument.allParagraphStyles[p].name == name)
                 return app.activeDocument.allParagraphStyles[p];
               }
               return null;
              }
              
              • 4. Re: Script to determine number of lines of text in a paragraph?
                seb400 Level 1

                Hi Jongware

                Thank you, its embarrasing that I can offer so little feedback to help you!

                 

                Couple of things though, having finally achived some sucess!

                 

                I had been pasting your script directly into my .txt file that I run using FindChangeByList, as I was looking for a solution that would combine with my other find/change list items. The title of my post is misleading therefore!!

                 

                I created a new document, pasting in only one line of text, styled as Callout_3

                I then saved your script as a .jsx file, and ran it separately - it swapped it to a Callout_1!!!!!

                I have amended the script to take care of 4 and 5 line callouts too.

                 

                However when I ran that .jsx on the working file I get the following error:

                 

                Error Number: 33794

                Error String: Object contains no text for find/change

                 

                Which I assumed was due to the style not being found as it is inside a 'paragraph style group'.

                Sure enough when I created new paragraph styles in the root and renamed them to match you script everything worked perfectly...

                 

                So two questions

                - can your script be incorporated into a FindChangeByList .txt file?

                - what needs to go before the 'findStyle ("Callout_1");' to reference it inside a 'paragraph style group' called "Callouts"?

                 

                Thanks very much, again

                Steve

                • 5. Re: Script to determine number of lines of text in a paragraph?
                  [Jongware] Most Valuable Participant

                  - can your script be incorporated into a FindChangeByList .txt file?

                   

                  No, it needs access to the actual paragraph that it finds to get the line count of that paragraph. FindChangeByList chains regular Find/Changes together, and you cannot search for some number of lines.

                   

                  - what needs to go before the 'findStyle ("Callout_1");' to reference it inside a 'paragraph style group' called "Callouts"?

                   

                  ... Nothing. Because the style may be inside a group, I wrote that little function. The findStyle function loops over *all* paragraph styles in the document, in the root as well as in every group, and returns the first style with the name you provide in the function call (tested!).

                  So if there is a "Callout_1" style anywhere, it ought to find it ... Are you sure you put the correct name at the top of the script? I saw in a later post it's actually called "Callout_1line".

                  • 6. Re: Script to determine number of lines of text in a paragraph?
                    seb400 Level 1

                    Jongware

                    Yes my fault - works perfectly, thanks very much!

                     

                    Steve