12 Replies Latest reply on Jan 3, 2014 8:26 AM by Laubender

    Nested styles and textStyleRanges

    fivan Level 1

      HI all!

       

      I have to recognize all textStyleRanges in my documents.

       

      Unfortunately character styles described by a nested style are not really applied to the text and the paragraph looks like a single textStyle range.

       

      Is it possible to apply the characterStyles from nested styles?

       

      thanks.

       

      Ivan

        • 1. Re: Nested styles and textStyleRanges
          Kai Rübsamen Adobe Community Professional

          Hi,

           

          nested character Styles ARE kind of applied to the text, but are hidden in some cases, because they are not at the same level as paragraph styles. Because they are applied to the text, they can be found with find/change.

           

          A possible solution would be to seek the appropriate text and apply the character Style twice. At the end you can remove the doubly assigned cStyle.
          var curSel = app.selection[0];
          var curStory = curSel.parentStory;
          var allRanges = curStory.textStyleRanges;
          alert (allRanges.length);
          
          var PARA_STYLE_NAME = 'MyParaStyle';
          var CHAR_STYLE_NAME = 'MyCharStyle';
          
          var doc = app.activeDocument;
          var pStyle = doc.paragraphStyles.itemByName(PARA_STYLE_NAME);
          var cStyle = doc.characterStyles.itemByName(CHAR_STYLE_NAME);
          
          app.findGrepPreferences = app.changeGrepPreferences = null;
          app.findGrepPreferences.appliedParagraphStyle = pStyle;
          app.findGrepPreferences.appliedCharacterStyle = cStyle;
          app.changeGrepPreferences.appliedCharacterStyle = cStyle;
          doc.changeGrep();
          
          alert( allRanges.length );
          

           

           

          Kai

          1 person found this helpful
          • 2. Re: Nested styles and textStyleRanges
            fivan Level 1

            Hi Kai.

             

            I do not understand what do you mean with "nested character Styles ARE kind of applied to the text" but your sample seems to be very usefull.

             

            Thanks.

             

            Ivan

            • 3. Re: Nested styles and textStyleRanges
              Kai Rübsamen Adobe Community Professional

              Hi Ivan,

               

              > I do not understand what do you mean with "nested character Styles ARE kind of applied to the text

               

              If you apply a character style direct to some text, the paragraph style and character style are in the same hierarchy. If you apply a character style as a part of a nested style, the character style is a part of the pStyle and therefore one level deeper in the hierarchy.

               

              My understanding is, that in every case, the character style is applied to the text, even if it is not visible to other programs or in this case for a textStyleRange. Therefore, it is possible to find those nested styles or GREP-Styles too  or export it e.g. to EPUB.

              • 4. Re: Nested styles and textStyleRanges
                Laubender Adobe Community Professional & MVP

                @Ivan – recently I posted something about text style ranges.

                Just for illustrating the problems, see my answers #3 to #6 in:

                 

                poortip87

                Get text selection and properties

                 

                http://forums.adobe.com/message/5943951#5943951

                 

                Uwe

                • 5. Re: Nested styles and textStyleRanges
                  fivan Level 1

                  Hi Kai

                   

                  Your solution works fine also with nestedLineStyles but not with nestedGrepStyles.

                  If I try to find a nestedGrepStyles, only the last occurrence is replaced.

                  Do you know why?

                   

                  Thanks.

                  Ivan

                  • 6. Re: Nested styles and textStyleRanges
                    Kai Rübsamen Adobe Community Professional

                    Hm, I did a short test with with ^\w+ and applied cStyle 'bold' on some lines and cannot see any problems here.

                    Maybe your scriptpart and a testfile is helpful.

                     

                    Kai

                    • 7. Re: Nested styles and textStyleRanges
                      fivan Level 1

                      My test is with this rule \d\d-\d\d-\d\d\d\d just to recognize dates with dd-mm-yyyy structure but also the UI tool can't find all the occurrences, not only my script.

                       

                      Now I will try with a brand new document..


                      Ivan

                      • 8. Re: Nested styles and textStyleRanges
                        fivan Level 1

                        After several test this is what I found:

                         

                        Find/replace GREP finds only the last occurrence in every paragraph.

                        This happens with nestedGrepStyles and nestedStyles too.

                         

                        In this example I use a paragraphStyle with three rules in nested styles.

                        The first and the last rule uses the same charStyle.

                        Find/replace GREP tool finds only the second occurence in every paragraph.

                         

                        I cant understand the logic.

                        I hope it's my fault (even if I dont know where), otherwise I cant see a way out.

                        Any suggest is really appreciated

                         

                        Schermata 2014-01-03 a 11.19.41.png

                        • 9. Re: Nested styles and textStyleRanges
                          Laubender Adobe Community Professional & MVP

                          Ok. You could test every single character with the property appliedNestedStyle. That will give you an array of applied nested styles. The result would be an undefined object (case: no nested style applied) or an array of length 1 or length >1 if more styles overlap on a certain character.

                           

                          Code:

                           

                          var myCharacterStylesArray = myCharacter.appliedNestedStyles;
                          
                          for(var n=0; n<myCharacterStylesArray.length;n++){
                              $.writeln(myCharacterStylesArray[n].name)
                              };
                          

                           

                          The problem here is: which of the styles will win to format the character?

                           

                          My tests are showing (and I could be dead wrong!), the last listed style in that array will list the winner.

                           

                          Code for testing  a selection of some characters with different nested styles or GREP styles (with overlapping results)  applied:

                           

                          var selectedCharacters = app.selection[0].characters;
                          
                          for(var n=0;n<selectedCharacters.length;n++){
                              try{
                              $.writeln(selectedCharacters[n].appliedNestedStyles[selectedCharacters[n].appliedNestedStyles.length-1].name);
                              }catch(e){};
                              };
                          
                          exit();
                          

                           

                          If there are no applied nested styles, the try/catch will prevent this loop ending throwing an error.
                          Of course we have to factor in a regular applied character style as well:

                           

                          var selectedCharacters = app.selection[0].characters;
                          
                          for(var n=0;n<selectedCharacters.length;n++){
                              try{
                              if(selectedCharacters[n].appliedCharacterStyle.index === 0){
                                  $.writeln("The winner is:"+"\t"+selectedCharacters[n].appliedNestedStyles[selectedCharacters[n].appliedNestedStyles.length-1].name);
                                  };
                              else{$.writeln("The winner is:"+"\t"+selectedCharacters[n].appliedCharacterStyle.name)};
                              }catch(e){};
                              };
                          
                          exit();
                          

                           

                          Phew, this took me a while to find out…

                           

                          Uwe

                          • 10. Re: Nested styles and textStyleRanges
                            Laubender Adobe Community Professional & MVP

                            Hmm, in a situation with overlapping styles (mixed styles), some properties from different nested styles will be mixed of course. So where does this all lead us?

                             

                            Uwe

                            • 11. Re: Nested styles and textStyleRanges
                              fivan Level 1

                              Anywhere.

                              Another test you can do with UI:

                               

                              Try to delete the cTyle from the characterStyle Panel and force the local application.

                              Characteristics of the style disappear, instead of being transferred to the text as local features.
                              This makes me go back to the first hypothesis. Styles in nestedStyles are not really applied.

                              This is just a "post-production" of InDesign...

                               

                              Ivan

                              • 12. Re: Nested styles and textStyleRanges
                                Laubender Adobe Community Professional & MVP

                                @Ivan – I really think nested styles are applied.

                                 

                                If we have a mixed situation all these styles are applied. The last one in the array is dictating the set properties. If some properties are not set, the other styles kick in. Or the underlying paragraph style.

                                 

                                If a "normal"  character style is applied, that will dictate the set properties. If local formatting is done, that will dictate the values of the local properties.

                                 

                                Uwe