14 Replies Latest reply on Dec 13, 2011 9:08 AM by FrankB62

    How to create paragraphs in VBscript

    FrankB62 Level 1

      Hi,

       

      I am new to scripting in InDesign, and I am having trouble figuring out the correct way of creating paragraphs in a document and appyuing styles to them.

       

      I have read the manual, but I cannot find anything on how to create paragraphs. All I see is adding text to text frames, stories, and existing paragraphs.

       

      What I am after is something like this:

       

      Set P = myStory.Paragraphs.Add

      P.Contents = "This is a header"

      P.ApplyParagraphStyle myHeaderStyle

       

      Set P = myStory.Paragraphs.Add

      P.Contents = "This is normal text"

      P.ApplyParagraphStyle MyTextStyle

       

      This obviously doesn't work, as there is no Add method on the Paragraphs collection, but how does one do it then?

        • 1. Re: How to create paragraphs in VBscript
          FrankB62 Level 1

          Never mind.

           

          I just did a test on creating paragraphs and applying styles to them one at a time (same style on all paragraphs to make it simple), and it runs extremely slow, even with screen updating turned off. This is no way to create documents.

           

          I am going to make a proper program instead, which creates IDML-format files that can be imported into InDesign. This scripting crap is useless for anything other than simple utilities.

          • 2. Re: How to create paragraphs in VBscript
            [Jongware] Most Valuable Participant

            To quote Vincent, "That's a bold statement."

             

            In Javascript, I can create an entire story of plain text and dump that into a text frame, and it'll take any default paragraph formatting I set just before dumping. Then, I can simply address any other paragraph I want to change the style of individual ones.

             

            Here is one link that shows off what a well-written script can do in the hands of someone who knows what he's dealing with: http://www.indiscripts.com/post/2010/01/wordalizer-a-tribute-to-wordle

            • 3. Re: How to create paragraphs in VBscript
              FrankB62 Level 1

              But that is useless. If you just dump a lot of text into a textframe, and it creates a bunch of paragraphs with default styling, how do you know which styles to apply to which paragraphs afterwards? You do not know what are titles, subtitles, body text, indended text etc. Each paragraph needs to be styled as it is put into the document, and so do character styles for that matter.

               

              I fail to see how building documents with InDesign scripts is in any way practical. It is another matter with the IDML-format, because here paragraphs are indeed objects which you have full control over. Why that isn't reflected in InDesign is beyond me.

              • 4. Re: How to create paragraphs in VBscript
                absqua Level 4

                It is another matter with the IDML-format, because here paragraphs are indeed objects which you have full control over. Why that isn't reflected in InDesign is beyond me.

                 

                In idml paragraphs are runs of text separated by <Br/>s, exactly as they are in InDesign. Which isn't to say creating them in idml won't be quicker than doing it via script in InDesign. It probably will be.

                • 5. Re: How to create paragraphs in VBscript
                  FrankB62 Level 1

                  No they are not. In IDML paragraphs have their own XML tags. I can write the starting-tag (including its style as an attribute) into a file, then I can write the text contents into the file, and then the closing tag. Where is the equivalent of this in scripting? How do I get a handle on the paragraph that I want to style and put text into when I am not in charge of creating that paragraph myself?

                   

                  Perhaps an example would be in place. The following is a simple file, which I want to convert into an InDesign Document. First parameter is the name of the paragraph style, and the second parameter is the text:

                   

                  "Chapter","Chapter one"

                  "Title","How to make bread"

                  "Subtitle","Making dough"

                  "Text","Mix water and flour in a bowl"

                  "IndentedText","Stir content of the bowl"

                  "IndentedText","Put dough on table and form it into a bread"

                  "Subtitle","Baking the bread"

                  "Text","Put the bread in the oven"

                  "IndentedText","Bake for an appropriate amount of time"

                   

                  If I now want to loop through this file, reading the two parameters into variables, and place the content into a text frame containing a paragraph for each pair with the correct styling, how do I go about doing that? In IDML it would be dead simple, because then it is just to write the appropriate XML into the output file for each pair, but in VBscript I don't see how it is done.

                  • 6. Re: How to create paragraphs in VBscript
                    [Jongware] Most Valuable Participant

                    Are you asking us to defend your choice for IDML and against VBS? You should use whatever you are happy with.

                    • 7. Re: How to create paragraphs in VBscript
                      FrankB62 Level 1

                      No, I am asking how to create and style paragraphs in VBscript.

                       

                      I may want to use VBscript instead of IDML to minor tasks which doesn't involve creating an entire catalog.

                       

                      How would you do the above example in VBscript? It may be simple, but I just don't know how. I have been looking through reference manuals and example scripts, but nowhere have I seen an example of how to do it. Putting text into a document and styling each paragraph should after all be the most fundamental thing to do.

                      • 8. Re: How to create paragraphs in VBscript
                        absqua Level 4

                        There is a ParagraphStyleRange element in idml, but no Paragraph element. You can generate the schema yourself and have a look, if you like (sorry—JavaScript; don't know VB):

                         

                        var folder = Folder(Folder.desktop + "/idml-schema/package");
                        folder.create();
                        app.generateIDMLSchema(folder, true);
                        

                         

                        Look in the Story.rnc file.

                         

                        A ParagraphStyleRange element can contain one or more paragraphs, which, as Jongware and others pointed out in the other thread, do not exist as real objects, but are a sort of convenience for acting on units of text. Try this in the ESTK on a document with some text in it:

                         

                        var doc = app.activeDocument;
                        var graph = doc.stories[0].paragraphs[0];
                        $.writeln(graph.getElements()[0].toSpecifier());
                        

                         

                        See how the paragraph is defined, not by name or id, but by a character range? Does that clarify things at all?

                         

                        If you're reading in paragraph contents and styles from a file, as your example suggests, there are a number of things you can look at: editing idml, generating tagged text to import, generating xml to import (mapping styles to  tags) or "straight" scripting. Without knowing more about your project, I can't recommend one over the others.

                         

                        Code sample for the scripting approach:

                         

                        var doc = app.activeDocument,
                            story = doc.stories[0],
                            pairs = [],
                            f = File("~/Desktop/paragraphs.txt"),
                            i, l;
                        
                        if(f.open('r')){
                            while(!f.eof){
                                pairs[pairs.length] = f.readln().replace(/\"/g, "").split(",");
                            }
                            f.close();
                        }
                        
                        for(i = 0, l = pairs.length; i < l; i++){
                            if(i > 0){
                                story.insertionPoints[-1].contents = "\r";
                            }
                            story.insertionPoints[-1].contents = pairs[i][1];
                            story.paragraphs[-1].applyParagraphStyle(doc.paragraphStyles.item(pairs[i][0]), true);
                        }
                        

                         

                        Again, I don't know VB; maybe someone who does will chip in. The mechanics won't be any different. Assumes you have a document open with an empty text frame and that all the styles you're calling out in your file exist in the document.

                        • 9. Re: How to create paragraphs in VBscript
                          FrankB62 Level 1

                          Ah, thanks a lot. That is exactly what I was after. I didn't know about the insertionPoints[-1] to insert text at the end.

                           

                          I know that it is a range in IDML, but still, you create a tag and have control over its content and styling. This scripting way is sort of backwards, because you first put in some text, and then you locate whatever "object" has been created at the bottom. It is not at all intuitive.

                           

                          PS: The javascript is fine. I have no problem translating that into VBscript.

                          • 10. Re: How to create paragraphs in VBscript
                            absqua Level 4

                            Glad to help.

                             

                            It is not at all intuitive.

                             

                            I'll give you that much.

                            • 11. Re: How to create paragraphs in VBscript
                              John Hawkinson Level 5

                              Several comments.

                              First of all, I think you should probably be generating an InDesign Tagged Text file and importing it into your document. This is less complex than IDML and much easier to manipulate. If you generate IDML, then you need a lot of extra hair, and careful positioning of text frames on pages and whatnot. I suppose you can get around some of that by generating a Snippet/IDMS file, but I'd say IDTT is easier.

                               

                              I fail to see how building documents with InDesign scripts is in any way practical. It is another matter with the IDML-format, because here paragraphs are indeed objects which you have full control over. Why that isn't reflected in InDesign is beyond me.

                              Building comples stories in ID scripting isn't as convenient. But building complex documents is. They're rather different. But while it might not be convenient to do so for stories, it's certainly not "impractical." All you need to do is write some methods to create paragraphs in the ways that you specify. But it's probably not worth the bother since you can use IDTT or IDML or INX or even InDesign's XML import.

                               

                              Jeff (absqua) wrote:

                              In idml paragraphs are runs of text separated by <Br/>s, exactly as they are in InDesign.

                              Err, tha's not right. IDML seperates paragraphs with <Br/>s but InDesign doesn't. InDesign seperates them with paragraph breaks, there is no <Br/> tag that you would see inside InDesign's scripting DOM representation or it's raw INDD file format.

                               

                              I know that it is a range in IDML, but still, you create a tag and have control over its content and styling. This scripting way is sort of backwards, because you first put in some text, and then you locate whatever "object" has been created at the bottom. It is not at all intuitive.

                              The scripting way is really modelled on how you do it if you are typing in InDesign.

                              And note that you need not do it the above way. You can set the paragraph style for a paragraph before you add the text to it. It will apply the paragraph style of the story (if empty) or of the last text in the story (if non-empty) to the next paragraph, just as if you were typing.

                               

                              So from a practical perspective, you might put a \n at insertionPoints[-1] and style it with your paragraph style, and then add text.

                               

                              I think I would still use InDesign Tagged Text for this, but I guess it depends on what is generating your input file.

                              • 12. Re: How to create paragraphs in VBscript
                                FrankB62 Level 1

                                My plan is not to build complex documents. I have much expecience with creating books and catalogs in FrameMaker, and the way I do it is to put everything into one single flow that runs through the entire document. I am not fiddeling around with placing text frames, images or tables at a precise locations. I let the flow determine where everything gets placed, mostly by using anchored objects and by attributes on paragraph styles. This is not very hard to do in IDML, especially as I can just create an example in InDesign, export it, and then se how it is made.

                                 

                                Now, I do not know the Tagged Text format, but what I need is a complete representation of everything that can be done in InDesign. I am not going to build my software on something that gives me limitations later on, and that is why I am thinking about IDML.

                                 

                                Another thing is how fast it runs. Creating a document externally, and then importing it, is so much faster than building it paragraph by parargaph via scripting. I tried that with 100 pages of "Hello World", and it is a pain (this is with screen updating turned off). I usually generate whole catalogs containing 600-700 pages with thousands of pictures and tables in a matter of minutes. Just for the record, I don't use FrameMaker's API to create documents either, because it is equally slow. Here I use the MIF format, which is equivalent to IDML.

                                 

                                The only thing that is actually keeping from just doing it in IDML, is that it is a zipped file, which seems cumbersome to work with, but perhaps that is the best solution after all.

                                 

                                The thing that will be generating my input file is a program I write myself. I don't use any standard software for this, I write everything myself, because then I have no limitations. That is really the key point for me, that I can create InDesign documents which no limitations whatsoever.

                                 

                                Sorry if I get a bit off the track here, but as I am new to this, I am trying to figure out the best way to do things, and scripting just seemed so obvious to me now that it is there. I needed to be able to create at least a simple document with paragraphs in order to see how it runs, and determine if this is the way forward for me. It probably is in some cases, but I doubt it will stand up to creating heavy catalogs.

                                • 13. Re: How to create paragraphs in VBscript
                                  John Hawkinson Level 5

                                  put everything into one single flow that runs through the entire document. I am not fiddeling around with placing text frames, images or tables at a precise locations. I let the flow determine where everything gets placed, mostly by using anchored objects and by attributes on paragraph styles. This is not very hard to do in IDML, especially as I can just create an example in InDesign, export it, and then se how it is made.

                                  Perhaps I'm missing something, but I'm not aware that you can do this solely in IDML. You can put all your content into a single <Story> tag and you can place the first n frames of the Story on pages, but as far as I'm aware you have  to do so explicitly, with no way to determine the number of pages your Story will consume  (n) and no clear way to avoid overset text. Now, this isn't a huge deal when you combine it with scripting -- you can simply autoflow the overset from a text frame on the first page. So perhaps it's not worth worrying about if you're not trying to be an IDML purist.

                                   

                                  The only thing that is actually keeping from just doing it in IDML, is that it is a zipped file, which seems cumbersome to work with, but perhaps that is the best solution after all.

                                  There are a number of unzipped IDML variants, including IDMS, the snippet format. I wouldn't be surprised if you could convince InDesign to read an unzipped IDML file somehow...

                                   

                                  The thing that will be generating my input file is a program I write myself. I don't use any standard software for this, I write everything myself, because then I have no limitations. That is really the key point for me, that I can create InDesign documents which no limitations whatsoever.

                                  "No limitations" is pretty broad. But if you need more than just text formatting, such as anchored objects, then yes, Tagged Text will be insufficient for you.

                                   

                                  Sorry if I get a bit off the track here, but as I am new to this, I am trying to figure out the best way to do things, and scripting just seemed so obvious to me now that it is there. I needed to be able to create at least a simple document with paragraphs in order to see how it runs, and determine if this is the way forward for me. It probably is in some cases, but I doubt it will stand up to creating heavy catalogs.

                                  Well, scripting will certainly "stand up" but there may be better performance from other approaches.

                                  I think the ID docs would encourage you to look at ID's XML import, which does handle anchored objects, but I think if you are comfortable generating IDML that is probably a better solution as it gives you vastly better control, though at the price of substantially more compexity.

                                  • 14. Re: How to create paragraphs in VBscript
                                    FrankB62 Level 1

                                    I have done some tests with IDML previously, and it is actually possible to put all contents into one story element, no matter how many pages it spans. When you import it, InDesign simply generates the pages necessary. The overflow from one page to the next can be set up on the master pages.

                                     

                                    Now that you mention IDMS (which I haven't tried yet), it might be a better solution. If my goal is to have only one story spanning the entire document, would it be possible to have one IDMS file, which I can import into this story? And is it a true representation of everything like IDML is?

                                     

                                    When I say "no limitations" and "true representation", I mean that I want to be able to have access to everything that is possible to do in an InDesign document. IDML is like this. If you export an InDesign document to IDML, and then import it again, you have the exact same result, so there is nothing you can't do. Everything can be represented in IDML, and that is what I want. Not some amputated import format, that is only able to represent a limited amount of information. I don't want to be in a situation where I can't do what I need to do: Making a cross reference to a specific product in another document for instance, or spanning cells within tables, or marking text for running headers, etc. And anchored objects are an absolute must.