6 Replies Latest reply: May 10, 2012 4:46 PM by John Hawkinson RSS

    How do I find character styles applied to a particular paragraph

    frameexpert Community Member

      I have to loop through the paragraphs in my InDesign document, and for each one, see what character styles are applied in the paragraph. For a given paragraph, how do I locate character styles applied to the paragraph? Thank you very much.

       

      Rick Quatro

        • 1. Re: How do I find character styles applied to a particular paragraph
          [Jongware] MVP

          Hmmm, not-so-easy. Thinking out loud:

           

          All "Text" objects in general have a property "appliedCharacterStyle", but inquiring this (and all other properties) when there are a multiple applied to the item you are inquiring -- in your case, per paragraph -- will result in either a (null) value or just pop up an error message.

           

          Try looping over its TextStyleRanges. This has a couple of drawbacks: first, you'll visit each and every sort of override, regardless of how it's applied; and second, I believe this property in its current incarnation suffers from a eeny weeny tiny bug, where the final text style range will happily "leave" the current paragraph and continue into the next one if the formatting is unchanged.

           

          I'd have to try to be sure but I believe even an entirely empty character style will be reported as a separate 'range'. But note that the inverse is not true: not every separate range is a character style of its own; any manual formatting inside a character-styled text will appear to "split" the character styled range in two or more parts.

          • 2. Re: How do I find character styles applied to a particular paragraph
            absqua Community Member

            [Jongware] wrote:

             

            I believe this property in its current incarnation suffers from a eeny weeny tiny bug, where the final text style range will happily "leave" the current paragraph and continue into the next one if the formatting is unchanged.

             

            Anyone had a chance to test for this in CS6? (One can hope!)

             

            Another drawback with textStyleRanges: grep, nested and line style-applied character styles don't start new ones, so you'll also have to query the appliedNestedStyles property for each textStyleRange, and then possibly break each one down further, if you want to catch those.

             

            You might go the other direction and do a findText() for every character style. Doing a find for a character style applied by a grep style is a good way to make InDesign crash, though.

             

            Jeff

            • 3. Re: How do I find character styles applied to a particular paragraph
              frameexpert Community Member

              It looks like the textStyleRanges property is going to work. If I select a paragraph and run this code, it gives me the information that I need. Thank you very much for your help.

               

              Rick

               

              #target indesign

              var doc = app.documents[0];

               

              // Set a variable for the selected paragraph.

              var pgf = app.selection[0];

               

              var textRanges = pgf.textStyleRanges;

              var textRange = null;

              var page = null;

               

              for (var i = 0; i < textRanges.length; i += 1) {

                textRange = textRanges[i];

                alert (textRange.contents);

                alert (textRange.appliedCharacterStyle.name);

                page = textRange.parentTextFrames[0].parentPage;

                alert (page.name);

               

              }

              • 4. Re: How do I find character styles applied to a particular paragraph
                [Jongware] MVP

                You got it.

                 

                To get the handle to an individual paragraph, right from your selection, you don't have to select it in its entirety.

                 

                The following trick I learned from ... (Marc? Tomaxxi? Peter K.?) ... someone here on the forum: to get a list of all objects, whether they occur only once or lots of times, you don't have to store them into an array and test for each new one if it's already in there. Just just an associative array -- an object, in fact -- and have the "key" represent the object. The value is of no importance, actually. Or well, maybe it is you want to count occurrences.

                A quick way to inspect the members of your new object is to use "toSource".

                 

                 

                #target indesign

                var doc = app.documents[0];

                 

                // Set a variable for the selected *paragraph*

                var pgf = app.selection[0].paragraphs[0];

                 

                var textRanges = pgf.textStyleRanges;

                var textRange = null;

                var page = null;

                 

                var charStyleList = {};

                 

                for (var i = 0; i < textRanges.length; i += 1) {

                          charStyleList[textRanges[i].appliedCharacterStyle.name] = true;

                }

                 

                alert (charStyleList.toSource());

                 


                .. Hey, Forum Maintenance, where did the Format Javascript button go!? I was going to use that!

                • 5. Re: How do I find character styles applied to a particular paragraph
                  Larry G. Schneider MVP

                  Jong, you have to go back and edit the message for the advanced options to show before you can use the syntax highlighting. At least this is how it was described on the Illustrator scripting forum.

                  • 6. Re: How do I find character styles applied to a particular paragraph
                    John Hawkinson Community Member

                    Yes, the advanced editor problem is well known. Visit the General > Forum Comments forum to find the long long list of issues compiled by the forum admins. The implication is that this (and many others) will be fixed soonish.