Skip to main content
dublove
Legend
Nov 29, 2026 4:36 PM UTC
Question

What's wrong with this script? It hasn't changed the size of all pages, and the document size hasn't changed either?

Hello everyone.

I wanted all pages to fit the size of the currently selected object, but after running it, I found that only the Hanqian page had been changed, and the document size was still 210 * 297.
It's strange, even when I reopen it, the document size still shows 210 * 297.
How to change all pages and document size simultaneously?

This script has many syntax errors, yet it runs normally.


pageFitObj()
function pageFitObj() {
var d = app.activeDocument;
var item = d.selection[0];
function reframePages(doc, CS_IN, MG, GB, errs, pages, pg, K, a, gb, t)
//----------------------------------
// Reframe all pages w.r.t. page items bounds.
{
// Checkpoint.
// ---
if (!(doc = app.properties.activeDocument)) {
alert("Please open a document.");
return;
}

// Set up the document metrics.
// ---
app.scriptPreferences.measurementUnit = MeasurementUnits.POINTS;
doc.zeroPoint = [0, 0];
doc.viewPreferences.rulerOrigin = RulerOrigin.PAGE_ORIGIN;

// Misc.
// ---
CS_IN = +CoordinateSpaces.INNER_COORDINATES;
MG = { top: 0, left: 0, bottom: 0, right: 0 };
GB = Array(4);
errs = [];

// Loop.
// ---
pages = doc.pages.everyItem().getElements().slice();
for each(pg in pages)
{
K = pg.pageItems;
if( !K.length ) continue; // Empty page -> noop

// Calculate the overall [T,L,B,R] thru min/max.
// ---
GB[0]= GB[1] = 1 / 0 ; GB[2]= GB[3] = -1 / 0;
a = K.everyItem().geometricBounds.slice();
for each(gb in a)
{
(t = gb[0]) < GB[0] && (GB[0] = t);
(t = gb[1]) < GB[1] && (GB[1] = t);
(t = gb[2]) > GB[2] && (GB[2] = t);
(t = gb[3]) > GB[3] && (GB[3] = t);
}

// Reframe the page.
// ---
pg.marginPreferences.properties = MG;
t = [[GB[1], GB[0]], [GB[3], GB[2]]];
try {
pg.reframe(CS_IN, t);
}
catch (e) {
errs.push("[" + pg.name + "] " + e);
}
}

// Report.
// ---
if (errs.length) {
alert("Unable to reset the following page(s):\r\r"
+ errs.slice(0, 20).join('\r'));
}
else {
alert(pages.length + " The page has been rearranged.");
}};

app.doScript
(
reframePages,
void 0,
void 0,
+UndoModes.ENTIRE_SCRIPT,
'Repositioning the page',
);
item.move([0, 0]);
}

 

    2 replies

    rob day
    Community Expert
    Community Expert
    Nov 29, 2026 10:38 PM UTC

    Also, I don’t think there is a way to change the Document Setup dialog, but you can add all the parent pages to the resizePages functio:

     

    //use points for the resize() function
    app.scriptPreferences.measurementUnit = MeasurementUnits.POINTS;

    //a selected rectangle
    var s = app.activeDocument.selection[0];
    var b = s.geometricBounds
    var w = b[3]-b[1]
    var h = b[2]-b[0]

    //call resizePage and pass in new width and height in points as an array
    app.doScript(resizePages, ScriptLanguage.JAVASCRIPT, [w,h], UndoModes.ENTIRE_SCRIPT, 'Page Resize');
    s.move([0,0])

    app.scriptPreferences.measurementUnit = AutoEnum.AUTO_VALUE;

    /**
    * Resize documents pages
    * @ param an array with new width and height
    * @ return value
    */
    function resizePages(a){
    var p = app.activeDocument.pages.everyItem().getElements();
    var ms = app.activeDocument.masterSpreads.everyItem().pages.everyItem().getElements();
    for (var i = 0; i < ms.length; i++){
    ms[i].resize (CoordinateSpaces.INNER_COORDINATES,AnchorPoint.CENTER_ANCHOR,ResizeMethods.REPLACING_CURRENT_DIMENSIONS_WITH,[w,h])
    };
    //resize pages to widh a[0] and height a[1]
    for (var j = 0; j < p.length; j++){
    p[j].resize (CoordinateSpaces.INNER_COORDINATES,AnchorPoint.CENTER_ANCHOR,ResizeMethods.REPLACING_CURRENT_DIMENSIONS_WITH,[a[0],a[1]])
    };
    }

     

    Translate
    rob day
    Community Expert
    Community Expert
    Nov 29, 2026 7:47 PM UTC

    I don’t understand the code—doesn’t seem like it should be that complicated. Something like this?

     

    //use points for the resize() function
    app.scriptPreferences.measurementUnit = MeasurementUnits.POINTS;

    //a selected rectangle
    var s = app.activeDocument.selection[0];
    var b = s.geometricBounds
    var w = b[3]-b[1]
    var h = b[2]-b[0]

    //call resizePage and pass in new width and height in points as an array
    app.doScript(resizePages, ScriptLanguage.JAVASCRIPT, [w,h], UndoModes.ENTIRE_SCRIPT, 'Page Resize');
    s.move([0,0])

    app.scriptPreferences.measurementUnit = AutoEnum.AUTO_VALUE;

    /**
    * Resize documents pages
    * @ param an array with new width and height
    * @ return value
    */
    function resizePages(a){
    var p = app.activeDocument.pages.everyItem().getElements();
    //resize pages to widh a[0] and height a[1]
    for (var j = 0; j < p.length; j++){
    p[j].resize (CoordinateSpaces.INNER_COORDINATES,AnchorPoint.CENTER_ANCHOR,ResizeMethods.REPLACING_CURRENT_DIMENSIONS_WITH,[a[0],a[1]])
    };
    }

     

     

     

     

    After:

     

    Translate