Replace Parent Page

Script for Adobe InDesign

Replace all assignments of one parent page with another and optionally delete the replaced page.

  • Choose from list of parent pages
  • Option to delete replaced page
  • Adapt open source to customize or create other scripts
Download
Replace Parent Page
Help me keep making new scripts by supporting my work. Click the PayPal button to contribute any amount you choose. Thank you. William Campbell

How-to Video

How to use the script

The interface has two sections: Parent pages and Options. Set options as desired and click the OK button to proceed.

Section 1: Parent pages

Find parent page — from the drop-down list of parent pages select one to replace.

Replace with — from the drop-down list of parent pages select one to replace the parent page selected above.

Every document page assigned the parent page to find is reassigned the replacement parent page.

Section 2: Options

Delete replaced parent page — once the parent page is replaced, the parent page is removed from the document.

Source code

(download button below)

/*

Replace Parent Page
Copyright 2023 William Campbell
All Rights Reserved
https://www.marspremedia.com/contact

Permission to use, copy, modify, and/or distribute this software
for any purpose with or without fee is hereby granted.

THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

*/

(function () {

    var title = "Replace Parent Page";

    if (!/indesign/i.test(app.name)) {
        alert("Script for InDesign", title, false);
        return;
    }

    // Script variables.
    var doc;
    var doneMessage;
    var error;
    var parentNames;
    var working;

    // Reusable UI variables.
    var g; // group
    var gc1; // group (column)
    var gc2; // group (column)
    var p; // panel
    var w; // window

    // Permanent UI variables.
    var btnCancel;
    var btnOk;
    var cbDeleteParent;
    var listParentFind;
    var listParentReplace;

    // SETUP

    // Script requires open document.
    if (!app.documents.length) {
        alert("Open a document", title, false);
        return;
    }
    doc = app.activeDocument;
    // Create list of parent page names.
    parentNames = doc.masterSpreads.everyItem().name;

    // CREATE WORKING WINDOW

    working = new Window("palette", undefined, undefined, {
        closeButton: false
    });
    working.preferredSize = [300, 80];
    working.add("statictext");
    working.t = working.add("statictext");
    working.add("statictext");
    working.display = function (message) {
        this.t.text = message || "Working... Please wait...";
        this.show();
        this.update();
    };

    // CREATE USER INTERFACE

    w = new Window("dialog", title);
    w.alignChildren = "fill";

    // Panel 'Layers'
    p = w.add("panel", undefined, "Parent pages");
    p.orientation = "column";
    p.alignChildren = "left";
    p.margins = [18, 24, 18, 18];
    // Group of 2 columns.
    g = p.add("group");
    // Groups, columns 1 and 2.
    gc1 = g.add("group");
    gc1.orientation = "column";
    gc1.alignChildren = "left";
    gc2 = g.add("group");
    gc2.orientation = "column";
    gc2.alignChildren = "left";
    // Rows.
    gc1.add("statictext", undefined, "Find parent page:").preferredSize.height = 23;
    listParentFind = gc2.add("dropdownlist", undefined, parentNames);
    listParentFind.preferredSize = [150, 23];
    gc1.add("statictext", undefined, "Replace with:").preferredSize.height = 23;
    listParentReplace = gc2.add("dropdownlist", undefined, parentNames);
    listParentReplace.preferredSize = [150, 23];

    // Panel 'Options'
    p = w.add("panel", undefined, "Options");
    p.alignChildren = "left";
    p.margins = [24, 24, 24, 18];
    cbDeleteParent = p.add("checkbox", undefined, "Delete replaced parent page");

    // Action Buttons
    g = w.add("group");
    g.alignment = "center";
    btnOk = g.add("button", undefined, "OK");
    btnCancel = g.add("button", undefined, "Cancel");

    // Panel Copyright
    p = w.add("panel");
    p.add("statictext", undefined, "Copyright 2023 William Campbell");

    // UI ELEMENT EVENT HANDLERS

    btnOk.onClick = function () {
        if (!listParentFind.selection) {
            alert("Select parent page to find", " ", false);
            return;
        }
        if (
            !listParentReplace.selection ||
            listParentFind.selection.index == listParentReplace.selection.index
        ) {
            alert("Select replacement parent page", " ", false);
            return;
        }
        w.close(1);
    };

    btnCancel.onClick = function () {
        w.close(0);
    };

    // DISPLAY THE DIALOG

    if (w.show() == 1) {
        doneMessage = "";
        try {
            app.doScript(process, ScriptLanguage.JAVASCRIPT, undefined, UndoModes.ENTIRE_SCRIPT, title);
            doneMessage = "Done";
        } catch (e) {
            error = error || e;
            doneMessage = "An error has occurred.\nLine " + error.line + ": " + error.message;
        }
        working.close();
        doneMessage && alert(doneMessage, title, error);
    }

    //====================================================================
    //               END PROGRAM EXECUTION, BEGIN FUNCTIONS
    //====================================================================

    function process() {
        var i;
        var page;
        var parentFind;
        var parentReplace;
        working.display();
        try {
            parentFind = doc.masterSpreads[listParentFind.selection.index];
            parentReplace = doc.masterSpreads[listParentReplace.selection.index];
            for (i = 0; i < doc.pages.length; i++) {
                page = doc.pages[i];
                if (page.appliedMaster == parentFind) {
                    page.appliedMaster = parentReplace;
                }
            }
            if (cbDeleteParent.value) {
                parentFind.remove();
            }
        } catch (e) {
            error = e;
            throw e;
        }
    }

})();
Help me keep making new scripts by supporting my work. Click the PayPal button to contribute any amount you choose. Thank you. William Campbell
Download
Replace Parent Page

For help installing scripts, see How to Install and Use Scripts in Adobe Creative Cloud Applications.

IMPORTANT: scripts are developed for the latest Adobe Creative Cloud applications. Many scripts work in CC 2018 and later, even some as far back as CS6, but may not perform as expected, or run at all, when used in versions prior to 2018. Photoshop features Select Subject and Preserve Details 2.0 definitely fail prior to CC 2018 (version 19) as the features do not exist in earlier versions. For best results use the latest versions of Adobe Creative Cloud applications.

IMPORTANT: by downloading any of the scripts on this page you agree that the software is provided without any warranty, express or implied. USE AT YOUR OWN RISK. Always make backups of important data.

IMPORTANT: fees paid for software products are the purchase of a non-exclusive license to use the software product and do not grant the purchaser any degree of ownership of the software code. Author of the intellectual property and copyright holder William Campbell retains 100% ownership of all code used in all software products regardless of the inspiration for the software product design or functionality.