adobeadobe-illustratorextendscript

Used AI to generate an Adobe Illustrator v28.3 script that groups the contents of each individual artboard. Can't get it to work


What would be the best way to solve the problem of wanting to 'auto group' all artboards?

Opposed to dragging a selection box of each artboard, then hitting CMD+G, each page has 8 artboards and I have probably 80-150 pages to get through.

To those proficient in javascript, does this code resemble something workable or should I pack it in?

var doc = app.activeDocument;

for (var i = 0; i < doc.artboards.length; i++) {
    doc.artboards.setActiveArtboardIndex(i);
    var artboard = doc.artboards[i];
    var itemsOnArtboard = [];

    for (var j = 0; j < doc.pageItems.length; j++) {
        var item = doc.pageItems[j];
        if (artboard.artboardRect.toString() == item.visibleBounds.toString()) {
            itemsOnArtboard.push(item);
        }
    }

    if (itemsOnArtboard.length > 1) {
        var group = doc.groupItems.add();
        for (var k = 0; k < itemsOnArtboard.length; k++) {
            itemsOnArtboard[k].moveToEnd(group);
        }
    }
}

I was expecting Illustrator to cycle through each art board and grouping the contents from each board until it finally reached the last board.

The code seemed to take, beach ball spun for a bit, but no result.


Solution

  • Probably something like this:

    var doc = app.activeDocument;
    
    // create boxes
    for (var i=0; i<doc.artboards.length; i++) {
        var r = doc.artboards[i].artboardRect;
        var box = doc.pathItems.rectangle(r[1], r[0], r[2]-r[0], -r[3]+r[1]);
        box.filled = false;
        box.stroked = false;
    }
    
    // group items
    for (var i=0; i<doc.artboards.length; i++) {
        doc.artboards.setActiveArtboardIndex(i);
        doc.selectObjectsOnActiveArtboard();
        app.executeMenuCommand('group');
    }
    

    The script creates boxes around all artboards of the active document and for each artboard groups all its items.