6 Replies Latest reply on Dec 19, 2013 3:57 AM by Laubender

    Get text selection and properties

    poortip87 Level 1

      Hi All,

       

      I need to get the text selection in my active document so that I can access the properties of selected text like paragraphs, font styles, colors etc.

      I wrote the following script to get text selection,

       

      var objDoc = app.activeDocument;

      var objText = app.selection[0];

       

      This gives me objText as a Text object. When I try to access paragraphs in this text, I get the full contents of the para, not just the ones in the selection. I do not want this. I want to access properties in just the text selection.

       

      How can I achieve this?

        • 1. Re: Get text selection and properties
          Jump_Over Level 5

          Hi,

           

          Everything you wrote is possible to access.

          If your result is wrong = just paste a code which you expect as correct one.

           

          Jarek

          • 2. Re: Get text selection and properties
            Green4ever Level 3

            objText.contents will give you the contents of the selection not the whole content of the paragraph.

             

            If you want to get the properties of the selection, try something like objText.appliedParagraphStyle.

             

            Warning: If you try to get the properties like appliedCharacterStyle, font-size, and font-family when there is multiple textStyleRanges it will not work properly. It will only give you the properties of the first textStyleRange (I hope).

             

            ~Green4ever

            • 3. Re: Get text selection and properties
              Laubender Adobe Community Professional & MVP

              @All – let's do an example of a selection with a screenshot (always best to illustrate the problem):

               

              Simple example, some characters selected with different formatting (in this case the fillColor property):

               

              TextStyleRanges_OfSelection.png

               

               

              In total 5 textStyleRanges in the text frame.
              Two textStyleRanges in the selection of the same frame below.

               

              If you want to ask for the fillColor used in the 2 textStyleRanges of the selection, you can do it like that:

               

              var mySelection = app.selection[0];
              var myTextStyleRanges = mySelection.texts[0].textStyleRanges.everyItem().getElements();
              
              for(var n=0;n<myTextStyleRanges.length;n++){
                  $.writeln(myTextStyleRanges[n].fillColor.name);
                  };
              

               

              The result of my example as written to the JavaScript console of the ESTK (ExtendScript Toolkit):

               

              "C=75 M=5 Y=100 K=0" for the first textStyleRange in the selection

              "C=100 M=0 Y=0 K=0" for the second textStyleRange in the selection

               

              But like the example with asking for paragraphs in a selection, the same goes for textStyleRanges:
              You are asking for the whole object, you'll get the whole object.

               

              And if you would ask for the "contents" (the not formatted string representation of a texts object), in the case of textStyleRanges in my example like that

               

              myTextStyleRanges[n].texts[0].contents
              

               

              you would get the following results:

               

              "ij" for the first textStyleRange in the selection

              "klmn" for the second textStyleRange in the selection

               

              However, the contents of the selection in my example would be:

              "ijkl"

               

              Uwe

              • 4. Re: Get text selection and properties
                Laubender Adobe Community Professional & MVP

                I just want to add for the OP: textStyleRanges will change every time the formatting of texts is changed. In the case of subtle changes at first glance to the text you cannot see them when looking in the UI. It could be anything you could format a text with.

                 

                So, if you are after some destinct properties (out of maybe 300), you may need other methods to be effective.

                 

                And another important thing: in the case your paragraphStyle includes GREP Style definitions, the method using textStyleRanges will not work as expected.

                 

                Example:

                 

                TextStyleRanges_WithGREPStylesInParaStyle.png

                 

                If you then ask how many textStyleRanges your selection will consist of,

                the result will be: 1

                 

                Asking for the fillColor of that range you'll get in my example:

                 

                "C=100 M=0 Y=0 K=0"

                 

                However, if you ask your selection for the fillColor.name of every individual character with

                 

                var mySelection = app.selection[0];
                var myCharacters = mySelection.texts[0].characters.everyItem().getElements();
                
                for(var n=0;n<myCharacters.length;n++){
                    $.writeln(myCharacters[n].fillColor.name);
                    };
                

                 

                you'll get the following answer:

                 

                "C=100 M=0 Y=0 K=0" for character 1 of your selection

                "C=0 M=100 Y=0 K=0" for character 2 of your selection

                 

                Uwe

                • 5. Re: Get text selection and properties
                  Laubender Adobe Community Professional & MVP

                  Another oddity with textStyleRanges in a selection:

                   

                  TextStyleRanges_AnotherOddity.png

                   

                  So, in this example we have four GREP Styles in the paragraph style and one single character selected.
                  How are the bets, if I'm asking for textStyleRanges and the "contents" of that (one!) range in my selection?

                   

                  Would the contents be

                  1. "b", (just the contents of the selection) or would it be

                  2. "bccc" (why? the textStyleRange could exceed my selection, but how?) or would it be

                  3. "bcccddddd"? (all the way to the end of the paragraph or beyond?)

                   

                  The surprising answer is #2.

                  The contents of the one and only textStyleRange of my selection would be
                  "bccc"

                   

                  So, in fact it will be include the characters b plus the three characters c.

                   

                  After some other tests with GREP Styles it seems clear, that the range of the textStyleRange of a selection will consist of the first and the next formatting change. Doesn't make a difference: if it will be forced by a GREP Style or if the formatting will fall back to its "normal" behaviour, if the GREP Style cannot kick in any more.

                   

                  Uwe

                  • 6. Re: Get text selection and properties
                    Laubender Adobe Community Professional & MVP

                    Just to illustrate my last point:

                     

                    Here an example with a paragraph formatted with only one GREP Style, letter "b" should change color to Cyan. If you select letter b only, the textStyleRange includes letter "b" (no surprise here) but also all the characters to the end!

                     

                    TextStyleRange_ONEGREPStyle_OneCharacterSelected.png

                     

                    Code:

                     

                    var mySelection = app.selection[0];
                    var myTextStyleRanges = mySelection.texts[0].textStyleRanges.everyItem().getElements();
                    
                    for(var n=0;n<myTextStyleRanges.length;n++){
                        $.writeln(myTextStyleRanges[n].texts[0].contents);
                        $.writeln(myTextStyleRanges[n].texts[0].fillColor.name);
                        };
                    

                     

                    Result in the console:

                     

                    bcccddddd

                    C=100 M=0 Y=0 K=0

                     

                    Which is misleading…

                     

                    Further we could try to get the fill Color for all characters of the textStyleRange and ask them about their fillColor.name:

                     

                    var mySelection = app.selection[0];
                    var myTextStyleRanges = mySelection.texts[0].textStyleRanges.everyItem().getElements();
                    
                    for(var n=0;n<myTextStyleRanges.length;n++){
                        
                        $.writeln("Contents of Range:"+"\r"+myTextStyleRanges[n].texts[0].contents);
                        $.writeln("fillColor.name of Range:"+"\r"+myTextStyleRanges[n].texts[0].fillColor.name);
                        
                        var allCharsInRange = myTextStyleRanges[n].texts[0].characters.everyItem().getElements();
                        
                        for(var c=0;c<allCharsInRange.length;c++){
                            $.writeln("Index number of loop: "+c+"\t"+allCharsInRange[c].contents+"\t"+allCharsInRange[c].fillColor.name);
                            };
                        
                        };
                    

                     

                    The result for my example would be:
                    First column of result: index number of for loop (0-8), there are 9 characters in the one and only range

                    Second column: contents of the single character

                    Third column: fillColor.name of the character

                     

                    Contents of Range:
                    bcccddddd
                    fillColor.name of Range:
                    C=100 M=0 Y=0 K=0
                    
                    Index number of loop: 0    b    C=100 M=0 Y=0 K=0
                    Index number of loop: 1    c    Black
                    Index number of loop: 2    c    Black
                    Index number of loop: 3    c    Black
                    Index number of loop: 4    d    Black
                    Index number of loop: 5    d    Black
                    Index number of loop: 6    d    Black
                    Index number of loop: 7    d    Black
                    Index number of loop: 8    d    Black
                    

                     

                    Uwe