Search code examples
scriptingadobe-illustrator

Centering Objects to Multiple Artboards with Illustrator Script


Assume, I have 5x5 25 artboards in Illustrator with different letters on each. I want them to be centered on each artboard. Is there a script for it? I have checked but generally they are all working for one artboard at once.


Solution

  • Here you go:

    var doc = app.activeDocument;
    
    for (var i=0; i<doc.artboards.length; i++) {
        app.selection = null;
        doc.artboards.setActiveArtboardIndex(i);
        doc.selectObjectsOnActiveArtboard();
        to_center(doc.artboards[i], app.selection[0]);
        app.selection = null;
    }
    
    function to_center(artboard, item){
        var artboard_x = artboard.artboardRect[0] + artboard.artboardRect[2];
        var artboard_y = artboard.artboardRect[1] + artboard.artboardRect[3];
        var x = (artboard_x - item.width)/2;
        var y = (artboard_y + item.height)/2;
        item.position = [x, y];
    }
    

    The script puts a first item on every artboard in the middle of the artboard.

    Before:

    enter image description here

    After:

    enter image description here