11 Replies Latest reply on Oct 29, 2012 7:24 AM by Peter Kahrel

    Selection line index

    Vamitul Level 4

      I need to find on whitch line of the parent story ist the current selection. How can i do that?

        • 1. Re: Selection line index
          csm_phil Level 4

          Hi Vamitul,

           

          Please provide the clear explanation? or snapshots.

           

           

          thx,

          csm_phil

          • 2. Re: Selection line index
            Vamitul Level 4

            Hi, phil!

             

            don't really know how to explain better, but i will try:

             

            let's say i have a story, and a word selected in the story:

            var myWord=app.activeDocument.selection[0]

            var myStory=myWord.parentStory;

            var myLineLen=myStory.lines.length; //is 25 for example;

             

            //how can i find out on what line does myWord sit?

            • 3. Re: Selection line index
              Jump_Over Level 5

              Hi,

               

              1. name your story 1st line (start) and the line where selection starts (end)

              2. Number of line will be a length of the part of story from start to end:

              story.lines.itemByRange(start,end).text[0].lines.length

               

              hope...

              1 person found this helpful
              • 4. Re: Selection line index
                Vamitul Level 4

                thanks. very convoluted way of doing something that should be vey simple. i'we used a different aproace (but just as wierd):

                var myLine=sel.lines[0];

                var myNextLine=sel.parentStory.lines.nextItem(myLine);

                var myLineNr=myNextLine.index

                • 5. Re: Selection line index
                  Peter Kahrel Adobe Community Professional & MVP

                  Vamitul,

                   

                  You're wrong: for myLineNr you don't get the line number, but the index of the paragraph's first insertion point. Also, in general, stay away from nextItem() because in longer texts it gets very, very, slow.

                   

                  Peter

                  • 6. Re: Selection line index
                    -hans- Level 4

                    Hi,

                     

                    I'd simply iterate ...

                     

                    var theSel = app.selection[0];
                    var lineIndex = theSel.lines[0].index;
                    var parentStoryLines = theSel.parentStory.lines;
                    ll = parentStoryLines.length;
                    for(var l = 0; l < ll;l++){
                        currIndex = theSel.parentStory.lines[l].index;
                        if(currIndex === lineIndex){alert('CurrSel-LineNbr.: ' + (l+1))};
                            }
                    

                    Hans-Gerd Claßen

                    • 7. Re: Selection line index
                      Vamitul Level 4

                      -hans- wrote:

                       

                      Hi,

                       

                      I'd simply iterate ...

                       

                      var theSel = app.selection[0];
                      var lineIndex = theSel.lines[0].index;
                      var parentStoryLines = theSel.parentStory.lines;
                      ll = parentStoryLines.length;
                      for(var l = 0; l < ll;l++){
                          currIndex = theSel.parentStory.lines[l].index;
                          if(currIndex === lineIndex){alert('CurrSel-LineNbr.: ' + (l+1))};
                              }
                      

                      Hans-Gerd Claßen

                      iterating is not a solution in my case, as i have do do it about 8-900 times per story, each story about 25.000 lines, and an averege of 10 stories per document.

                       

                      peter, i'we checked and you are right, but strangly in my test cases the script worked as intended. I'we seen thata it's very slow, but i didn't know it was because of nextItem.

                      thank you all

                      • 8. Re: Selection line index
                        -hans- Level 4

                        What about using the first InsertionPoint of selection:

                         

                        var currSel = app.selection[0];
                        var currStartPosition = currSel.insertionPoints[0].index;
                        var theResult = currSel.parentStory.insertionPoints.itemByRange(0, currStartPosition);
                        alert('Current Selection starts in line: ' + theResult.lines.length)
                        
                        • 9. Re: Selection line index
                          Peter Kahrel Adobe Community Professional & MVP

                          Hans -- So then you're back at jump_over's approach (which I think is the correct one).

                           

                          Peter

                          • 10. Re: Selection line index
                            Vamitul Level 4

                            almost, as hans's solution skips the naming part, and seems a bit more elegant. But both of them work the same, so.. how do i mark them as correct?

                            • 11. Re: Selection line index
                              Peter Kahrel Adobe Community Professional & MVP

                              By 'naming' I guess that jump means 'assign to a variable'. His and Hans's scripts boil down to the same thing: define a range of insertion points and get the number of lines lines in that range.

                               

                              Peter