3 Replies Latest reply on Nov 22, 2010 10:10 AM by IT.Coordinator

    How to apply 'Paragraph Styles' into a Script? need help!

    IT.Coordinator Level 1

      Hello,

       

      I'm in Need of a Script for InDesign Cs3. I'm not new to programming. I know VB, C++ and Assmbler. I'm rather new to Javascript though, and just don't know all the syntex commands.

       

      I would like a script that applies a Paragraph style to a document in indesign. But I would like the script ot apply this same paragraph style ot each paragraph on the document. Is there a way to let the script know to stop at the end of the text window, so that it doesnt make the text of the paragraphs exceed the page's size?

       

      I wrote this script so far, It applies the 'Paragraph Style' to one paragraph, but I want it to apply to all the text in the word box on one page at a time. I'm just not sure how to make it apply to more than one paragraph with the style. Basically 'Thursday' and 'November 11' happens only once at the top of the page, but the paragraph style stays the same for every paragraph. 'Thursday' and 'Novmeber 11' are generic, each week it be a different day of the week and date of the given month, is there a way for the script to differ between dates and days?

       

      var myDoc = app.documents[0]
      var mySel = app.selection[0]
      var myHolidayStyle1 = "Holiday"
      //var myHolidayStyle2 = "Normal";

       

      mySel.appliedParagraphStyle = myDoc.paragraphStyles.item (myHolidayStyle1);
      mySel.paragraphs[-1].insertionPoints[-1].paragraphs[0];

       

       

      I'm relatively New to Paragraph Styles too, so I'm hoping that will be simplier to do than to make the script, and maybe that for another prt of the forum, But I' just like to get the script part done. The Paragraph Style would be called "Holiday".I made this Paragraph style after hours of trial and error, now I just need to know how to apply multiple styles to a page and have those styles be consistent with each page. I work for a Newspaper company, so this has to be accurate and work.

       

      This is an example of what the Paragraph header would look like:

       

      Thursday     (present LT std. Black 22pt)

       

      November 11  (Palatino Linotype Bold 10 pt)


      Title of article: text goes here, bla bla bla
      blablablablabl bla bla bla bla bla bla                    (Palatino LT std Roman 9 pt)
      bla bla bla bla bla bla bla bla bla
      bla bla bla bla bla bla. (end of Paragraph)

       


      (next paragraph)
      Title of article: text goes here, bla bla bla
      blablablablabl bla bla bla bla bla bla
      bla bla bla bla bla bla bla bla bla
      bla bla bla bla bla bla.

       

      repeat

       

      repeat

       


      Any help would be greatly appreciated. I would really like get a understand of how it should look like in the script, if someone could help me, that be wonderful.

       

      Thank You.

        • 1. Re: How to apply 'Paragraph Styles' into a Script? need help!
          [Jongware] Most Valuable Participant

          Given a reference to a story, it's easy to address individual paragraphs. You seem to indicate the target story by clicking your text cursor in it, so going from that, you can do this:

           

          mySel = app.selection[0];  // Now pointing to the text cursor
          myStory = mySel.parentStory; // Now we are pointing to the entire story
          
          myStory.paragraphs[0].appliedParagraphStyle = someStyle;
          myStory.paragraphs[1].appliedParagraphStyle = someStyle; // .. etc.
          

           

          But I would like the script ot apply this same paragraph style ot each paragraph on the document.

           

          Easy: loop over all paragraphs in a story by checking its paragraphs.length:

           

          for (loop=0; loop<myStory.paragraphs.length; loop++)
            myStory.paragraphs[loop].appliedParagraphStyle = somestyle;
          

           

          Is there a way to let the script know to stop at the end of the text window, so that it doesnt make the text of the paragraphs exceed the page's size?

           

          Ehm -- you lost me there. Does applying the paragraph style change the amount of text in its frame?

          In any case, the parentTextFrames of a story (and a paragraph, and all other Text items) contain an array of all text frames that are spanned by the story. To check if you "drop out" of a certain frame, you could do something like this (untested):

           

          currTextFrame = app.selection[0].parentTextFrames[0];
          currPar = currTextFrame.paragraphs[0];
          while (currPar && currPar.parentTextFrames[0] == currTextFrame)
          {
            currPar.appliedParagraphStyle = someStyle;
            currPar = currTextFrame.paragraphs.nextItem(currPar);
          }
          
          
          1 person found this helpful
          • 2. Re: How to apply 'Paragraph Styles' into a Script? need help!
            IT.Coordinator Level 1

            so am I to assume that,

             

            myStory.paragraphs[0].appliedParagraphStyle = someStyle;
            myStory.paragraphs[1].appliedParagraphStyle = someStyle; // .. etc.

             

            [0] --- applies to the first line, like 'Thursday' Style

            [1] --- applies to the second line, like 'November 11' Style

            [2] --- could apply to the paragraph line....

             

            Just trying to get a generalized idea of what the 0,1,2,3 all represent in the text box.

            • 3. Re: How to apply 'Paragraph Styles' into a Script? need help!
              IT.Coordinator Level 1

              var myDoc = app.documents[0]
              var mySel = app.selection[0]
              var myStory = mySel.parentStory; // Now we are pointing to the entire story
              var myHolidayStyle1 = "Holiday-Day_of_the_week" // Header 'day of the week'
              var myHolidayStyle2 = "Holiday-Month_and_date" // Header 'month and day'
              var myHolidayStyle3 = "Lead Paragraph" // Lead Paragraph 'default paragraph style'

               

               

               

              myStory.paragraphs[0].appliedParagraphStyle = myHolidayStyle1; //Header 'day of the week' and Month and day.
              myStory.paragraphs[1].appliedParagraphStyle = myHolidayStyle2; // Header 'month of the day'
              myStory.paragraphs[2].appliedParagraphStyle = myHolidayStyle3; // Lead Paragraph 'default paragraph style'
              //.appliedParagraphStyle = myDoc.paragraphStyles.item (myHolidayStyle1);
              //mySel.paragraphs[-1].insertionPoints[-1].paragraphs[0];

               


              //Loops to change all the paragraphs to Paragraph body to ever paragraph (only problem is its limited to the entire page.)
              for (loop=0; loop<myStory.paragraphs.length; loop++)
                myStory.paragraphs[loop].appliedParagraphStyle = myHolidayStyle3;

               

               
              // Something simple to check if text 'drops out' of  a certain text frame. 
              currTextFrame = app.selection[0].parentTextFrames[0];
              currPar = currTextFrame.paragraphs[0];
              while (currPar && currPar.parentTextFrames[0] == currTextFrame) {
                currPar.appliedParagraphStyle = someStyle;
                currPar = currTextFrame.paragraphs.nextItem(currPar);
              }

               


              // Script for starting Spell Check
              var WshShell = app.menus.item(0);
              var WshShell2 = WshShell.submenus.item(1);
              var WshShell3 = WshShell2.submenus.item(1);
              var WshShell4 = WshShell3.menuItems.item(0);
              WshShell4.associatedMenuAction.invoke();

               

               

              Would this work, do I got a pretty generalized idea of how it should be written.

               

              I want this script to apply data that comes in from a export that is imported into Indesign. Basically Holidaystyle1 and 2 are already preset in data from a program called Linear. But the day, month, and date, will change. So I hoping there is a way from the script above to make 1 and 2 be at the top of the page, and style 3 be the paragraph style that leads in the text until the next articles are reach that start with like say 'Friday' and 'November 12', and the routine repeats itself again, until there is no-more data. (**edit** Is there a way to tell the script to put a line break or empty line between Style 0 and style 1?)

               

              I hope this make sense.

               

              I would love for the script to do all the data at once, and at the end, I put a spell check function that checks to make sure the spelling and grammar are correct, before the script finally ends.