Site logo

How to set many properties at once — don't use “with”

Harbs:

"with" is generally frowned on as bad practice (for a number of reasons). This is a much better way to set many properties at once:

myDocument = app.documents.add(); 
myDocument.pages.item(0).marginPreferences.properties = { 
          columnCount : 3,
          //columnGutter can be a number or a measurement string. 
          columnGutter : "1p",
          bottom : "6p",
          //When document.documentPreferences.facingPages == true, 
          //"left" means inside; "right" means outside. 
          left : "6p",
          right : "4p", 
          top : "4p" 
}

and:

var myDoc = app.activeDocument;
var myPg = myDoc.pages.item(-1);
var myLastPage = myDoc.pages.add(LocationOptions.AT_END, myPg);
myLastPage.marginPreferences.properties = {
          bottom : "6p",
          left : "6p",
          right : "4p", 
          top : "4p"
}

Part of the reason I say not to use with particularly in InDesign comes from experience.

"with" performs poorly performance-wise and jumps through all kinds of hoops to work at all. I once had a really obscure issue with InDesign dialogs, "with" and local variables. I don't remember all the details but the solution was to get rid of "with"...

The reason Ole used "with" for the script samples was to retain consistancy between the different languages in the examples -- not because it was inherently good...

When setting multiple properties, "properties" has the advantage that you only need to interact with the C++ level once, so it's inherently more efficient than any other method. The fact that it's easy to read is just an added plus...