Skip to main content
Participant
November 10, 2014
Question

Using "with" Blocks: Any Problem?

  • November 10, 2014
  • 1 reply
  • 180 views

Is the use of with blocks frowned upon among InDesign scripters these days? I'm aware that they are often disfavored in the web world, but I see them used in some sample code I've seen, and being analogous to AppleScript tell blocks, it seems to cut out a lot of repetition or creating non-essential variables. Is there really a risk? Some of the reasoning I've seem mentioned to avoid it involve scoping of variables with the same name, but that could easily be avoided in a personal script, no?

This topic has been closed for replies.

1 reply

Trevor:
Legend
November 10, 2014

Hi Rc

In my opinion there are 2 issues regarding the use of with.

1) General issues which are the sources of the frowns and the reason why ECMA V5 strict mode bans it use see with - JavaScript | MDN I can not see why those reasons should not apply to Indesign.

2) Specific InDesign issues.

A quick intro for the wider audience.

With can be as far as I know used in 3 ways 1) Method, 2) Property Set 3) Property Get.

The following 2 scripts show how we can apply these 3 ways using and not using with,

// With With

var myRec = app.activeDocument.rectangles[0]; // Note script will crash here if you don't use your brains

var s;

with (myRec) {

    move ([],[10,30]); // method

    rotationAngle = 45; // property set

    shearAngle = 15; // property set

    strokeWeight = 3;// property set

    fillColor = app.activeDocument.colors.itemByName("Cyan");// property set

    strokeColor = app.activeDocument.colors.itemByName("Magenta");// property set

    s = shearAngle;// property Get

}

// Without With

var myRec = app.activeDocument.rectangles[0]; // Note script will crash here if you don't use your brains

var s;

myRec.move ([],[10,10]); // method

myRec.properties = {

    rotationAngle: 45,// property set

    shearAngle: 15, // property set

    strokeWeight: 3,// property set

    fillColor: app.activeDocument.colors.itemByName("Cyan"),// property set

    strokeColor: app.activeDocument.colors.itemByName("Magenta"),// property set

}

s = myRec.shearAngle // property get

If you run both script you might think there's not much in it.

Regarding the "method" and "get" usages besides the general "with" issues then I don't think there's much of a difference.

However regarding the "set" usage there can be a very significant difference.  You can see this by running each script and undo the changes made to your rectangle by each script.

Using with you have to undo each and every property setting as each one is applied in turn.

Using properties all the properties are set at the same time and only one undo is needed. (Each property setting can result in a document redraw and a disk writing.

Setting the properties in one go can have a very large difference to the run time of the script.  Although one can reduce this by using redraw = false and FAST_ENTIRE_UNDOMODE using these can sometime be problematic.

However even when scripting Illustrator and the like where one cannot set properties on mass the general issues apply.

My pennies worth,

Trevor