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

Target all lines in page except the last line

Explorer ,
Apr 01, 2022 Apr 01, 2022

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);
TOPICS
Scripting

Views

312

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 2 Correct answers

Community Expert , Apr 01, 2022 Apr 01, 2022

Just something like this? 

 

var allButLastLine = myRightPage.textFrames.everyItem().lines.everyItem().getElements();
allButLastLine.pop();
alert(allButLastLine[allButLastLine.length-1].contents);

 

Votes

Translate

Translate
Community Expert , Apr 02, 2022 Apr 02, 2022

@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

...

Votes

Translate

Translate
Community Expert ,
Apr 01, 2022 Apr 01, 2022

Copy link to clipboard

Copied

Hi Sayed,

scratched my answer.

I'll be back with a better one.

 

Regards,
Uwe Laubender

( ACP )

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 01, 2022 Apr 01, 2022

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);

 

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 02, 2022 Apr 02, 2022

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?

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 02, 2022 Apr 02, 2022

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.

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 02, 2022 Apr 02, 2022

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.

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 02, 2022 Apr 02, 2022

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. 

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 03, 2022 Apr 03, 2022

Copy link to clipboard

Copied

Aha, ok.

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 03, 2022 Apr 03, 2022

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 )

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 04, 2022 Apr 04, 2022

Copy link to clipboard

Copied

LATEST

Thank you @Laubender variable myRightPage is a textframe. and finally the script worked properly. i used itemByRange as @Peter Kahrel  suggested.

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