4 Replies Latest reply on Sep 28, 2015 2:13 AM by devsel



      Hi All,

      I'm new in Indesign scripting.


      How to find the nested style applied characters, then check overrides. Finally, get the report from overrides of nested characters (page number with content).


      Can you please help about this.






      Screen shot 2015-09-14 at 8.34.44 PM.png








      Screen shot 2015-09-14 at 8.36.05 PM.png


      Advance thanks,


        • 1. Re: Nested+overrides
          Marc Autret Level 5

          Hi Devsel,


          IMHO there would be easier ways to get started with InDesign scripting than working with nested styles and overrides—you should first study how regular styles work.




          Basically we have to deal with both paragraph attributes and character attributes. What you seem to focus on is character attributes overrides. But as paragraph styles also contain character attributes, a full report of what happens to some portion of text has to include, I'm afraid, the paragraph style level. Here is a recap of those cascading effects which affect character attributes:


          1. Paragraph style level (incl. inheritance mechanism, cf basedOn property).

          2. Manually edited paragraph attributes (i.e. "paragraph overrides," which may include nested style overrides!)

          3. Nested styles (i.e. stack of automated character styles dictated by the current paragraph attributes.)

          4. Manually applied—not [None]—character style (incl. inheritance mechanism, cf basedOn property.)

          5. Manually edited character attributes (which override the current character style.)


          Given a piece of Text, its textStyleRanges collection allows to identify discontiguities at levels 4 and 5, that is, manually applied changes regarding either character style or attributes. (Note however that a character style might be 'empty' relative to its inherited attributes, meaning that no actual change is done in terms of visible properties, although this still is reported as a range break.)


          Now, about nested styles (level 3), see them as a stack of pointers that involve existing character styles in an automated way (from the paragraph level.) Technically nested styles are distributed in three collections: NestedStyles, NestedLineStyles, and NestedGrepStyles. Given a paragraph (or a paragraph style) P, you need to inspect both P.nestedStyles, P.nestedLineStyles, and P.nestedGrepStyles to have a complete view of all nested styles defined in P, which does not mean that the underlying character styles are all actually applied.


          [Note: As nested styles are managed as paragraph attributes, you can see them in fact from any text portion inside a paragraph, but do not inspect them from a larger portion, e.g. a Story, because you will then lose specific data.]


          Since changes resulting from nested styles DO NOT APPEAR as text style ranges, they do not count as overrides, so the crucial question is to identify pieces of text that undergo the effects of some nested style(s). Here we have a useful property, myText.appliedNestedStyles, which returns an array of all character styles that actually act on myText as a result of nested/nestedLine/nestedGrep-style mechanism. But this property is reliable only if myText is a 'uniform' range regarding the nested styles applied. If you select a larger portion, results won't be relevant.


          To determine nested styled texts that also undergo manual overrides (level 5), you need to extract from the overridden text style ranges the uniform parts which have a non-empty appliedNestedStyles array. Problem is, overridden ranges are not easy to identify because the styleOverridden property does not make any distinction between paragraph-level and character-level overrides! That's the deepest issue.


          So, unfortunately, I have no simple solution so far. We can get the style ranges and select those meeting the condition styleOverridden==true, but I don't know how to easily exclude the 'good boys' from there... (A deep attribute compare routine could be required for a perfect solution)

          The draft code I suggest is just an approximation, assuming that paragraph styles are properly applied and do not provide misleading overrides:


          // ===============================
          var story = app.selection[0].parentStory; // assuming some text is selected
          var ranges = story.textStyleRanges,
              nr = ranges.length, r,
              t, a, i, iMin, iMax, cur, s,
              res = [];
          // Loop in the style ranges
          // ---
          for( r=0 ; r < nr ; ++r )
              t = ranges[r];
              if( !t.styleOverridden || !t.appliedNestedStyles.length ) continue;
              t = t.characters;
              if( !t.length ) continue;
              // Inspect this range (regarding nested styles.)
              // ---
              a = t.everyItem().appliedNestedStyles;
              iMax = a.length-1;
              cur = a[iMin=0].join('|');
              for( i=1 ; i <= iMax ; ++i )
                  s = a[i].join('|');
                  if( s == cur ) continue;
                  cur && res.push(t.itemByRange(iMin,i-1).getElements()[0]);
                  iMin = i;
                  cur = s;
              cur && res.push(t.itemByRange(iMin,iMax).getElements()[0]);
          // Show the results as contents.
          // ---
          i = res.length;
          while( i-- ){ res[i]=res[i].contents }
              "The nested-styled text overrides should be in this array:\r\r\r" +


          Hope that can help.




          • 2. Re: Nested+overrides
            devsel Level 1

            Hi Marc,


            Thanks for the detailed explanation and Sorry for the delayed response.


            Yes, the text override operation does not work at some places.


            Since the process is a bit complex, I will try to complete one at a time. I will let you know in case of any queries.


            Thanks again,


            • 3. Re: Nested+overrides
              Dirk Becker Level 4

              Adding to Marc's answer:


              InDesign internally uses the term run-in styles for all those indirect ways to apply a character style.


              The cascade is even more complicated - I would list style inheritance down to the root paragraph style as separate entry. Besides there is a certain precedence order (by kind) in the evaluation of the run-in styles (e.g. grep overrules line, line overrules nested, nested overrules dropcap), so you'd also make them separate entries. Finally there are reasons to consider dropcaps separate from nested styles, e.g. their activation is bound to additional conditions.


              As you mention overrides: there is some mechanism that optimizes "redundant" attributes away when you apply styles, but the mechanism does not kick in when you later change the style and local overrides become redundant that way. So even comparing attribute values is not always reliable.



              • 4. Re: Nested+overrides
                devsel Level 1

                Hi Marc,

                Thanks for the additions.