12 Replies Latest reply on Dec 12, 2011 12:01 PM by [Jongware]

    How to ADD A Paragraph?

    Zoffix222 Level 1

      Can someone please assist with this small script.

       

      I'm trying to create a text frame with several paragraphs in it (so I could apply different paragraph styles).

       

      I spent half an hour already looking through Adobe's Scripting manual, and I found all the bells and whistles of paragraps, except how to add more than one paragraph to the text frame

       

      Here's what I have so far, obviously the paragraphs.item(1).contents line fails. I'm stumped. What I want is products[0].number and products[0].text to be on two separate lines and to be two separate paragraphs so I could style them differently.

       

      var doc = app.documents.add();
      var text_frame = doc.pages[0].textFrames.add();
      text_frame.geometricBounds = ['6p', '6p', '240pt', '240pt'];
      text_frame.contents = products[0].number + "\n\n" + products[0].text;
      text_frame.parentStory.paragraphs.item(1).contents = 'TEST';
      text_frame.fit(FitOptions.frameToContent);
      var b = text_frame.geometricBounds;
      
        • 1. Re: How to ADD A Paragraph?
          Zoffix222 Level 1

          Well, I found that paragraphs automatically get created if there are "\r" characters.

           

          Is there a function that would add a paragraph and leave the "\r" characters alone if they are present?

           

          Working with Adobe products is nothing but frustration >_<

          • 2. Re: How to ADD A Paragraph?
            Haakenlid Level 3

            Is there a function that would add a paragraph and leave the "\r" characters alone if they are present?

            Do you mean something like this?

             

            function addParagraph(myStory, myString){ // adds texts as new paragraph at end of a story
                myStory.contents = myStory.contents.replace(/\s+$/,"") //removes trailing whitespace in the story
                if (myStory.paragraphs.length > 0){
                    myString = "\r"+myString;
                }
                myStory.insertionPoints[-1].contents = myString;
            }
            
            • 3. Re: How to ADD A Paragraph?
              Harbs. Level 6

              I'm not sure what's so frustrating. I have never seen an application that is more scriptable than InDesign.

               

              You do have to learn how it works. Instead of getting frustrated, you can ask questions. That's how we all learned.

               

              Think about the UI. How do you add paragraphs? You type in text. Scripting works exactly the same way. It makes no sense to add a paragraph if there's no text in it.

               

              Yes. The paragraph delimiter is \r (just like every other app in existance).

               

              Harbs

              1 person found this helpful
              • 4. Re: How to ADD A Paragraph?
                Zoffix222 Level 1

                Thanks, that's what I was looking for, with the exception that the /\s+$/ becomes /\r/ (and I need to look up JavaScript's version of Perl's s/\r+//g operator)

                • 5. Re: How to ADD A Paragraph?
                  Zoffix222 Level 1

                  I guess I'm just upset that I spent so much time today trying to find all the little bits and pieces threwn all over the place.

                   

                  The fact that I just switched from GIMP and Scribus to Photoshop and InDesign and now my productivity dropped by 1000% because I need to learn the interface contributes to my current frustration

                   

                  Trying to find what functions do what in the PDF was a nightmare, but now I found this (which is a good start): http://jongware.mit.edu/idcs5/

                  • 6. Re: How to ADD A Paragraph?
                    Haakenlid Level 3

                    String.replace(/\r/,"") will remove the first \r in the string, in this case the entire content of the existing story. I'm pretty sure you don't want that.
                    String.replace(/\s+$/,"") will remove any whitespace that might be at the end of the String, including any combination of spaces, tabs, \r and \n.

                     

                    I've never used Perl, but I think that Javascript's regular expressions are heavily inspired from Perl.
                    What does the "s" and the second "/" signify in that expression? I assume the "g" is a global flag as in Javascript.

                    • 7. Re: How to ADD A Paragraph?
                      Zoffix222 Level 1

                      My problem is that the string I'm adding as a paragraph might contain "\r" characters anywhere in it. Thus, if I add it without taking care of them, I'll end up with multiple paragraphs instead of one.

                       

                      The 's' and '/' are not important, I was implying that I need to look up how to use the global flag in JavaScript

                       

                      I'll figure this out tomorrow.

                       

                      Thanks for the help.

                      • 8. Re: How to ADD A Paragraph?
                        Harbs. Level 6

                        Yes. Jongware's reference is the best way to browse the DOM.

                         

                        The chm version is the best of the best: http://www.jongware.com/idjshelp.html

                         

                        Harbs

                        • 9. Re: How to ADD A Paragraph?
                          Haakenlid Level 3

                          Zoffix222 wrote:

                           

                          My problem is that the string I'm adding as a paragraph might contain "\r" characters anywhere in it. Thus, if I add it without taking care of them, I'll end up with multiple paragraphs instead of one.

                           

                          The 's' and '/' are not important, I was implying that I need to look up how to use the global flag in JavaScript

                           

                          I'll figure this out tomorrow.

                           

                          Thanks for the help.

                           

                          I see. You can do somthing like this:

                           

                          function addParagraph(myStory, myString){ // adds texts as new paragraph at end of a story 
                              myString = myString.replace(/\r+/g," "); // replaces one or more \r with a single space. 
                              myStory.contents = myStory.contents.replace(/\s+$/,"") //removes trailing whitespace in the story
                              if (myStory.paragraphs.length > 0){
                                  myString = "\r"+myString;
                              }
                              myStory.insertionPoints[-1].contents = myString; // adds the Story to the end of the story.
                          }
                          
                          
                          1 person found this helpful
                          • 10. Re: How to ADD A Paragraph?
                            FrankB62 Level 1

                            That does not make sense. A paragraph is an object, and you should be able to create a new paragraph, just like you can create any other object. It is not at all obvious that you create a new object by adding a carriage return to an already existing object.

                             

                            I am used to working with FrameMaker, and there a paragraph is an object which you can create and delete as you like, and you don't create other objects by adding carriage returns, or delete objects by deleting carriage returns. InDesign is indeed frustrating.

                             

                            InDesign needs a paragraphs.Add method. That is the only reasonable way to add paragraphs to a document.

                            • 11. Re: How to ADD A Paragraph?
                              FrankB62 Level 1

                              Quote:

                               

                              "It makes no sense to add a paragraph if there's no text in it."

                               

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

                               

                              Who is talking about paragraphs with no text in them? You have to create an object before you can put anything into it, don't you?

                               

                              The obvious thing would be to create the paragraph object, put text into it and apply a style, and it will of course be empty of contents when it is newly created, just like any other object is.

                              • 12. Re: How to ADD A Paragraph?
                                [Jongware] Most Valuable Participant

                                A paragraph is not a real existing object, as for example a rectangle is. It's more of a handy expression. Other "objects" of this ilk are Characters, Lines, and Words. The base object, however, is a Story.

                                 

                                And you cannot create a Story either out of nothing. You need a text frame first (which, I believe, does something like creating a zero-length story somewhere in memory).

                                 

                                Adding paragraphs to a story is as simple as adding a hard return: \r -- just like adding a Word can be done by first adding a space.