Skip to main content
Inspiring
August 21, 2015
Answered

Applying custom MasterPage using Extendscript

  • August 21, 2015
  • 1 reply
  • 1454 views

Is it even possible?

The closest I've gotten to it is about 2 steps shy of actually applying the custom masterpage.

Fcodes([app.GetNamedCommand("MasterPageUsage").Fcode])

This gets the dialog box open but how do you choose the custom masterpage by name and apply it without opening a dialog box?

And on the flip side, if a custom masterpage is already set, how do you change it back to the default, "Right"?

Cheers

This topic has been closed for replies.
Correct answer cudspan

Actually, your instructions only set the background.  If the master page has a different layout (like the FIRST page might have more white space above the text frame), then you actually have to apply the master page formatting, like so...

var doc = app.ActiveDoc;

var first = doc.GetNamedMasterPage('First');

var right = doc.GetNamedMasterPage('Right');

var left = doc.GetNamedMasterPage('Left');

...

// If you have a page that needs "First", then you can say:

page.ApplyPageLayout(first);

//To reset all pages to defaults, you would need to say:

page = doc.FirstBodyPageInDoc;

while(page.ObjectValid()) {

     if(page.PageIsRecto) {

          page.ApplyPageLayout(right);

     } else {

          page.ApplyPageLayout(left);

     }

}

1 reply

Inspiring
August 21, 2015

To my understanding, the Fcodes command is a way to simulate user input, therefore you get the dialog.

You can set the master page using this code:

// Optional: Get the current page of the active document

var doc, page;

doc = app.ActiveDoc;

page = doc.CurrentPage;

// Apply a master page called "Separator"

page.PageBackground = Constants.FV_BGD_OTHER;

page.MasterPage = "Separator";

To revert to the default master page, use

page.PageBackground = Constants.FV_BGD_DEFAULT;

skinjpAuthor
Inspiring
August 25, 2015

Excellent. Thank you!