8 Replies Latest reply on Dec 14, 2012 1:51 AM by davehaigh

    How to create paragraphs from scratch and insert them into a text frame.

    davehaigh Level 1

      How do I create paragraph objects within a script, set their contents, set the paragraph style and then insert them into a text frame?

       

      For example how would I create the following entirely within a script?

       

      Capture.JPG

       

      I'm using InDesign CS6 and coding in Javascript.

        • 1. Re: How to create paragraphs from scratch and insert them into a text frame.
          csm_phil Level 4

          Hi davehaigh,

           

          Try the below JS code.

           

          This code will create the new document and create the new text frame and insert the text like "Some text in this paragraph" and create the paragraph styles "TEST" apply this style to the entire contents.

           

           

          var myDoc = app.documents.add();
          
          myDoc.paragraphStyles.add({name:"TEST"})
          
          var myTF = myDoc.pages[0].textFrames.add({geometricBounds:['3p','3p', '30p', '30p']});
          myTF.contents = 'Some text in this paragraph\nSome text in this paragraph\nSome text in this paragraph';
          myTF.parentStory.appliedParagraphStyle = myDoc.paragraphStyles.item("TEST");
          

           

           

          thx,

          csm_phil

          • 2. Re: How to create paragraphs from scratch and insert them into a text frame.
            davehaigh Level 1

            Thanks Phil,

             

            That creates one paragraph with one style name.

             

            Do you know how would I generate what I have in the screenshot? i.e. multiple paragraphs with varying style names?

            • 3. Re: How to create paragraphs from scratch and insert them into a text frame.
              [Jongware] Most Valuable Participant

              I use one of two methods, whichever is more convenient (or less inconvenient).

               

              The first way needs to know in advance how many paragraphs are to be added (and in the case of your purple text, which nth word is going to be colored). It has the advantage that inserting text in bulk  is very fast.

               

              The second way is to only append text at the end of your current text, setting the 'insertionpoint' properties to whatever needs changing. Slow, but if you do not know up front what text gets what formatting, it's the only way.

              (Well, that or you need to do a lot of book-keeping, saving the number of added paragraphs and whatnot.)

               

              alert ("method 1");
              var myDoc = app.documents.add();
              myDoc.paragraphStyles.add({name:"Test Name 1"})
              myDoc.paragraphStyles.add({name:"Test Name 2"})
              myDoc.colors.add({space:ColorSpace.RGB, colorValue:[255, 0, 255], name:"purply"});
              var myTF = myDoc.pages[0].textFrames.add({geometricBounds:['3p','3p', '30p', '30p']});
              myTF.contents = "Some text in this paragraph\rSome text in this paragraph";
              myTF.paragraphs[0].appliedParagraphStyle = "Test Name 1";
              myTF.paragraphs[1].appliedParagraphStyle = "Test Name 2";
              myTF.paragraphs[1].words[4].fillColor = "purply";
              
               alert ("method 2");
              var myDoc = app.documents.add();
              myDoc.paragraphStyles.add({name:"Test Name 1"})
              myDoc.paragraphStyles.add({name:"Test Name 2"})
              myDoc.colors.add({space:ColorSpace.RGB, colorValue:[255, 0, 255], name:"purply"});
              var myTF = myDoc.pages[0].textFrames.add({geometricBounds:['3p','3p', '30p', '30p']});
              myTF.insertionPoints[-1].appliedParagraphStyle = "Test Name 1";
              myTF.insertionPoints[-1].contents = "Some text in this paragraph\r";
              myTF.insertionPoints[-1].appliedParagraphStyle = "Test Name 2";
              myTF.insertionPoints[-1].contents = "Some text in this ";
              myTF.insertionPoints[-1].fillColor = "purply";
              myTF.insertionPoints[-1].contents = "paragraph";
              
              • 4. Re: How to create paragraphs from scratch and insert them into a text frame.
                [Jongware] Most Valuable Participant

                I also once or twice used a method 3: write all text into a temporary plain text file as Tagged Text, with all the right formatting tags in place, and import that into a fresh text frame. Again, it has some advantages but a couple of disadvantages as well.

                • 5. Re: How to create paragraphs from scratch and insert them into a text frame.
                  davehaigh Level 1

                  Hi Jongware. I'm going to give method 2 a try. If you're curious, as InDesign doesnt export indexes to epub, I am attempting to parse through an Index and generate a plain text version, using hyperlinks to text anchors instead of actual index markers. This should then be exported to an epub.

                   

                  By the way your CS6 Object Model Javascript reference guide (http://jongware.mit.edu/idcs6js/) has been invaluable so far. Thanks alot.

                  • 6. Re: How to create paragraphs from scratch and insert them into a text frame.
                    davehaigh Level 1

                    [Jongware], I have altered method 2 slightly so that it adds a third line with paragraph style "Test Name 1". However the style of "Test Name 1" has been applied to every paragraph.

                     

                    Could you take a look at the code and screenshot and point me towards what I have done wrong please?

                     

                     var myDoc = app.documents.add();
                    myDoc.paragraphStyles.add({name:"Test Name 1"})
                    myDoc.paragraphStyles.add({name:"Test Name 2"})
                    var myTF = myDoc.pages[0].textFrames.add({geometricBounds:['3p','3p', '30p', '30p']});
                    
                    myTF.insertionPoints[-1].appliedParagraphStyle = "Test Name 1";
                    myTF.insertionPoints[-1].contents = "Some text in this paragraph\r";
                    myTF.insertionPoints[-1].appliedParagraphStyle = "Test Name 2";
                    myTF.insertionPoints[-1].contents = "Some text in this paragraph\r";
                    myTF.insertionPoints[-1].appliedParagraphStyle = "Test Name 1";
                    myTF.insertionPoints[-1].contents = "Some text in this paragraph";
                     
                    

                     

                    Capture.JPG

                    • 7. Re: How to create paragraphs from scratch and insert them into a text frame.
                      [Jongware] Most Valuable Participant

                      Aagh. How utterly frustrtating!

                       

                      Okay, the problems are actually twofold. For one, 'insertionPoints[-1] should apply your new formatting "to" the position just after the last character, i.e., on the position of the to-be-added text. It works for simple formatting (such as my fillColor example) but in the case of paragraph styles there seem to be some hiccups!

                       

                      ... Which leads us to problem two. "insertionPoints[-1]" points to the 'character after the last hard return' (which may or may not be there), and moving the '\r' code from end to beginning of each paragraph just moves the problem around without solving it.

                       

                      So for paragraphs, you can try this

                       

                      myTF.insertionPoints[-1].contents = "Some text in paragraph 1\r";

                      myTF.insertionPoints[-2].appliedParagraphStyle = "Test Name 1";

                      myTF.insertionPoints[-1].contents = "Some text in paragraph 2\r";

                      myTF.insertionPoints[-2].appliedParagraphStyle = "Test Name 2";

                      myTF.insertionPoints[-1].contents = "Some text in paragraph 3\r";

                      myTF.insertionPoints[-2].appliedParagraphStyle = "Test Name 1";

                       

                      -- first I reversed inserting contents and applying the paragraph style, but then it fails (because, presumably, it tries to set the paragraph style of "what's just behind the hard return"). So I have to go back one additional character, using -2 instead of -1.

                       

                      Please don't tell me that this, in turn, fails with four paragraphs!  

                      • 8. Re: How to create paragraphs from scratch and insert them into a text frame.
                        davehaigh Level 1

                        I tried this on the simple example we were working with and it worked fine.

                         

                        However, when incorporated into my script I was having issues with the position at which the styles were being applied. See screenshot 1 as an example - the letter D should have a style of "Index Section Head". Do you have any idea why this could be happening?

                         

                        Capture1.JPG

                        Screenshot 1.

                         

                        I have found a way around this by using 0 as the index of the insertion point for adding content and for applying the style. This obviously would add everything in the wrong order as it is prepending the content to the beginning of the frame. So I have reversed my arrays and added the section titles after parsing the topics, and then added the main index title at the very end. This results in screenshot 2. Obviously it would be feel nicer to add paragraphs in order but this is the only reliable way I can see as a workaround.

                         

                        Capture2.JPG

                        Screenshot 2

                         

                        Thanks for your help on this Jongware. If yoy can think of a tweak to your method that means I can add content in the correct order rather than adding them in reverse like I am doing, then i'd be keen to see it.