Skip to main content
Sunil Yadav
Legend
September 22, 2020
Answered

Margin Preferences Scripting Exception Or Bug?

  • September 22, 2020
  • 3 replies
  • 638 views

Hi Everyone,

I am stuck here. I am confused whether it is a bug or I misunderstood.

When I am changing margin preferences of individual pages the code works. But when I am trying to change margin preferences of all pages at once then code doesn't work.

This code works:

 

for(var i = 0; i < app.documents[0].pages.length; i++){
    app.documents[0].pages[i].marginPreferences.left = 12;
    app.documents[0].pages[i].marginPreferences.right = 12;
    app.documents[0].pages[i].marginPreferences.top = 12;
    app.documents[0].pages[i].marginPreferences.bottom = 12;
    }

 

& This code doesn't work:

 

app.documents[0].pages.everyItem().marginPreferences.left = 12;
app.documents[0].pages.everyItem().marginPreferences.right = 12;
app.documents[0].pages.everyItem().marginPreferences.top = 12;
app.documents[0].pages.everyItem().marginPreferences.bottom = 12;

 

And there is one more point to mention:

I can access margin preference of document like :

 

app.documents[0].marginPreferences.left = 12;
app.documents[0].marginPreferences.right = 12;
app.documents[0].marginPreferences.top = 12;
app.documents[0].marginPreferences.bottom = 12;

 

But I am not able to access these :

 

app.documents[0].marginPreferences.columnCount = 2;
app.documents[0].marginPreferences.columnGutter = 25;

 

I need to understand whether this has to do with any specific version of InDesign or is there any limitation or any other reason.

 

Please guide me on this.

I am using Windows OS & InDesign CC 2019.

 

Thanks

Sunil

 

This topic has been closed for replies.
Correct answer SychevKA

hi, try this code:

app.documents[0].pages.everyItem().properties = {
	marginPreferences:{left:12,right:12,top:12,bottom:12}
}

3 replies

Community Expert
September 22, 2020

Note:

document.pages.everyItem()

does not return an array. It is returning a collection of pages that, at first glance, looks like an array.

 

You'll get an array if you do:

document.pages.everyItem().getElements();

 

Read about everyItem() and its various implications on Marc Autret's superb website:

https://www.indiscripts.com/post/2010/06/on-everyitem-part-1

https://www.indiscripts.com/post/2010/07/on-everyitem-part-2

 

FWIW: Setting more than one property at one time using property properties is a very clever solution.

 

Regards,
Uwe Laubender

( ACP )

Sunil Yadav
Legend
September 22, 2020

I am always your BIG fan.

 

Thank you for your time & effort.

SychevKA
SychevKACorrect answer
Inspiring
September 22, 2020

hi, try this code:

app.documents[0].pages.everyItem().properties = {
	marginPreferences:{left:12,right:12,top:12,bottom:12}
}
Sunil Yadav
Legend
September 22, 2020

For setting margin preferences of all pages SychevKA's code works very well.

And I understand now that I can not access columnCount & ColumnGutter value from marginPreferences of Document.

 

I understood.

[Edited]

I agree with Uwe Laubender. 

Uwe Laubender said : Setting more than one property at one time using property properties is a very clever solution.

 

Thank you very much for Effort.

And Thank you everybody to take your valuable time to clear my doubts indeed.

 

Thanks...

SychevKA
Inspiring
September 22, 2020

in fact, the properties change not once, but as many times as there are pages in the document, so please write with doscript

Charu Rajput
Community Expert
Community Expert
September 22, 2020

Hi Sunil,

I just noticed while debugging when you try to use everyItem(), marginPreferences is return as an array. So, if you try following code

app.documents[0].pages.everyItem().marginPreferences.length

This will give you length as 1

app.documents[0].pages.everyItem().marginPreferences[0].left = 12;
app.documents[0].pages.everyItem().marginPreferences[0].right = 12;
app.documents[0].pages.everyItem().marginPreferences[0].top = 12;
app.documents[0].pages.everyItem().marginPreferences[0].bottom = 12;

 

The above will work with everyItem(). I have tried on mac Indesign 2020

Best regards
Sunil Yadav
Legend
September 22, 2020

Thank you Charu for taking for effort that you put for me.

I also tested this code.

But what i realized I think you might be getting value of below line only 1. Because you might have only one page in your document. What I noticed is that how many pages you have that much array you will get in return.

app.documents[0].pages.everyItem().marginPreferences.length

 Opposite to that the way SychevKA is setting those values for margin that is the best solution I think.

 

Thanks

Sunil