Using Javascript to change marginPreferences, then restore them

Contributor ,
Jul 02, 2022 Jul 02, 2022

Copy link to clipboard

Copied

Hi all,

I'm working on a script that changes the left and right margin of all pages to 0.

However, I want to be able to store the original margins (see lines 8 & 9) so I can restore them later (see lines 25 & 26).

I can't seem to work out exactly how that's done without getting expected array errors, etc. 

Be great if someone can help. J

 

app.scriptPreferences.measurementUnit = MeasurementUnits.MILLIMETERS;

var myDoc = app.activeDocument;
var myMasters = myDoc.masterSpreads.everyItem().pages.everyItem();
var myPages = myDoc.pages.everyItem();

//store original preferences???
var origPagePrefs = myPages.marginPreferences;
var origMasterPrefs = myMasters.marginPreferences;

//left and right margins set to 0mm. Uncomment to include more.
var marginPrefs = {
    left: 0,
    right: 0,
    //top: 10,
    //bottom: 10,
    //columnCount: 6,
    //columnGutter: 5
}

myMasters.properties = {marginPreferences : marginPrefs};
myPages.properties = {marginPreferences : marginPrefs};

//restore original margins
//myMasters.properties = {marginPreferences : origMasterPrefs};
//myPages.properties = {marginPreferences : origPagePrefs};

 

 

TOPICS
Scripting

Views

136

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Adobe Community Professional , Jul 03, 2022 Jul 03, 2022

Hi @JustyR , if I trace Ariel’s allPagePrefs object it returns undefined, so I think that’s the problem:

 

 

var myDoc = app.activeDocument;
var myPages = myDoc.pages.everyItem();
//store original preferences???
var myPageMargins = [];
var allPagePrefs = myPages.marginPreferences;
for (var i = 0; i < allPagePrefs.length; i++){
    $.writeln(allPagePrefs.top)
    //returns undefined
	myPageMargins.push({top: allPagePrefs.top, left: allPagePrefs.left, bottom: allPagePrefs.bottom, right: allPagePre
...

Likes

Translate

Translate
Adobe Community Professional ,
Jul 02, 2022 Jul 02, 2022

Copy link to clipboard

Copied

Hi @JustyR, just a quick note to say that you might be making this task difficult for yourself by using .everyItem(), which is a special beast and once you have an .everyItem (for example your var myPages is an .everyItem) asking for further properties of that will give you—I think, I'm going on memory hereresults nested in (at least one level of) Arrays: each element of the array will be another array (or indesign collection) of the property you asked for.

 

So my first question/suggestion is: can you do this without getting .everyItem()'s marginPreferences? For example, maybe all the pages are expected to have the same marginPreferences? If this is the case, don't use everyItem(). But, if not, and they must be assumed in worse case to all be different, then using .everyItem() might be great. However you will need to test the results carefully until you understand exactly what, for example, myDoc.pages.everyItem().marginPreferences is actually giving you—from memory I think it gives an array, with each element the marginPreferences of one of the pages in myPages. You may benefit from logging the results, for example give this a try:

$.writeln(myPages.marginPreferences[0].toSpecifier());

And look at what Indesign believes the objects are. Please see this article and this article by @Marc Autret, which explains the everyItem() method very well. If you don't understand what it does (and as importantly—what it gives you back) it will be very difficult to troubleshoot problems.

 

So back to your script: I think myPages.properties = {marginPreferences:origPagePrefs}; will fail because you are trying to set every page's marginPreferences to an Array of (I think!) marginPreferences. I'm almost certain that it won't work like that. So I think you may have to restore the original margins using a conventional loop. But it's very likely that an expert will come along with more knowledge and a better approach.

- Mark

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Jul 03, 2022 Jul 03, 2022

Copy link to clipboard

Copied

Thanks, Mark. Trying to make sense of what you said, as well as Marc's explanation, I realised I need to better understand the hierarchy of information in the API (here). At the moment, it isn't at all logical to me where each bit goes, so I've finally downloaded Peter Kahrel's book and will start reading that today.

 

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
Jul 03, 2022 Jul 03, 2022

Copy link to clipboard

Copied

I think that is sensible, and will save you time in the long run. When you finally get your script masterpiece working—it will be very rewarding. 🙂

- Mark

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Valorous Hero ,
Jul 03, 2022 Jul 03, 2022

Copy link to clipboard

Copied

What Mark said is basically correct. You have to resort to a regular loop at some point.

Also note the use of myPages = myPages.getElements(), which is necessary to turn the pages collection that has been created with everyItem() into loopable array.

app.scriptPreferences.measurementUnit = MeasurementUnits.MILLIMETERS;

var myDoc = app.activeDocument;
var myPages = myDoc.pages.everyItem();

//store original preferences???
var myPageMargins = [];
var allPagePrefs = myPages.marginPreferences;
for (var i = 0; i < allPagePrefs.length; i++){
	myPageMargins.push({top: allPagePrefs.top, left: allPagePrefs.left, bottom: allPagePrefs.bottom, right: allPagePrefs.right});
}

//left and right margins set to 0mm. Uncomment to include more.
var marginPrefs = {
    left: 0,
    right: 0
    //top: 10,
    //bottom: 10,
    //columnCount: 6,
    //columnGutter: 5
}

myPages.properties = {marginPreferences : marginPrefs};
myPages = myPages.getElements();
//restore original margins
for (var i = 0; i < myPages.length; i++){
	myPages[i].properties = {marginPreferences: myPageMargins[i]};
}

(I've omitted the master pages margins for brevity.)

Ariel

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Jul 03, 2022 Jul 03, 2022

Copy link to clipboard

Copied

Thank you, Ariel. It does change the margins but it doesn't appear to change them back to the original setting.

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
Jul 03, 2022 Jul 03, 2022

Copy link to clipboard

Copied

Hi @JustyR , if I trace Ariel’s allPagePrefs object it returns undefined, so I think that’s the problem:

 

 

var myDoc = app.activeDocument;
var myPages = myDoc.pages.everyItem();
//store original preferences???
var myPageMargins = [];
var allPagePrefs = myPages.marginPreferences;
for (var i = 0; i < allPagePrefs.length; i++){
    $.writeln(allPagePrefs.top)
    //returns undefined
	myPageMargins.push({top: allPagePrefs.top, left: allPagePrefs.left, bottom: allPagePrefs.bottom, right: allPagePrefs.right});
}

 

 

Does this work for you?:

 

 

var myDoc = app.activeDocument;
var myPages = myDoc.pages;

//store original preferences
var myPageMargins = [];
for (var i = 0; i < myPages.length; i++){
    myPageMargins.push(myPages[i].marginPreferences.properties);
    //set new margins
    myPages[i].marginPreferences.properties = {left: 0, right: 0};
    //do something brfore restoring margins
};   

//restore original margins
for (var i = 0; i < myPages.length; i++){
	myPages[i].marginPreferences.properties = myPageMargins[i];
}

 

 

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Jul 03, 2022 Jul 03, 2022

Copy link to clipboard

Copied

LATEST

Thanks, Rob. Very helpful and insightful.

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines