“This will require a nesting algorithm” You may be overthinking it. OP hasn’t said if they want the artboards to be automatically reordered or not. They may require the artboards to appear in the exact order listed in the CSV. Or the size and number of artboards may be small enough that they will all fit into a single AI document without rearrangement. Or perhaps OP prefers any overflow is moved to another document. All questions to ask when developing a production solution. Here is a simple tiling algorithm which creates the artboards in the listed order. It assumes the ruler origin is already placed in the top-left corner of the document. I have hardcoded some artboard sizes for demonstration purposes; reading this data from a file is left as an exercise. (function () {
var artboardSizes = [ // test data; sizes are in inches
{w: 95, h: 10},
{w: 10, h: 43},
{w: 26, h: 31},
{w: 80, h: 36},
{w: 19, h: 12},
{w: 30, h: 10},
];
function in2pt(n) {
return n * 72;
}
var doc = app.activeDocument;
var oldArtboard = doc.artboards[0]; // the original artboard will be discarded later
var MAX_WIDTH = in2pt(224); // this assumes ruler origin is near top-left of document
var PAD_X = in2pt(0.1), PAD_Y = in2pt(0.1); // spacing between artboards
var x = 0, y = 0, rowHeight = 0;
for (var i = 0; i < artboardSizes.length; i++) {
var size = artboardSizes[i];
var w = in2pt(size.w), h = in2pt(size.h);
if (x + w > MAX_WIDTH) { // will artboard fit on current row? if not, start a new row
x = 0;
y = y - rowHeight - PAD_Y;
rowHeight = 0;
}
rowHeight = Math.max(rowHeight, h);
doc.artboards.add([x, y, x + w, y - h]); // this will throw if an artboard is too large
x += w + PAD_X;
}
oldArtboard.remove();
})(); `MAX_WIDTH` is the distance from the origin to the right side of the document, which determines when to start a new row. The script just throws an error if an artboard is too wide to fit the document or falls off the bottom, but those cases can also be handled if needed. If reordering the artboards to fit more efficiently, quick and crude is to sort the dimensions by height: artboardSizes.sort(function (a, b) { return a.h < b.h; }); It is not an optimal fit but may be “good enough”. Smarter packing algorithms can be found online and adapted to run on Illustrator’s ancient ExtendScript (ES3, aka JavaScript 1999) if OP requires a tighter fit.
... View more