Copy link to clipboard
Copied
I need to find on whitch line of the parent story ist the current selection. How can i do that?
Copy link to clipboard
Copied
Hi Vamitul,
Please provide the clear explanation? or snapshots.
thx,
csm_phil
Copy link to clipboard
Copied
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?
Copy link to clipboard
Copied
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...
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
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
.index; if(currIndex === lineIndex){alert('CurrSel-LineNbr.: ' + (l+1))};
}
Hans-Gerd Claßen
Copy link to clipboard
Copied
-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
.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
Copy link to clipboard
Copied
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)
Copy link to clipboard
Copied
Hans -- So then you're back at jump_over's approach (which I think is the correct one).
Peter
Copy link to clipboard
Copied
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?
Copy link to clipboard
Copied
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