5 Replies Latest reply on May 20, 2013 11:22 AM by foredit

    Select one or more characters after insertion point

    foredit

      Hello, i think easy job for scripters.

      From this line

       

      var myStart = app.activeDocument.selection[0].parentStory.insertionPoints;

       

      how to select one or more characters after

       

      thanks a lot

        • 1. Re: Select one or more characters after insertion point
          Laubender Adobe Community Professional & MVP

          @foredit – here an example for selecting ten character before the current selection using the selection point approach:

           

           

          //How many characters should be selected before?
          var howManyCharsBefore = 10;
          
          //The index of the first insertion point of the Story related to its story:
          var begin = app.selection[0].insertionPoints[0].index;
          
          //The index of the last insertion point of the Story related to its story:
          var end = app.selection[0].insertionPoints[-1].index;
          
          //A check, if there are enough insertion points as necessary before the selection:
          var control = begin - howManyCharsBefore+1;
          
          if(control > 0){
              app.select(app.selection[0].parentStory.insertionPoints.itemByRange(begin-howManyCharsBefore,end));
              };
          
          //in the case of not enough selection points before, you could establish an else{} statement, 
          //that expands the selection to the beginning of the story:
          
          else{app.select(app.selection[0].parentStory.insertionPoints.itemByRange(0,end));};
          


          Maybe from that you could figure out the right logics for selecting characters after the selection.

           

          Uwe

          • 2. Re: Select one or more characters after insertion point
            Laubender Adobe Community Professional & MVP

            For further reading on properties and methods (as always) see:

             

            http://www.jongware.com/idjshelp.html

             

            Uwe

            • 3. Re: Select one or more characters after insertion point
              foredit Level 1

              @Laubender, great help and i'm very happy to solve on my own the after selection.

              And a visit to Jongware site is (as always) a precious advice.

               

              Best regards

              • 4. Re: Select one or more characters after insertion point
                Trevorׅ Adobe Community Professional

                Hi Foredit

                 

                Uwe answer is good so you can mark it as correct.

                You can give me a helpful for this.

                The Math.min and max is there to make sure you don't select text that doesn't exist.

                Have a good look at the script below because it the basic way of navigating around your document.

                P.s. there are very very very rarely cases were one should "select" texts

                mySelection = app.selection[0].insertionPoints[0];
                mySelection.parentStory.characters.itemByRange (mySelection.index, Math.min(mySelection.index + 10, mySelection.parentStory.characters.length - 1)).select();
                alert("Now for a word trick\rSelect from 2 word before till 2 words after the selection ");
                myWordPos = mySelection.parentStory.texts.itemByRange (mySelection.parentStory.insertionPoints[0], mySelection).words.length - 1;
                mySelection.parentStory.words.itemByRange (myWordPos - 2, myWordPos + 2).select();
                //myWordPos = mySelection.parentStory.texts.itemByRange (mySelection.parentStory.insertionPoints[0], mySelection).lines.length - 1;
                //mySelection.parentStory.lines.itemByRange (Math.max(myWordPos - 2, 0), Math.min(myWordPos + 2, mySelection.parentStory.lines.length - 1)).select();
                
                • 5. Re: Select one or more characters after insertion point
                  foredit Level 1

                  @Laubender

                  Here it is

                   

                  //Total Insertion points of the Story

                  var insPointNum = app.selection[0].parentStory.insertionPoints.length;

                   

                   

                  //How many characters should be selected after?

                  var howManyCharsAfter = 10;

                   

                  //The index of the first insertion point of the Story related to its story:

                  var begin = app.selection[0].insertionPoints[0].index;

                   

                  //The index of the last insertion point of the Story related to its story:

                  var end = app.selection[0].insertionPoints[-1].index;

                   

                  //A check, if there are enough insertion points as necessary after the selection:

                  var control = begin + howManyCharsAfter-1;

                   

                  if(control < insPointNum){

                      app.select(app.selection[0].parentStory.insertionPoints.itemByRange(begin+howManyCharsAft er,end));

                      };

                   

                  //in the case of not enough selection points after, you could establish an else{} statement,

                  //that expands the selection to the end of the story:

                   

                  else{app.select(app.selection[0].parentStory.insertionPoints.itemByRange(-1,end));};