3 Replies Latest reply on May 6, 2010 7:59 AM by Dave Saunders

    [JS] [CS3 onwards] Simplify some code

    Roy Marshall Level 1

      Hi.

       

      I have been using a for loop to find a certain paragraoh by its style name for a while now, and I am wondering if I am using the best approach?

       

      I am using:

       

      for (x=0;x<=myStory.paragraphs.length-1;x++)

           {

           if(myStory.paragraphs[x].appliedParagraphStyle.name == "SOS - Savings")

           {

           code here...

       

       

      but wondering if I can use somethng like:

       

      mySavePrice = myStory.paragraphs.appliedParagraphStyle.name("SOS - Savings");

      Just a thought...
      Cheers
      Roy

        • 1. Re: [JS] [CS3 onwards] Simplify some code
          Fred Goldman Level 3

          It would most likely be a lot faster if you did a findText() instead of looping through all the paragraphs.

          • 2. Re: [JS] [CS3 onwards] Simplify some code
            Dave Saunders Level 4

            Roy,

             

            I sometimes use this approach to processing a story. Usually, I'll have a large switch that operates on the appliedParagraphStyle.name property. But, unfortunately, this:

             

            myStory = app.selection[0].parentStory;
            myPSnames = myStory.paragraphs.everyItem().appliedParagraphStyle.name;
            

             

            Doesn't work. So, instead, I use:

             

            myStory = app.selection[0].parentStory;
            myPstyles = myStory.paragraphs.everyItem().appliedParagraphStyle;
            for (var j = myPstyles.length - 1; j >= 0; j--) {
                 switch (myPstyles[j].name) {
                      case ...
                      case ...
            

             

            etc.

             

            Dave

            • 3. Re: [JS] [CS3 onwards] Simplify some code
              Dave Saunders Level 4

              Now I think about it, when processing a story in this fashion, it doesn't matter a lot whether you go forwards or backwards through the story. I wrote the loop in the example above to go backwards out of habit, but when you're accessing paragraphs by index in the story, as long as you don't change the number of paragraphs, you can loop either way.

               

              Dave