Advertisement


How do I position an InDesign object using Javascript?


Question

I'm working on a calendar in InDesign and I have a circle object (oval) that I want to position over the day of the month:

enter image description here

I'm pretty sure it should be possible to write a script that sets the x,y position properties of the oval object on each page to the proper values. I've already got the seven x values and the 6 y values that make up the month-grid, but I'm very new to JavaScript so I don't know how to get started. The sample scripts I saw were very involved, and I think what I want to do is much simpler, albeit with some iterations, etc.

If anyone can help me script the basics to get me started I would greatly appreciate it.

2016/09/16
1
0
9/16/2016 10:19:00 PM

Here is a proposal:

    var main = function() {
    var doc = app.properties.activeDocument,
    today = (new Date()).getDate(),
    found, x1, x2, y1, y2;


    if ( !doc ) return;

    app.findGrepPreferences = null;
    app.findGrepPreferences.properties = {
        findWhat : "^"+today+"$",
        pointSize:24
    };

    found = doc.findGrep();

    if ( !found.length ) {
        alert("Nothing found");
        return;
    }

    found = found[0];

    if ( !found.parentTextFrames.length )  {
        alert( "The text may be overriding. Script can't continue" );
        return;
    }

    x1 = found.horizontalOffset;
    x2 = found.endHorizontalOffset;
    y1 = found.baseline;
    y2 = y1-found.ascent;
    pz = found.pointSize;
    xc = (x1+x2)/2;
    yc = (y1+y2)/2;

    var p = found.parentTextFrames[0].parentPage;

    var dateLayer = doc.layers.item ( "dateLayer");
    dateLayer.isValid && dateLayer.remove ();
    dateLayer = doc.layers.add ( {name:"dateLayer"} );
    dateLayer.move ( LocationOptions.AT_BEGINNING );

    var o = p.ovals.add (  {
        geometricBounds:[yc-pz/3, xc-pz/3,yc+pz/3, xc+pz/3],
        strokeColor:"Black",
        strokeWeight:2,
        itemLayer:dateLayer
    });

  }

  var u;
  app.doScript ( "main();",u,u,UndoModes.ENTIRE_SCRIPT, "Mark date" );
2016/09/21
3
9/21/2016 3:48:00 PM