3 Replies Latest reply on Mar 1, 2013 6:34 AM by Jump_Over

    Create reference up to a given character style

    SimonLinden Level 1

      I have some text that I need to sort in subsections so that I can reference them through script one section at a time. My idea is to create instances of the Paragraphs class for each section, but I can't figure out how to do this.  Each new section starts with a chapter number that can be identified by character style 'c-number'. The first paragraph of each new chapter also has a unique paragraph style 'p-new-chapter', so it should be be simple to separate the chapters by including all the paragraphs from the first 'p-new-chapter' to the last paragraph up to (but not including) the next 'p-new-chapter', or using the character style if that's easier. But I can't figure out how to do this.

       

      I've found the property called insertionPoint and I was thinking that if I create a reference to the first insertionPoint of a chapter number and then lower its index by '1', that should put me in the last paragraph of the chapter. So I think I know how to create references to the first and the last paragraph of each chapter. It shouldn't be to hard to do the rest. Isn't there some function that would let me select the next paragraph kind of like this:

       

      curParagraph.nextParagraph();
      

       

      If a function like that exists then the loop  below should be in the right direction. There doesn't seem to be a function in Paragraphs to add instances to it though. How would I do that (I call it append() in the code below).

       

      while(true){
           myParagraphs.append(curParagraph);
           curParagraph = curParagraph.nextParagraph();
           if(curParagraph == lastParagraph){
                break;
           }
      }
      
        • 1. Re: Create reference up to a given character style
          Jump_Over Level 5

          Hi,

           

          My idea is:

          - use 'p-new-chapter' as a textFindPreferences.appliedParagraphStyle set;

          - story.findText() will create an array of those paragraphs;

          - you could iterate through them and catch a "start" and "end" paragraphs of section.

          - if start paragraph would be a story.findText()[1] ==> end paragraph would be story.paragraphs.previousItem(story.findText()[2]);

          - so your section could be a part of story defined by
          story.paragraphs.itemByRange(start,end).texts[0]

           

          rgds

          1 person found this helpful
          • 2. Re: Create reference up to a given character style
            SimonLinden Level 1

            Thanks, it really helped me. Especially how to adress the whole stack of paragraphs through the story (I use parentStory because story isn't supported).

             

            Now I just think my way of getting a paragraph is probably not the best. I do it by creating insertionPoins somewhere in that paragraph and then calling the firstItem() of paragraphs of that insertionPoint.

             

            var insPoint = foundChapterNumbers[cNum].insertionPoints[0];

            firstParagraph = insPoint.paragraphs.firstItem();

             

            insPoint = foundChapterNumbers[cNum+1].insertionPoints[0];

            lastParagraph = insPoint.parentStory.paragraphs.previousItem(insPoint.paragraphs.firstItem());

             

            var chapter1 = insPoint.parentStory.paragraphs.itemByRange(firstParagraph,lastParagraph);

             

            I shouldn't complain since it's working but I would have thought I could just call foundChapterNumbers[cNum].parent or something and that that would give me the current paragraph.

            • 3. Re: Create reference up to a given character style
              Jump_Over Level 5

              Hi,

               

              This code iterate through paragraphs and alert each section beginning:

              // some textFrame with proper story is selected
              // "p-new-chapter" is a first paragraph's style of each section
              //
              mFound = sectionFinder (app.selection[0], app.activeDocument.paragraphStyles.item("p-new-chapter") );
              
              for (var k = 0; k<mFound.length; k++) {
                  startPara = mFound[k].paragraphs[0];
                  if ( k == mFound.length - 1)
                      lastPara = myStory.paragraphs[-1];
                  else
                      lastPara = myStory.paragraphs.previousItem(mFound[k+1].paragraphs[0]);
              
                  currSection = myStory.paragraphs.itemByRange(startPara,lastPara).texts[0];
                  // first line of each section is alerted. You could procces another code here 
                  alert (currSection.lines[0].contents);
                  }
              
              function sectionFinder (frame, paraStyle) {
                  myStory = frame.parentStory;
                  app.findTextPreferences = null;
                  app.findTextPreferences.appliedParagraphStyle = paraStyle;
                  return myStory.findText();
              }
              

               

              so you do not have to create insertionPoint to catch paragraph

               

              rgds