• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Selection line index

Advisor ,
Oct 25, 2012 Oct 25, 2012

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?

TOPICS
Scripting

Views

1.8K

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advocate ,
Oct 25, 2012 Oct 25, 2012

Copy link to clipboard

Copied

Hi Vamitul,

Please provide the clear explanation? or snapshots.

thx,

csm_phil

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advisor ,
Oct 25, 2012 Oct 25, 2012

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?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Mentor ,
Oct 25, 2012 Oct 25, 2012

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...

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advisor ,
Oct 25, 2012 Oct 25, 2012

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Oct 26, 2012 Oct 26, 2012

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Oct 26, 2012 Oct 26, 2012

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advisor ,
Oct 29, 2012 Oct 29, 2012

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Oct 29, 2012 Oct 29, 2012

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)

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Oct 29, 2012 Oct 29, 2012

Copy link to clipboard

Copied

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

Peter

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advisor ,
Oct 29, 2012 Oct 29, 2012

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?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Oct 29, 2012 Oct 29, 2012

Copy link to clipboard

Copied

LATEST

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines