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

What is the fastest way to select next x number of lines?

Explorer ,
Apr 12, 2023 Apr 12, 2023

Copy link to clipboard

Copied

I want to select x number of lines starting from the line that the cursor is on, or the first line of selected text frame if a text frame is selected.

 

I got together this very slow script that does the job, but there has to be a better way. My understanding of indesign scripting is very limited so I wanted to ask the elite.

 

I couldn't use textFrame.lines because i want to continue selecting lines from the next frame, also I couldn't use paragraphs.lines because the start may be in the middle of a paragraph.

 

var numLines = 5; // Change this to the number of lines you want to select
var selectedLine = app.selection[0].lines[0].index;
var firstChars = getFirstChars(numLines);
var start = selectedLine;
var end = firstChars[numLines - 1] - 1;

var secilecek = app.selection[0].insertionPoints[0].parent.characters.itemByRange(start, end);
secilecek.select();

function getFirstChars(numLines) {
var firstChars = [];
var nextLine = app.selection[0].lines[0].insertionPoints[-1].lines[0];
for (var i = 0; i < numLines; i++) {
nextLine.characters[0].select();
firstChars.push(nextLine.characters[0].index);
nextLine = app.selection[0].lines[0].insertionPoints[-1].lines[0];
}
return firstChars;
}

 

TOPICS
Scripting

Views

306

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

correct answers 1 Correct answer

Community Expert , Apr 12, 2023 Apr 12, 2023

This is probably quicker:

1. Get the number of the current line, which you do by counting the number of lines between the start of the story and the insertion point.

2. Then select the number of lines from that line.

 

story = app.selection[0].parentStory;

// Get the selected line's number

lineNumber = story.insertionPoints
  .itemByRange (0, app.selection[0].index)
  .lines.length-1;

// Select from it plus the following n

story.lines.itemByRange(x, x + numLines -1).select();

 

Peter

Votes

Translate

Translate
Community Expert ,
Apr 12, 2023 Apr 12, 2023

Copy link to clipboard

Copied

This is probably quicker:

1. Get the number of the current line, which you do by counting the number of lines between the start of the story and the insertion point.

2. Then select the number of lines from that line.

 

story = app.selection[0].parentStory;

// Get the selected line's number

lineNumber = story.insertionPoints
  .itemByRange (0, app.selection[0].index)
  .lines.length-1;

// Select from it plus the following n

story.lines.itemByRange(x, x + numLines -1).select();

 

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
Explorer ,
Apr 12, 2023 Apr 12, 2023

Copy link to clipboard

Copied

Yes, this is way faster, thank you very much.

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 ,
Apr 12, 2023 Apr 12, 2023

Copy link to clipboard

Copied

Hi @ali u , Try this:

 

var nl = 5;
var ps = app.selection[0].parent.lines;
var ss, se;
for (var i = 0; i < ps.length; i++){
    if (ps[i] == app.selection[0].lines.lastItem()) {
        ss = ps[i+1]
        se = ps[i+nl]
    } 
};   
app.select(ps.itemByRange(ss,se))

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
Explorer ,
Apr 12, 2023 Apr 12, 2023

Copy link to clipboard

Copied

@rob day Thank you, this works faster if there is one or two pages, but when I run it at page 150 it waited quite a bit.

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 ,
Apr 12, 2023 Apr 12, 2023

Copy link to clipboard

Copied

Maybe limit the loop to the text frame of the selection:

 

var nl = 5;
//var ps = app.selection[0].parent.lines;
var ps = app.selection[0].lines[-1].parentTextFrames[0].lines;

var ss, se;
for (var i = 0; i < ps.length; i++){
    if (ps[i] == app.selection[0].lines.lastItem()) {
        ss = ps[i+1]
        se = ps[i+nl]
    } 
};   
app.select(ps.itemByRange(ss,se))

 

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
Explorer ,
Apr 12, 2023 Apr 12, 2023

Copy link to clipboard

Copied

LATEST

Yes, this makes it faster but then if I want to select more lines than the selected text frame has it doesn't work.

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