Skip to main content
Known Participant
July 16, 2023
Question

How do you call the actively selected spread length/width in a document, or page bounds?

  • July 16, 2023
  • 4 replies
  • 778 views

In a script I am using 

 

var docWidth = Number(app.activeDocument.documentPreferences.pageWidth);
var docHeight = Number(app.activeDocument.documentPreferences.pageHeight);
 
to get the dimensions of an actively selected page. But I realized this is unsufficient for two-page spreads! The reason is that, when I select a page, I want to draw guides on them. But the spread's ruler goes across both pages instead of re-interpreting the second page as a new, separate page with its own origin.
 
How do I call the active SPREAD dimensions rather than the page dimensions, so that I can fix this? Or is there otherwise a better way to identify individual page bounds within a spread? 
This topic has been closed for replies.

4 replies

rob day
Community Expert
Community Expert
July 17, 2023

In a script I am using

var docWidth = Number(app.activeDocument.documentPreferences.pageWidth);

 

Also, here’s why it might be a problem to get the width from documentPreferences.pageWidth. In my example above if I set w as this:

 

 

var w = doc.documentPreferences.pageWidth;
$.writeln(w)
//returns 8.5 the document‘s default page size

 

 

And try the script on a spread with custom page sizes the guides are wrong because the width of page 3 is 4" not 8.5":

 

 

 

But if I get the width from page 3’s bounds the guides are placed where I want them

 

 

var w = pg3.bounds[3]-pg3.bounds[1];
$.writeln(w)
//returns 4 for the custom page 3

 

 

Known Participant
July 17, 2023

I appreciate everyone's response but I'm still extremely confused and have no idea what's going on, please don't assume I know anything about coding, I still have absolutely no clue whatsoever what ".bounds" means. It's top of...something, bottom of...something, no one explained of what though, and any documentation is atrociously lacking in any semblance of user-friendly detail or any kind of explanation. 

 

Below is code that "almost" does what I want.

 

var pagey1 = app.activeWindow.activePage.bounds[0];
var pagex1 = app.activeWindow.activePage.bounds[1];
var pagey2 = app.activeWindow.activePage.bounds[2];
var pagex2 = app.activeWindow.activePage.bounds[3];


for (var i = 0; i < 20; ++i) {
    app.activeWindow.activePage.guides.add({name:"Blah"+i, location: (pagex2-pagex1)*Number(Subdivisions[i]), orientation: HorizontalOrVertical.VERTICAL});
}
 
This code correctly draws guides on the first page of a selected spread. But I don't want first page, I want ***First or Second***, or even third, whichever actively selected page of whichever actively selected spread just ***happens to be actively selected***.  How do I make that happen using the most minimal possible changes to the current code? .bounds[3]-[1] doesn't work. I have zero idea why, all I know is that it just doesn't. 
m1b
Community Expert
Community Expert
July 17, 2023

Hi @dane13860245,

 

1. Bounds, in Indesign, are [top, left, bottom, right]. eg. Page.bounds, or PageItem.geometricBounds.

 

2. you can add guides to a spread the same as you add a guide to a page:

mySpread.guides.add({ name: 'myguide', location: '10mm', orientation: HorizontalOrVertical.VERTICAL })

 

If you need more help with the exact code, please describe in great detail what you are doing, and what your inputs and constraints will be.

- Mark

m1b
Community Expert
Community Expert
July 16, 2023

Hi @dane13860245, every Page is part of a Spread, and every Spread has at least one Page. So you can do:

var mySpread = myPage.parent;
var myPages = mySpread.pages;

 

I think the only way to get the bounds of a Spread, is to get the bounds of all the spread's Pages, using Page.bounds as Robert mentioned. Here is some example code for getting the bounds of the whole spread:

/**
 * Example of getting bounds of a spread.
 * @author m1b
 * @discussion https://community.adobe.com/t5/indesign-discussions/how-do-you-call-the-actively-selected-spread-length-width-in-a-document-or-page-bounds/m-p/13940663
 */
(function () {

    var doc = app.activeDocument,
        page = doc.layoutWindows[0].activePage,
        spread = page.parent,
        spreadBounds = getExpandedBounds(spread.pages.everyItem().bounds);

    alert('Bounds of active spread:\n'+spreadBounds);

})();

/**
 * Returns the overall bounds, when
 * given an array of bounds ordered
 * as Indesign bounds [TLBR].
 * @author m1b
 * @version 2023-07-17
 * @param {Array<Array<Number>>} arr - an array of bounds arrays.
 * @returns {Array<Number>} - the expanded bounds [TLBR].
 */
function getExpandedBounds(arr) {

    var bounds = [Infinity, Infinity, -Infinity, -Infinity];

    for (var i = 0; i < arr.length; i++) {

        for (var j = 0; j < arr[0].length; j++) {

            if (j < 2)
                bounds[j] = Math.min(bounds[j], arr[i][j])
            else
                bounds[j] = Math.max(bounds[j], arr[i][j])
        }
    }

    return bounds;

};

 

- Mark

Robert at ID-Tasker
Legend
July 17, 2023

But - as I've mentioned in the other thread - when pages are shifted and with gaps - your code will return wrong result as it doesn't add separate sizes - but just outside corners? 

 

Known Participant
July 17, 2023

Alright, so...how do I get this to work? How do I place guides accurately on the *actively selected page* of an *actively selected apread* if bounds 3 - 1 and bounds 2 - 0 doesn't work? What's going on? How do I rectify this?

Robert at ID-Tasker
Legend
July 16, 2023
Known Participant
July 16, 2023

Thanks, I've encountered that webpage before, but the problem with that page, as well as every other page on that site, is that it never spells out examples or how exactly to use the code, and what its attributes look like. You can provide an example of successfully using the bounds property of a page and also a spread to draw a guide?