Please continue this script:#target illustrator

// Helper function to add artboards and centered rectangles
function createArtboardsAndRectangles(filenames) {
    var artboardSize = 200;
    var margin = 20;
    var cols = Math.ceil(Math.sqrt(filenames.length));
    var rows = Math.ceil(filenames.length / cols);
    
    // Create document with initial artboard
    var doc = app.documents.add(
        DocumentColorSpace.RGB,
        cols * (artboardSize + margin),
        rows * (artboardSize + margin)
    );
    
    // Make use of the first artboard as the first item
    var initialAbRect = doc.artboards[0].artboardRect;
    var rectangles = [];
    
    for (var i = 0; i < filenames.length; i++) {
        var row = Math.floor(i / cols);
        var col = i % cols;
        var left = col * (artboardSize + margin);
        var top = doc.height - (row * (artboardSize + margin));
        
        // Only add a new artboard if it is not the first one
        if(i > 0) {
            var artboardRect = [left, top, left + artboardSize, top - artboardSize];
            doc.artboards.add(artboardRect);
        } else {
            // Adjust the first artboard size
            doc.artboards[0].artboardRect = [initialAbRect[0], initialAbRect[1], initialAbRect[0] + artboardSize, initialAbRect[1] - artboardSize];
        }
        
        doc.artboards[i].name = filenames[i].substring(0, filenames[i].lastIndexOf(‘.’));
        
        // Each rectangle is exactly the artboardSize and centered within the artboard
        var rect = doc.pathItems.rectangle(top - margin / 2, left + margin / 2, artboardSize, artboardSize);
        rect.stroked = false; // No stroke
        rect.filled = true; // Set to false if no fill is desired
        rect.fillColor = new NoColor(); // No color
        rectangles.push(rect);
    }
  
    return {document: doc, rectangles: rectangles};
}

// Function to place Illustrator files into the artboards
function placeIllustratorFiles(folderPath, doc, rectangles) {
    var filesFolder = new Folder(folderPath);
    var illustratorFiles = filesFolder.getFiles(function(f) { return f instanceof File && /.ai$/i.test(f.name); });

    for (var i = 0; i < illustratorFiles.length && i < doc.artboards.length; i++) {
        var placedItem = doc.placedItems.add();
        placedItem.file = illustratorFiles[i];
        
        // Center the placed item within the artboard
        var ab = doc.artboards[i].artboardRect;
        placedItem.width = 200;
        placedItem.height = 200;
        var x = ab[0] + (ab[2] - ab[0] - placedItem.width) / 2;
        var y = ab[1] - (ab[1] - ab[3] - placedItem.height) / 2;
        placedItem.position = [x, y];
        
        var rect = rectangles[i];
        placedItem.zOrder(ZOrderMethod.BRINGTOFRONT); // Place the item in front of the rectangle
        rect.zOrder(ZOrderMethod.SENDTOBACK); // Move the rectangle to back
    }

    if (illustratorFiles.length > doc.artboards.length) {
        alert(‘There are more Illustrator files than artboards.’);
    }
}

// Ask user to select the folder where the Illustrator files are located
var selectedFolder = Folder.selectDialog(‘Please select the folder containing the Illustrator files:’);
if (selectedFolder) {
    var illustratorFiles = selectedFolder.getFiles(function(f) { return f instanceof File && /.ai