Skip to main content
TestriteVisual
Inspiring
June 13, 2023
Answered

How to set the Layout Margins to equal the Document Setup Margins via Javascript

  • June 13, 2023
  • 1 reply
  • 502 views

Greetings, 

The code below is setting the File->Document Setup "Margins" to 0 for all sides,

    graphicTemplate.marginPreferences.properties = {
        top : 0,
        left: 0,
        right: 0,
        bottom:0
    };

but I noticed it's leaving the "Margin" values under Layout -> Margins and Columns on some default value of 0.5".

 

Can you please provide the javascript needed to have the Layout Margins equal the Document Margins.

 

Thank you for your help on this.

This topic has been closed for replies.
Correct answer m1b

Hi @TestriteVisual, the document's marginPreferences only apply to new pages added with no master applied. If pages already have margin preferences applied, you need to explicitly target the individual pages (including master pages, if any). Here is an example:

/**
 * @author m1b
 * @discussion https://community.adobe.com/t5/indesign-discussions/how-to-set-the-layout-margins-to-equal-the-document-setup-margins-via-javascript/m-p/13863018
 */
function main() {

    var doc = app.activeDocument,
        allPages = doc.masterSpreads.everyItem().pages.everyItem().getElements().concat(doc.pages.everyItem().getElements());

    for (var i = 0; i < allPages.length; i++) {
        allPages[i].marginPreferences.properties = {
            top: 0,
            left: 0,
            right: 0,
            bottom: 0
        };
    }

}

app.doScript(main, ScriptLanguage.JAVASCRIPT, undefined, UndoModes.ENTIRE_SCRIPT, 'Set All Pages Margins');

 - Mark

1 reply

m1b
Community Expert
m1bCommunity ExpertCorrect answer
Community Expert
June 13, 2023

Hi @TestriteVisual, the document's marginPreferences only apply to new pages added with no master applied. If pages already have margin preferences applied, you need to explicitly target the individual pages (including master pages, if any). Here is an example:

/**
 * @author m1b
 * @discussion https://community.adobe.com/t5/indesign-discussions/how-to-set-the-layout-margins-to-equal-the-document-setup-margins-via-javascript/m-p/13863018
 */
function main() {

    var doc = app.activeDocument,
        allPages = doc.masterSpreads.everyItem().pages.everyItem().getElements().concat(doc.pages.everyItem().getElements());

    for (var i = 0; i < allPages.length; i++) {
        allPages[i].marginPreferences.properties = {
            top: 0,
            left: 0,
            right: 0,
            bottom: 0
        };
    }

}

app.doScript(main, ScriptLanguage.JAVASCRIPT, undefined, UndoModes.ENTIRE_SCRIPT, 'Set All Pages Margins');

 - Mark

TestriteVisual
Inspiring
June 14, 2023

Awesome @m1b , this worked perfectly! I added your code block below the Document Margins code, and now the Layout Margins are also set to 0. Thank you!!