Copy link to clipboard
Copied
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);
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
myRightPage.textFrames[0].lines.everyItem()
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.
T
...Copy link to clipboard
Copied
Hi Sayed,
scratched my answer.
I'll be back with a better one.
Regards,
Uwe Laubender
( ACP )
Copy link to clipboard
Copied
Just something like this?
var allButLastLine = myRightPage.textFrames.everyItem().lines.everyItem().getElements();
allButLastLine.pop();
alert(allButLastLine[allButLastLine.length-1].contents);
Copy link to clipboard
Copied
Thank You @brianp311 , that worked, but the pop() will change the array instead of creating new array, what if we want the "allButLastLine" in a new array?
Copy link to clipboard
Copied
Thank you @brianp311 that worked properly, but the pop() changes the array, what if we want to make a new array for "allButLastLine" instead of changing it.
Copy link to clipboard
Copied
@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
myRightPage.textFrames[0].lines.everyItem()
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[0].lines.itemByRange(0, -2);
This assumes that you have only one text frame.
P.
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
Aha, ok.
Copy link to clipboard
Copied
Hi Sayed,
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
https://www.indiscripts.com/post/2010/06/on-everyitem-part-1
On ‘everyItem()’ – Part 2
Marc Autret, July 19, 2010
https://www.indiscripts.com/post/2010/07/on-everyitem-part-2
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() .
Regards,
Uwe Laubender
( ACP )
Copy link to clipboard
Copied
Thank you @Laubender variable myRightPage is a textframe. and finally the script worked properly. i used itemByRange as @Peter Kahrel suggested.