6 Replies Latest reply on Jul 15, 2015 8:21 AM by David_N_2000

    Cycling through paragraph styles script - Help Needed

    David_N_2000

      Hi

       

      I have a large document (potentially 3000 pages), on each page is a graphic of a picture frame and inside each frame is a famous quotation. I need to have the type in each quotation set in a different font so i've setup 10 different paragraph styles. I need a script that will apply 'QuoteStyle1' to the text on the first page, 'QuoteStyle2' to the next page etc, and once 'QuoteStyle10' has been applied the script loops and continues to cycle through the styles applying a different style on each page until it reaches the end of the document.

       

      I've made a start on the script (see attached image) but I think the problem is line 11 - I want to run the script without having to select anything.

       

      Some further info that may or may not be of use - All of the quotes are being pulled in from a data-merge. There's also a name and a colour coming from the data-merge. The background on each picture frame is a different colour and that is created with the a very large 'Rule Below' in a number of other paragraph styles. These are being applied with a 'Find&ReplacebyList' i.e. find 'Red' replace with paragraph style 'ColourRed'. In the script i've tried to skip these 'Colour' paragraph styles (lines 2-9) so it's only the 'Quote' styles that the script effects.

       

      Any help getting this running would be most helpful.Screen Shot 2015-07-15 at 10.01.20.png

        • 1. Re: Cycling through paragraph styles script - Help Needed
          Vamitul Level 4

          Your code seems correct if all your quotes have just one paragraph and all your quote frames are threaded together (Albeit your coding style might use some work). In order to make the script work without preselecting the story, you need a way to identify the said story in some way, and that depends on how your document is set up.

          I do have the feeling, however, that your document is not set up like i first described it, but more like a single, standalone textframe per page. maybe grouped with the graphic frame?

          Maybe you could post a sample of the document?

           

          ps. also, instead of using a screenshot of ESTK, maybe paste the actual code (using the advanced editor of the forum you can find a "code block" widget). I'm just too lazy to type all those paragraph style names.

          • 2. Re: Cycling through paragraph styles script - Help Needed
            David_N_2000 Level 1

            Hi

             

            Thanks for commenting. Yeah the text frames containing the quotes aren't threaded together, they are single stand alone text frames per page. They aren't group though.

             

            Here's a WeTransfer link to download the file. It's only dummy data at the moment but i'll soon have 3000+ records to format.

             

            Thanks again for your help.

            • 3. Re: Cycling through paragraph styles script - Help Needed
              Vamitul Level 4

              First of all congrats for the very creative use of blending modes. I would have never thought about it by myself.

              Now for the script, here are two ways to do it (both take advantage of the fact that you use a BaseQuote paragraph style):

              1) use a grep search:

              var doc=app.activeDocument;
              var baseQ=doc.paragraphStyles.item('BaseQuoteStyle');
              var switchToStyles=[
              'QuoteStyle1',
              'QuoteStyle2',
              'QuoteStyle3',
              'QuoteStyle4',
              'QuoteStyle5',
              'QuoteStyle6',
              'QuoteStyle7',
              'QuoteStyle8',
              'QuoteStyle9',
              'QuoteStyle10'];
              var l=switchToStyles.length;
              app.findGrepPreferences=app.findChangeGrepOptions=null;
              app.findGrepPreferences.appliedParagraphStyle=baseQ;
              app.findChangeGrepOptions.includeMasterPages=false;
              var quotes=doc.findGrep();
              for (var i=0; i<quotes.length; i++){
                quotes[i].appliedParagraphStyle=switchToStyles[i%l];
              }
              

               

              2) cycle through all textFrames:

              var doc=app.activeDocument;
              var baseQ=doc.paragraphStyles.item('BaseQuoteStyle');
              var pags=doc.pages.everyItem().getElements();
              var switchToStyles=[
              'QuoteStyle1',
              'QuoteStyle2',
              'QuoteStyle3',
              'QuoteStyle4',
              'QuoteStyle5',
              'QuoteStyle6',
              'QuoteStyle7',
              'QuoteStyle8',
              'QuoteStyle9',
              'QuoteStyle10'];
              var l=switchToStyles.length;
              for (var p=0;p<pags.length;p++){
                var tf=p.textFrames.everyItem().getElements();
                for (var i=0; i<tf.length;i++){
                if (tf[i].paragraphs[0].appliedParagraphStyle==baseQ){
                tf[i].parentStory.appliedParagraphStyle=switchToStyles[p%l];
                break;
                }
                }
              }
              
              • 4. Re: Cycling through paragraph styles script - Help Needed
                David_N_2000 Level 1

                That's great thanks. No.1 using the GREP search worked just fine. I got an error on No.2 (line 17 - undefined is not an object).

                 

                But i can use the first one, that's perfect!

                 

                The next thing I need to do with this piece of work involves scaling the text on each page to as large a size as it will go within the text box. Some of the quotes may be short and some may be long, so one font size won't fit all. This could be done with this 'Fit Text to Frame' script here Fun With Text Fitting in InDesign | in-tools.com but this script requires a text frame to be selected before running the script. Also I can't open and edit the script. Have you any ideas?

                 

                Thanks again!

                • 5. Re: Cycling through paragraph styles script - Help Needed
                  Vamitul Level 4

                  Second one should read:

                  var doc=app.activeDocument;
                  var baseQ=doc.paragraphStyles.item('BaseQuoteStyle');
                  var pags=doc.pages.everyItem().getElements();
                  var switchToStyles=[
                  'QuoteStyle1',
                  'QuoteStyle2',
                  'QuoteStyle3',
                  'QuoteStyle4',
                  'QuoteStyle5',
                  'QuoteStyle6',
                  'QuoteStyle7',
                  'QuoteStyle8',
                  'QuoteStyle9',
                  'QuoteStyle10'];
                  var l=switchToStyles.length;
                  for (var p=0;p<pags.length;p++){
                    var tf=pags[p].textFrames.everyItem().getElements();
                    for (var i=0; i<tf.length;i++){
                    if (tf[i].paragraphs[0].appliedParagraphStyle==baseQ){
                    tf[i].parentStory.appliedParagraphStyle=switchToStyles[p%l];
                  break;
                    }
                    }
                  }
                  

                   

                  and it is better suited to your needs.

                  As for fitting the text:

                  var doc=app.activeDocument;
                  var baseQ=doc.paragraphStyles.item('BaseQuoteStyle');
                  var pags=doc.pages.everyItem().getElements();
                  var switchToStyles=[
                  'QuoteStyle1',
                  'QuoteStyle2',
                  'QuoteStyle3',
                  'QuoteStyle4',
                  'QuoteStyle5',
                  'QuoteStyle6',
                  'QuoteStyle7',
                  'QuoteStyle8',
                  'QuoteStyle9',
                  'QuoteStyle10'];
                  var l=switchToStyles.length;
                  for (var p=0;p<pags.length;p++){
                    var tf=pags[p].textFrames.everyItem().getElements();
                    for (var i=0; i<tf.length;i++){
                    if (tf[i].paragraphs[0].appliedParagraphStyle==baseQ){
                    tf[i].parentStory.appliedParagraphStyle=switchToStyles[p%l];
                              while (!tf[i].overflows){
                                  tf[i].parentStory.pointSize++;
                                  }
                              tf[i].parentStory.pointSize--;
                              break;
                    }
                      
                    }
                  }
                  
                  • 6. Re: Cycling through paragraph styles script - Help Needed
                    David_N_2000 Level 1

                    BOOM - that last piece of script is perfect