Skip to main content
Participant
February 24, 2023
Question

reordering pages

  • February 24, 2023
  • 2 replies
  • 426 views

I have a nearly 3000 page document that I need to reorder the pages based on original page numbers. I found this script on the site: https://community.adobe.com/t5/indesign-discussions/script-to-reorder-pages/td-p/2307666 which works if there are only a few pages but since I've got way more than that, it is loading for hours and I eventually have to force quit it. Any suggestions on what I need to change? Thank you!

This topic has been closed for replies.

2 replies

m1b
Community Expert
Community Expert
February 24, 2023

Hi @Elizabeth285754906b5l, the scripts you linked to may have been garbled a little due to being migrated from another forum platform. I have repaired @Marc Autret's code (see below) and tested with a short document. Can you try it with your massive document? (Edit: Don't forget to set your own desired order.)

- Mark

 

 

function main() {

    var order = "4, 2-3, 1";
    reorderPages(order);

}

app.doScript(main, ScriptLanguage.JAVASCRIPT, undefined, UndoModes.ENTIRE_SCRIPT, 'Reorder Pages');



/**
 * Reorder active document's pages.
 * @author Marc Autret
 * @discussion https://community.adobe.com/t5/indesign-discussions/script-to-reorder-pages/td-p/2307666
 * @param {String} newOrder - the desired order eg, '10-20, 1-9, 11, 13, 12'.
 */
function reorderPages(/*str*/newOrder) {

    var pages = app.activeDocument.pages;

    var pgMap = (function () {
        var items = newOrder.replace(/[^\d,-]/g, '').split(',');
        var r = [], bkp = {}, i, sz, p;

        while (i = items.shift()) {
            i = i.split('-');

            // don't allow "x-y-z"
            if ((sz = i.length) > 2) return false;

            // don't allow "max-min"
            if (i[0] > i[sz - 1]) return false;

            for (p = +i[0]; p <= +i[sz - 1]; p++) {
                if (!pages.item('' + p).isValid) return false;

                if ('' + p in bkp) return false;
                bkp['' + p] = 1;

                r.push(pages.item('' + p).id);
            }
        }
        return r;
    })();

    if (!pgMap) {
        alert("Invalid reordering string");
        return false;
    }

    var sz = pgMap.length;
    if (sz != pages.length) {
        alert("Page number mismatch -- " + pgMap.length + " given, " + pages.length + " in document");
        return false;
    }

    // at this point, {pgMap} contains a valid permutation

    var i;
    app.scriptPreferences.enableRedraw = false;

    for (i = 0; i < sz; i++)
        pages.itemByID(pgMap[i]).move(LocationOptions.BEFORE, pages[i]);

    app.scriptPreferences.enableRedraw = true;

}

 

 

James Gifford—NitroPress
Legend
February 24, 2023

Wow, on two or three levels. First, a 3,000 page doc. Second, having to reorder the content. Third, reordering content by moving pages. Look at all the pretty red flags! 🙂

 

You might want to outline the document and project here for some more coherent advice that could save you time, effort and frustration. I suspect that any approach at all is going to result in repeated breakage and corruption, if not just confusion and difficulty managing the project and results.

 

Or maybe you have it all under control and just need a simple answer, which is okay... but I had to make the suggestion.