9 Replies Latest reply on Apr 10, 2013 5:36 AM by Trevorׅ

    JS for all styles within a TextFrame

    eboda_snaf Level 1

      hi, is there any JavaScript to get all the paragraph & character styles within a TextFrame?

      thks in advance.. 

       

      cheers 

        • 1. Re: JS for all styles within a TextFrame
          Trevorׅ Adobe Community Professional

          Here's 2 approaches of doing it.

          I would have thought it could be done in 2 lines but ....

           

          Approach 1

           

           

          // by Trevor http://forums.adobe.com/thread/1188585?tstart=0
          var  doc = app.activeDocument, n =0, l,
                  pss = "Paragraph Styles\n************",
                  css =  "\n\nCharacter Styles\n************",
                  tf, ps = [], cs = [], p, c, found;
          tf =  app.activeDocument.textFrames[0], // change to the textFrame you want
          app.findGrepPreferences = null;
          p = doc.paragraphStyles.everyItem().getElements();
          c = doc.characterStyles.everyItem().getElements();
          l = p.length;
          while (l--)
              {
                  app.findGrepPreferences.appliedParagraphStyle = p[l];
                  found = tf.findGrep();
                  if (found.length > 0) 
                      {
                          ps[n++] =  p[l];
                          pss += "\n" + p[l].name;
                       }
                }
          app.findGrepPreferences = null;
          l = c.length; 
          n = 0;
          while (l--)
              {
                  app.findGrepPreferences.appliedCharacterStyle = c[l];
                  found = tf.findGrep();
                  if (found.length > 0) 
                      {
                          cs[n++] =  c[l];
                          css += "\n" + c[l].name;
                       }
                }
          alert(pss + css);
          

           

          Approach 2

           

           

          // by Trevor http://forums.adobe.com/thread/1188585?tstart=0
          var  n,
                  pss = "Paragraph Styles\n************",
                  css =  "\n\nCharacter Styles\n************",
                  tf, ps, cs;
          tf =  app.activeDocument.textFrames[0], // change to the textFrame you want
          
          ps = (tf.paragraphs.length < tf.textStyleRanges.length) ? tf.paragraphs.everyItem().appliedParagraphStyle : tf.textStyleRanges.everyItem().appliedParagraphStyle;
          cs = tf.textStyleRanges.everyItem().appliedCharacterStyle;
          
          
          ps = getUniqueStyles (ps);
          cs = getUniqueStyles (cs);
          
          
          for (n in ps) pss += "\n" + ps[n].name;
          for (n in cs) css += "\n" + cs[n].name;
          alert(pss + css);
          
          function getUniqueStyles (x)
              {
                  var xl = x.length, mo = {};        
                  while (xl--) mo[x[xl].name] = x[xl];
                  return mo; // retuns an object can be converted to a unique array if neaded
               }
          
          1 person found this helpful
          • 2. Re: JS for all styles within a TextFrame
            BEGINNER_X Level 3

            Hi snaf,

             

            Please use this code, may it will helpful for you

             

             

            var myDoc = app.activeDocument

             

            myParaStyles = new Array

             

            myCharStyles = new Array

             

            var myPages = app.activeDocument.pages.item(0)

             

            var myTF = myPages.textFrames.add()

             

            myTF.geometricBounds = [-50, -100, 200, 200]

             

             

            //To get Paragraph Styles

            var myPStyles = app.activeDocument.paragraphStyles.everyItem().getElements()

             

            for (i = 2; i < myPStyles.length; i++) {

                myParaStyles.push(myPStyles[i].name)

            }

             

            myParaStyles = myParaStyles.toString()

             

            myTF.insertionPoints[-1].contents = "ParagraphStyles: \r" + myParaStyles

             

             

            Use the same concept for Character styles also....

             

             

            Thanks

            BEGINNER_X

            1 person found this helpful
            • 3. Re: JS for all styles within a TextFrame
              Vamitul Level 4

              beginner, your code gets all the paragraph styles in the document, regardless of wether they are in use or not, and defenatly not just the ones used in the textframe.

              Trevors methods work just fine, and the second one is beautiful and fast.

              One qestion, Trevor: whay do you check for  tf.paragraphs.length?  Doesn't just plain  ps=tf.textStyleRanges.everyItem().appliedParagraphStyle work?

              • 4. Re: JS for all styles within a TextFrame
                eboda_snaf Level 1

                Thanks all for your help...

                 

                What i'm facing is i could have within a single Text Frame;

                the 1st para could be using parastyle: para1

                2nd using parastyle: para2.. 

                and so forth

                 

                is there a way to get all the paraStyles used in that Text Frame?

                 

                Thanks

                • 5. Re: JS for all styles within a TextFrame
                  TᴀW Adobe Community Professional & MVP

                  Did somebody say two lines? Here's a two-liner for getting all the

                  paragraph styles. Repeat for char styles. Select your text frame and run:

                   

                  var uniqueParas = [], everyPara =

                  app.selection[0].textStyleRanges.everyItem().appliedParagraphStyle;

                  for (i=0; i<everyPara.length; i++) uniqueParas[everyPara[i].name] =

                  everyPara[i];

                   

                  Ariel

                   

                  PS I hope this doesn't get too mangled by the forum software. It tends

                  to replace square brackets with links!

                  1 person found this helpful
                  • 6. Re: JS for all styles within a TextFrame
                    eboda_snaf Level 1

                    Thanks i've modified abit and got the result i wanted.. 

                     

                    everyPara = app.selection[0].textStyleRanges.everyItem().appliedParagraphStyle;

                    for (i=0; i<everyPara.length; i++)

                         alert(everyPara[i].name);

                    • 7. Re: JS for all styles within a TextFrame
                      Trevorׅ Adobe Community Professional

                      @Ariel, Nice one! I'd definitely allocate you the correct here. mine do look a bit on the overkill side!

                       

                      @Vamital,

                       

                      As stated above "a bit on the overkill side!" the length check is to see whether there are more paragraphs or more text range styles. In this case it's completly not necessary BUT if one was iterating through a long document then you would want to take the shortest path.

                       

                      A 1000 page document could have easily 10,000  paragraphs in it but only 15 textStyleRanges in it so it would be much quicker to iterate the textStyleRanges on the other hand each paragraph could have several textStyleRanges and it would be much quicker to iterate the paragraphs.  However on a long document the first script would prove much much faster.


                      The "normal" way to check for appliedParagraphStyles would be to iterate the paragraphs and not the ranges.

                       

                      @ X

                       

                      Congratualtions on you new medal.

                       

                      Regards to all,

                       

                      Trevor

                      • 8. Re: JS for all styles within a TextFrame
                        TᴀW Adobe Community Professional & MVP

                        In my snippet above, I would change everyPara[i].name to everyPara[i].id -- just in case two paras in two different groups have the same name.

                         

                        Ariel

                        • 9. Re: JS for all styles within a TextFrame
                          Trevorׅ Adobe Community Professional

                          Yep, Ditto for me on script 2

                          change while (xl--) mo[x[xl].name] = x[xl];

                          to

                          while (xl--) mo[x[xl].id] = x[xl];