Skip to main content
Bedazzled532
Inspiring
April 5, 2023
Answered

Unlock Master page items

  • April 5, 2023
  • 1 reply
  • 1682 views

Hello,

This script unlocks all the master page items from documents pages.  You dont have to Ctrl_Shift+ Click on each page to unlock master page items.

 

Issue with the script is that it works only if the document does not have facing pages. What modifications would be requried here to enable for both facing pages and non facing pages document?

 

Here is the script:

//DESCRIPTION:Overrides a selected item of the master page in the entire document.
#target InDesign

if(! (app.selection.length == 1) ){
alert("Error\rSelect exactly one item on a master page and try again.");
exit();
}

var sel = app.selection[0];
var masterSpread = sel.parentPage.parent;

if(! (masterSpread.constructor.name == "MasterSpread") ){
alert("Error\rThe selected item is not on a master page. Select an item on a master page and try again.");
exit();
}

var doc = app.activeDocument;
var side = sel.parentPage.side;

for(var i = 0; i < doc.pages.length; i++){
var page = doc.pages[i];
if(page.side == side && page.appliedMaster == masterSpread){
sel.override(page);
}
}

 

Thanks

This topic has been closed for replies.
Correct answer m1b

Hi @Bedazzled532, well done on seeing what to change in the script to make it work—you removed the predicate that was interfering with your needs. However there is another problem: the script will crash when the selected page item doesn't exist on the page, which will happen when the selected page item is on, say, the left-hand page of the master spread, but the script is checking page 1 which is a right-hand page. I've added a try-catch that ignores pages that get this error.

 

And a counter is always nice so that you know that something happened!

- Mark

 

function main() {

    var doc = app.activeDocument;
    var sel = doc.selection[0];

    if (
        sel == undefined
        || !sel.isValid
        || !sel.hasOwnProperty('parentPage')
        || sel.parentPage.parent.constructor.name !== "MasterSpread"
    ) {
        alert("Error\rSelect exactly one item on a master page and try again.");
        return;
    }

    var masterSpread = sel.parentPage.parent,
        counter = 0;

    for (var i = 0; i < doc.pages.length; i++) {
        var page = doc.pages[i];
        if (page.appliedMaster == masterSpread) {
            try {
                sel.override(page);
                counter++;
            } catch (error) {
                // item doesn't exist on that page
            }
        }
    }

    alert('The selected item was overridden on ' + counter + ' pages.')

};

app.doScript(main, ScriptLanguage.JAVASCRIPT, undefined, UndoModes.ENTIRE_SCRIPT, 'Override Selected Master Page Item');

1 reply

Bedazzled532
Inspiring
April 5, 2023

I have changed the following code  :

if(page.side == side && page.appliedMaster == masterSpread)

TO

if(page.appliedMaster == masterSpread)

AND

it worked!

Any other better alternative ?

Thanks.

m1b
Community Expert
m1bCommunity ExpertCorrect answer
Community Expert
April 5, 2023

Hi @Bedazzled532, well done on seeing what to change in the script to make it work—you removed the predicate that was interfering with your needs. However there is another problem: the script will crash when the selected page item doesn't exist on the page, which will happen when the selected page item is on, say, the left-hand page of the master spread, but the script is checking page 1 which is a right-hand page. I've added a try-catch that ignores pages that get this error.

 

And a counter is always nice so that you know that something happened!

- Mark

 

function main() {

    var doc = app.activeDocument;
    var sel = doc.selection[0];

    if (
        sel == undefined
        || !sel.isValid
        || !sel.hasOwnProperty('parentPage')
        || sel.parentPage.parent.constructor.name !== "MasterSpread"
    ) {
        alert("Error\rSelect exactly one item on a master page and try again.");
        return;
    }

    var masterSpread = sel.parentPage.parent,
        counter = 0;

    for (var i = 0; i < doc.pages.length; i++) {
        var page = doc.pages[i];
        if (page.appliedMaster == masterSpread) {
            try {
                sel.override(page);
                counter++;
            } catch (error) {
                // item doesn't exist on that page
            }
        }
    }

    alert('The selected item was overridden on ' + counter + ' pages.')

};

app.doScript(main, ScriptLanguage.JAVASCRIPT, undefined, UndoModes.ENTIRE_SCRIPT, 'Override Selected Master Page Item');
Bedazzled532
Inspiring
April 6, 2023

@m1b Thanks a ton for this help. I really appreciate it.

 

Thanks once again.