3 Replies Latest reply on Feb 26, 2013 6:10 AM by Peter Kahrel

    Read text from document line by line

    SimonLinden Level 1

      I have some text that I want to manipulate either in InDesign or before placing it in InDesign. I know how to do GREP search/find from the script but now I need to read the text into the document to do things I don't think I can do with GREP. So the short question is how to get external text into a string that I can loop over and make changes to with other script functions, preferably line by line.

       

      This should show how I'm thinking:

       

      grepFind("(^.+)") = curString;

       

      function grepFind(find){

          app.findGrepPreferences = NothingEnum.nothing;

          app.findGrepPreferences.findWhat = find;

         

          //return found text

      }

       

       

      ------------------------------------------------------------------------------------------ --------------------------

       

      I'm providing the full background just in case someone has suggestions on how to do this more efficiently:

       

       

      I have many textdocuments that are ordered like this:

      a ...[some random text]

      b ...

      c ...

       

      a ...

      b ...

       

      a ...

       

      Each instance of 'a' signifies a new chapter and each new line after that is a new verse in that chapter (until we reach the next 'a'). So the example above I'd like to change to:

      1:1 ...

      1:2 ...

      1:3 ...

       

      2:1 ...

      2:2 ...

       

      3:1 ...

       

      To automate this I need variables that increase with every found instance of a pattern - to my knowledge this can't be done with GREP?

        • 1. Re: Read text from document line by line
          Peter Kahrel Adobe Community Professional & MVP

          I would use paragraph numbering, then maybe a Find/Change query to apply the paragraph styles. Something like this:

           

          1. Create a paragraph style, named e.g. chapter and verse. Enable paragraph numbering, set the level to 2, and use ^1.^#.^t as the number.

           

          2. Apply this paragraph style to all lines that start with a chapter or a verse number.

           

          You'll see that everything is now numbered as if you had just one chapter. That's because the chapter number changes only if you insert a paragraph of level 1. You don't use those, but you have to have them, so you insert them and you hide them:

           

          3. Create a paragraph style, named e.g. chapter. Enable paragraph numbering, set the level to 1, use ^1 as the number string. Set the leading to 0 and the character colour to None.

           

          4. Insert a blank line before all paragraphs that start with a, and apply the chapter paragraph style to those blank lines. Two simple grep replacements do the job:

          a. Find what: (?<=\r)a

          b. Replace with: @@@@\ra

           

          a. Find what: @@@@

          b. Replace with: <nothing>

          c. Change format: par. style chapter

           

          Peter

          • 2. Re: Read text from document line by line
            SimonLinden Level 1

            That's a clever solution. Didn't realize you could use numbered lists like that.

             

            Is there a way to "apply" this formatting so that the numbers are a real part of the text?

            • 3. Re: Read text from document line by line
              Peter Kahrel Adobe Community Professional & MVP

              The only thing I can think of is to convert the numbers to text. In the UI you'd use Type > Bulleted and numbered lists > Convert . . . to text. In a script you would use the paragraph's bulletsAndNumberingResultText property.