Skip to main content
Known Participant
June 23, 2013
Answered

Centerlines on All Artboards

  • June 23, 2013
  • 1 reply
  • 729 views

Hello Everyone,

I'm trying to create some simple vertical centerlines to be drawn in the same spot on all existing artboards in my document.  What I have so far is creating the proper amount of centerlines but only drawing them in the correct spot on half of the artboards while the other half are being drawn in the space between the artboards.  Here's what I have so far.

function addCenterLines() {

    var doc = app.activeDocument;

    for (var i = 0; i < doc.artboards.length; i++) {

    var abRect = doc.artboards.artboardRect;

    { // Add top line

    var line = doc.pathItems.add();

    line.stroked = true;

    line.setEntirePath( Array( Array(abRect[2]/2, abRect[1]-10), Array(abRect[2]/2, abRect[1]-20) ) );

    };

    { // Add bottom line

    var line = doc.pathItems.add();

    line.stroked = true;

    line.setEntirePath( Array( Array(abRect[2]/2, abRect[3]+10), Array(abRect[2]/2, abRect[3]+20) ) );

    };

    }

}

Here's a screenshot showing the top of a couple of the artboards and the misplaced centerlines in between.

I think I have a great misunderstanding of how the abRect coordinates work, as in which side is [0],[1],[2] and [3], which may be leading to my inability to get this right.  If someone could point me in the right direction so I can sort out my centerlines, I would be greatly appreciative!

Thank you,

Ben

NOTE: The centerlines provided in the print dialog are not an option in this case.

This topic has been closed for replies.
Correct answer CarlosCanto

artboarRect array values are [left, top, right, bottom]

check this sample, I got carried away and added Horizontal lines

function addCenterLines() {

    var doc = app.activeDocument;

    for (var i = 0; i < doc.artboards.length; i++) {

    var abRect = doc.artboards.artboardRect;

    // be more explicit to make it easier to visualize/read/handle

    var abLeft = abRect[0];

    var abTop = abRect[1];

    var abRight = abRect[2];

    var abBottom = abRect[3];

    var abWidth = abRect[2]-abRect[0];

    var abHeight = abRect[1]-abRect[3];

    var abCenter = abWidth/2;

    var abMiddle = abHeight/2;

    { // Add top line

    var line = doc.pathItems.add();

    line.stroked = true;

    line.setEntirePath( Array( Array(abLeft+abCenter, abTop-10), Array(abLeft+abCenter, abTop-20) ) );

    };

    { // Add bottom line

    var line = doc.pathItems.add();

    line.stroked = true;

    line.setEntirePath( Array( Array(abLeft+abCenter, abBottom+10), Array(abLeft+abCenter, abBottom+20) ) );

    };

    { // Add left line

    var line = doc.pathItems.add();

    line.stroked = true;

    line.setEntirePath( Array( Array(abLeft+10, abBottom+abMiddle), Array(abLeft+20, abBottom+abMiddle) ) );

    };

    { // Add right line

    var line = doc.pathItems.add();

    line.stroked = true;

    line.setEntirePath( Array( Array(abRight-10, abBottom+abMiddle), Array(abRight-20, abBottom+abMiddle) ) );

    };

    }

}

addCenterLines ();

1 reply

CarlosCanto
Community Expert
CarlosCantoCommunity ExpertCorrect answer
Community Expert
June 23, 2013

artboarRect array values are [left, top, right, bottom]

check this sample, I got carried away and added Horizontal lines

function addCenterLines() {

    var doc = app.activeDocument;

    for (var i = 0; i < doc.artboards.length; i++) {

    var abRect = doc.artboards.artboardRect;

    // be more explicit to make it easier to visualize/read/handle

    var abLeft = abRect[0];

    var abTop = abRect[1];

    var abRight = abRect[2];

    var abBottom = abRect[3];

    var abWidth = abRect[2]-abRect[0];

    var abHeight = abRect[1]-abRect[3];

    var abCenter = abWidth/2;

    var abMiddle = abHeight/2;

    { // Add top line

    var line = doc.pathItems.add();

    line.stroked = true;

    line.setEntirePath( Array( Array(abLeft+abCenter, abTop-10), Array(abLeft+abCenter, abTop-20) ) );

    };

    { // Add bottom line

    var line = doc.pathItems.add();

    line.stroked = true;

    line.setEntirePath( Array( Array(abLeft+abCenter, abBottom+10), Array(abLeft+abCenter, abBottom+20) ) );

    };

    { // Add left line

    var line = doc.pathItems.add();

    line.stroked = true;

    line.setEntirePath( Array( Array(abLeft+10, abBottom+abMiddle), Array(abLeft+20, abBottom+abMiddle) ) );

    };

    { // Add right line

    var line = doc.pathItems.add();

    line.stroked = true;

    line.setEntirePath( Array( Array(abRight-10, abBottom+abMiddle), Array(abRight-20, abBottom+abMiddle) ) );

    };

    }

}

addCenterLines ();

Inspiring
June 23, 2013

Is this needed… artboards have both center mark and cross hairs… both of which are scriptable options…

Known Participant
June 24, 2013

Thank you, Mark.  I didn't know that but I'll look into it for my next go.  After all of the searching I did here, I couldn't find anything referencing this.  That's probably because I was on the kick of having to solve this via the artboardRect.  I didn't even bother searching for "centerlines" in the scripting reference guide.

Thanks again, Mark.  Very helpful, as always!