7 Replies Latest reply on Mar 21, 2013 5:55 AM by Eugene Tyson

    Alternate Object Styles?

    Eugene Tyson Adobe Community Professional & MVP

      I have a document that requires the background colour changes per text frame.

       

      I was thinking an alternating object style would work best. But I can't figure out how to do it.

       

      I cannot use tables for this project!

       

      The layout is generally like this. The Text Frames will be threaded - and they must alternate colours each time a new heading appears.

       

      Has anyone come across a script like this before?

       

       

      1 Header
      Lorem ipsum dolor sit amet, consectetur adipiscing elit. Mauris a malesuada augue. Mauris blandit aliquet magna, sed sollicitudin justo aliquet vel. Sed dignissim dapibus sapien, ut varius tortor ultricies nec. Nam accumsan nulla eu leo ornare sed iaculis risus imperdiet. Donec nec pharetra lectus. Etiam at sollicitudin erat. Phasellus a consectetur magna. Nulla a nibh in mi tempus congue. Ut tempus, lacus id lacinia elementum, turpis urna pellentesque justo, vitae auctor dui arcu vitae eros. Nam eleifend blandit lorem facilisis pulvinar.Lorem ipsum dolor sit amet, consectetur adipiscing elit. Mauris a malesuada augue. Mauris blandit aliquet magna, sed sollicitudin justo aliquet vel. Sed dignissim dapibus sapien, ut varius tortor ultricies nec. Nam accumsan nulla eu leo ornare sed iaculis risus imperdiet. Donec nec pharetra lectus. Etiam at sollicitudin erat. Phasellus a consectetur magna. Nulla a nibh in mi tempus congue. Ut tempus, lacus id lacinia elementum, turpis urna pellentesque justo, vitae auctor dui arcu vitae eros. Nam eleifend blandit lorem facilisis pulvinar.

       

      2 Header
      Lorem ipsum dolor sit amet, consectetur adipiscing elit. Mauris a malesuada augue. Mauris blandit aliquet magna, sed sollicitudin justo aliquet vel. Sed dignissim dapibus sapien, ut varius tortor ultricies nec. Nam accumsan nulla eu leo ornare sed iaculis risus imperdiet. Donec nec pharetra lectus. Etiam at sollicitudin erat. Phasellus a consectetur magna. Nulla a nibh in mi tempus congue. Ut tempus, lacus id lacinia elementum, turpis urna pellentesque justo, vitae auctor dui arcu vitae eros. Nam eleifend blandit lorem facilisis pulvinar.Lorem ipsum dolor sit amet, consectetur adipiscing elit. Mauris a malesuada augue. Mauris blandit aliquet magna, sed sollicitudin justo aliquet vel. Sed dignissim dapibus sapien, ut varius tortor ultricies nec. Nam accumsan nulla eu leo ornare sed iaculis risus imperdiet. Donec nec pharetra lectus. Etiam at sollicitudin erat. Phasellus a consectetur magna. Nulla a nibh in mi tempus congue. Ut tempus, lacus id lacinia elementum, turpis urna pellentesque justo, vitae auctor dui arcu vitae eros. Nam eleifend blandit lorem facilisis pulvinar.

       

      3 Header
      Lorem ipsum dolor sit amet, consectetur adipiscing elit. Mauris a malesuada augue. Mauris blandit aliquet magna, sed sollicitudin justo aliquet vel. Sed dignissim dapibus sapien, ut varius tortor ultricies nec. Nam accumsan nulla eu leo ornare sed iaculis risus imperdiet. Donec nec pharetra lectus. Etiam at sollicitudin erat. Phasellus a consectetur magna. Nulla a nibh in mi tempus congue. Ut tempus, lacus id lacinia elementum, turpis urna pellentesque justo, vitae auctor dui arcu vitae eros. Nam eleifend blandit lorem facilisis pulvinar.Lorem ipsum dolor sit amet, consectetur adipiscing elit. Mauris a malesuada augue. Mauris blandit aliquet magna, sed sollicitudin justo aliquet vel. Sed dignissim dapibus sapien, ut varius tortor ultricies nec. Nam accumsan nulla eu leo ornare sed iaculis risus imperdiet. Donec nec pharetra lectus. Etiam at sollicitudin erat. Phasellus a consectetur magna. Nulla a nibh in mi tempus congue. Ut tempus, lacus id lacinia elementum, turpis urna pellentesque justo, vitae auctor dui arcu vitae eros. Nam eleifend blandit lorem facilisis pulvinar.

       

      4 Header
      Lorem ipsum dolor sit amet, consectetur adipiscing elit. Mauris a malesuada augue. Mauris blandit aliquet magna, sed sollicitudin justo aliquet vel. Sed dignissim dapibus sapien, ut varius tortor ultricies nec. Nam accumsan nulla eu leo ornare sed iaculis risus imperdiet. Donec nec pharetra lectus. Etiam at sollicitudin erat. Phasellus a consectetur magna. Nulla a nibh in mi tempus congue. Ut tempus, lacus id lacinia elementum, turpis urna pellentesque justo, vitae auctor dui arcu vitae eros. Nam eleifend blandit lorem facilisis pulvinar.Lorem ipsum dolor sit amet, consectetur adipiscing elit. Mauris a malesuada augue. Mauris blandit aliquet magna, sed sollicitudin justo aliquet vel. Sed dignissim dapibus sapien, ut varius tortor ultricies nec. Nam accumsan nulla eu leo ornare sed iaculis risus imperdiet. Donec nec pharetra lectus. Etiam at sollicitudin erat. Phasellus a consectetur magna. Nulla a nibh in mi tempus congue. Ut tempus, lacus id lacinia elementum, turpis urna pellentesque justo, vitae auctor dui arcu vitae eros. Nam eleifend blandit lorem facilisis pulvinar.
        • 1. Re: Alternate Object Styles?
          [Jongware] Most Valuable Participant

          Eugene Tyson wrote:

           

          The layout is generally like this. The Text Frames will be threaded - and they must alternate colours each time a new heading appears.

           

          It should be easy to script.

           

          Do all new text frames start with a new heading? Or can texts under a single heading run over multiple text frames (and thus have the same text frame color)?

           

          Do you prefer a background color change per frame, assigned ad hoc with a Swatch, or would you rather have an Object Style assigned?

          • 2. Re: Alternate Object Styles?
            [Jongware] Most Valuable Participant

            Um, I guess what I meant is: if every text frame starts with a heading, I don't have to check "headings" -- then I can simply iterate over the text frames in order and alternate the color/object style. Way easier.

             

            Alternatively, I can iterate over the text frames in order, check if it contains a header, and *then* toggle the color if it does.

             

            The behavior is different but you would only notice so if (1) you have a heading where the following text spans more than one frame, and/or (2) you have text frames without a heading.

            • 3. Re: Alternate Object Styles?
              Eugene Tyson Adobe Community Professional & MVP

              Currently I have the Heading Style to start on a new page. This bascially is a story "Heading" followed by any number of paragraphs

               

              So I can get each "story" to start in a new Text Frame. But I have to manually apply the object style to make one dark shade and one a light shade.

               

              I have to get each story to fit under each other, currently I will apply a spacing of 2mm between each text frame containing a story.

               

              So a story may end up threading onto the next page, where it needs the same colour text frame.

               

               

              So I guess I'd need the Object Style applied wherever a Heading style appears, and only the next Object Style applied where a new heading begins.

               

              Basically Apply "Object Style 1" where "Heading 1" starts for every frame until "Heading 1" > then apply Object Style 2 > repeat until end.

              • 4. Re: Alternate Object Styles?
                [Jongware] Most Valuable Participant

                Okay, here we go. The osList array contains the list of Object Style names -- you can put as many as you want in there, the used styles will be rotated automatically. The variable pstyle contains the name of the toggle paragraph style -- here it's "Heading".

                 

                Click your text cursor inside any of the linked text frames so it knows where to start, then double-click the script to run. Enjoy!

                 

                var osList = [
                  "LightBlue",
                  "DarkBlue"
                ];
                 
                var pstyle = app.activeDocument.paragraphStyles.item("Heading");
                var current_os = 0;
                
                 if (app.documents.length != 0 && app.selection != 0 && app.selection[0].hasOwnProperty("baseline"))
                {
                  story = app.selection[0].parentStory;
                  // iterate over text containers
                  for (i=0; i<story.textContainers.length; i++)
                  {
                    // test if there is a paragraph style "Heading" in this frame
                    styles = story.textContainers[i].paragraphs.everyItem().appliedParagraphStyle;
                    for (j=0; j<styles.length; j++)
                    {
                     if (styles[j] == pstyle)
                     {
                      current_os = (current_os+1) % osList.length;
                      break;
                     }
                    }
                    story.textContainers[i].applyObjectStyle(app.activeDocument.objectStyles.item(osList[current_os]));
                  }
                }
                
                
                • 5. Re: Alternate Object Styles?
                  Eugene Tyson Adobe Community Professional & MVP

                  Brilliant! Thanks for that it works great!!!

                   

                  One glitch that I didn't mention and didn't realise it would affect it.

                   

                  The first page of this is a single column, and it has a different object style.

                   

                  The rest of the frames throughout are 2 column.

                   

                  When I run the script (even by selecting the 2nd heading) it goes back to the 1st heading and applies the Object Style for 2 columns.

                   

                  This pushes the text over the page - therefore giving me 2 light green frames in a row.

                   

                   

                  Object styles go like

                  1. light green - first box
                  2. Dark Green
                  3. Light Green

                   

                  with 2 and 3 alternating.

                  • 6. Re: Alternate Object Styles?
                    [Jongware] Most Valuable Participant

                    Easiest would be to make it *ignore* the very first linked frame . All that it takes is changing this line

                     

                    for (i=0; i<story.textContainers.length; i++)

                     

                    to this

                     

                    for (i=1; i<story.textContainers.length; i++)

                     

                    Is that close enough?

                    • 7. Re: Alternate Object Styles?
                      Eugene Tyson Adobe Community Professional & MVP

                      That is just beautiful! Thank you!!!