Hi, Dear Friends!
I want to target all lines in my page except the last line.
Here is my attempt to do that but i get "myRightPage_lines.slice is not a function".
var myRightPage_lines = myRightPage.lines.everyItem(); var myRightPage_lines_except_last = myRightPage_lines.slice(0, -1);
scratched my answer.
I'll be back with a better one.
( ACP )
Copy link to clipboard
Just something like this?
var allButLastLine = myRightPage.textFrames.everyItem().lines.everyItem().getElements(); allButLastLine.pop(); alert(allButLastLine[allButLastLine.length-1].contents);
@brianp311 -- You code returns the penultimate line of a text frame.
@Sayed Ali Mousawi Your code doesn't work because InDesign pages don't contain lines directly. They contain text frames, which in turn contain lines, as Brian's code shows. Another problem in your code would have been that
returns a collection, whereas .slice() is an array function. In addition, .slice() is a text function, and you want to target not text but InDesign text objects.
To target all lines in the text frame on a page but the last one, you need this:
var myRightPage_lines_except_last = myRightPage.textFrames.lines.itemByRange(0, -2);
This assumes that you have only one text frame.
I was just running the alert as an example. The array is all lines except the last. But yes, your code is more efficient assuming one text frame and provides a single collection object as opposed to an array.
let me add something.
In my first answer that I scratched I also suggested a solution with everyItem().getElements() to build an array out of the collection. Then, I thought this might not be a good idea. Had no time over the weekend to suggest something with itemByRange() as Peter did.
In any case read into:
On ‘everyItem()’ – Part 1
Marc Autret, June 30, 2010
On ‘everyItem()’ – Part 2
Marc Autret, July 19, 2010
Back to your code and situation. I wonder what your variable myRightPage does contain.
Is it a page object? Then your code would not work at all, because line is not the property of a page object.
Is it a text object? Like the text of one text frame? Or is it the text of several text frames on that page?
Could make a difference then how InDesign gathers the collection with lines.everyItem() .
( ACP )