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

How to identify the first and last pages?

Guide ,
Aug 08, 2025 Aug 08, 2025

My code.:

var sel = app.documents[0].selection;
pp = sel[j].parentPage;

Now there are three conditions that can identify left and right pages, as well as single pages

pp.side == PageSideOptions.RIGHT_HAND
pp.side == PageSideOptions.SINGLE_SIDED
pp.side == PageSideOptions.LEFT_HAND

I now want to handle the first and last pages separately. It would be helpful if I could determine whether the current page is a spread.

TOPICS
How to , Scripting
162
Translate
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 , Aug 08, 2025 Aug 08, 2025

A page is not a spread ever afaik. Spread contains pages. A page's parent will give you the spread. So if you want to know if a page is the only one in the spread then something like the following would work

if(pp.parent.pages.length == 1)
   alert("PP is the single page in its spread.")

If you want the first and last page of a spread you can use the following. Do note that if the spread has only 1 page then both these pages would be same

var firstPageOfSpread = pp.parent.pages[0]
var lastPageOf
...
Translate
Community Expert , Aug 09, 2025 Aug 09, 2025

Testing the side doesn’t tell you if it’s a first or last page—the document could be setup like this:

 

Screen Shot 42.png

 

You can use the page’s document offset to test for first or last like this:

 

var sel = app.documents[0].selection[0];

if (sel.parentPage.documentOffset == 0) {
	$.writeln("Selection is on first page")
} 
if (sel.parentPage.documentOffset == app.documents[0].pages.length-1) {
	$.writeln("Selection is on last page")
} 
Translate
Community Expert ,
Aug 08, 2025 Aug 08, 2025

You can check the number of pages in the parentPage’s parent (the spread)

 

var sel = app.documents[0].selection[0];
$.writeln("The selection’s spread contains " + sel.parentPage.parent.pages.length + " pages")
Translate
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 ,
Aug 08, 2025 Aug 08, 2025

A page is not a spread ever afaik. Spread contains pages. A page's parent will give you the spread. So if you want to know if a page is the only one in the spread then something like the following would work

if(pp.parent.pages.length == 1)
   alert("PP is the single page in its spread.")

If you want the first and last page of a spread you can use the following. Do note that if the spread has only 1 page then both these pages would be same

var firstPageOfSpread = pp.parent.pages[0]
var lastPageOfSpread = pp.parent.pages[-1]

-Manan

Translate
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 ,
Aug 09, 2025 Aug 09, 2025
LATEST

Testing the side doesn’t tell you if it’s a first or last page—the document could be setup like this:

 

Screen Shot 42.png

 

You can use the page’s document offset to test for first or last like this:

 

var sel = app.documents[0].selection[0];

if (sel.parentPage.documentOffset == 0) {
	$.writeln("Selection is on first page")
} 
if (sel.parentPage.documentOffset == app.documents[0].pages.length-1) {
	$.writeln("Selection is on last page")
} 
Translate
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