Skip to main content
Disposition_Dev
Legend
December 31, 2014
Answered

Create element at center of artboard, move to next artboard and repeat

  • December 31, 2014
  • 2 replies
  • 2118 views

Hey All! I've been lurking around here for quite some time and i've always been able to find what i needed. Until now. 😃

I'm writing a Javascript to create multiple rectangles on the center of the active artboard and then repeat for all remaining artboards. However, i'm getting hung up on how to shift focus to the next artboard. I've read about  "setActiveArtboardIndex();" but whenever i try to use this feature, i get the error "setActiveArtboardIndex(); is not a function".

Here's the code i'm working with. Could anyone please tell me how to get the loop to switch to the next artboard before creating the next set of color blocks?

//Beginning of Script

#target illustrator

if (app.documents.length > 0) {

var docRef = app.activeDocument

}

else {

alert("No Document Open");

}

//Add Color Block Layer

artLayers.locked = true;

function existBlockLayer(layers, name) {

    for (i=0; i<docRef.layers.length; i++){

        if (layers.name===name) return true;

        }

    return false;

    }

          

if (existBlockLayer(docRef.layers,"Color Blocks")) {

    var removeLayer = artLayers.getByName("Color Blocks");

    removeLayer.locked = false;

    removeLayer.remove();

    var blockLayer = docRef.layers.add();

    blockLayer.name = "Color Blocks"

    }

else {

    var blockLayer = docRef.layers.add();

    blockLayer.name = "Color Blocks"

    }

blockLayer.locked = false;

//Get Document Swatches

var swatchList = docRef.swatches;

var artLayers = docRef.layers

var aB = docRef.artboards

var colorList = [];

for(var k=0; k< swatchList.length; k++) {

    if (swatchList.name.toLowerCase() != "thru-cut" && swatchList.name.toLowerCase() != "[registration]"//

    && swatchList.name.toLowerCase() != "[none]" && swatchList.name.toLowerCase() != "cut line"){

     

       colorList.push(swatchList.name);

           }

       }

//Create 1 Color Block per Swatch and Apply Swatch

for (var a=0; a<aB.length; a++) {

  

  

  

    var aBLeft = aB.artboardRect[0];

    var aBRight = aB.artboardRect[2]

    var aBTop = aB.artboardRect[1]

    var aBBottom = aB.artboardRect[3]

  

  

    var W = (aBRight/2);

    var H = (aBBottom/2);

  

    var posH = (H/2);

  

    var posW = (W/2);

    alert(W + " width " + H + " height");

 

    var activeArtboard = aB;

  

    //var aBPlus = setActiveArtboardIndex(a);

  

    for(var c=0,len=colorList.length;c<len;c++){

        var newBlock = docRef.pathItems.rectangle(posH, posW,10,10);

        newBlock.stroked = false;

      

   }  

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

        docRef.pathItems.fillColor=docRef.swatches.getByName(colorList).color;

    }

}

   

artLayers.locked = false;

blockLayer.zOrder (ZOrderMethod.SENDTOBACK);

blockLayer.locked = true;

Thanks in advance all!

This topic has been closed for replies.
Correct answer pixxxelschubser

My apologies. I feel I'm still being unclear. I need every instance of that teal box to be created at the center of each artboard.


Happy New Year williamadowling,

there is only some little mistakes in your code.

For your main problem:

Replace these four lines:

var W = (aBRight/2);

var H = (aBBottom/2);

var posH = (H/2);

var posW = (W/2);

with these two lines:

var posW = aBLeft+(aBRight-aBLeft)/2;

var posH = aBTop+(aBBottom-aBTop)/2;

Have fun

P.S.

and further – this:

  1. else { 
  2. alert("No Document Open"); 

should be the end of your code

2 replies

aiScripting
Inspiring
September 3, 2015

I built me a simple little "centerMe" function that I use all the time

Hope this is useful to other!

function centerMe(me,obj){

            var x = obj.left + (obj.width / 2);

            var y = obj.top - (obj.height / 2);

   

            me.position = [x - (me.width / 2),y + (me.height / 2)]

   

    };//end of centerMe function

Disposition_Dev
Legend
December 31, 2014

i assume the solution has something to do with resetting the ruler origin, but i cannot seem to figure out the correct syntax to do that either.

also, after i posted this i noticed an error in that i was dividing by two twice (which explains why my blocks weren't ending up at the center of the page. i corrected that.

as the script is written now, the first color block ends up exactly where i want it. the second one, however, gets placed in the mathematical center of the two artboards instead of the center of the next artboard.

Silly-V
Legend
December 31, 2014

try aB.setActiveArtboardIndex(a); 

Disposition_Dev
Legend
December 31, 2014

ok. that got rid of the error.. Thanks! = )

however, it doesn't seem to do what i had expected. if i manually click on an artboard to make it the active artboard, my rulers adjust and the ruler origin adjusts to the active artboard. i had assumed that aB.setActiveArtboardIndex(a); would do the same, however, it doesn't.

can you assist me with the rulerOrigin syntax?? this is what i have right now, but it doesn't seem to do anything..

    var aBLeft = aB.artboardRect[0];

    var aBRight = aB.artboardRect[2]

    var aBTop = aB.artboardRect[1]

    var aBBottom = aB.artboardRect[3]

    var activeArtboard = aB;

    var newRuler = activeArtboard.rulerOrigin[aBLeft,aBTop]

    aB.setActiveArtboardIndex(a);

    

shouldn't this set the new ruler origin to the top left corner of artboard? or do i misunderstand how to set the ruler origin?

Thanks again for your help.