var doc = app.activeDocument;
var selectedItems = doc.selection;
var selectedCount = selectedItems.length;
if (selectedCount === 0) {
alert('⚠️ No layers are selected!');
} else if (selectedCount < 2) {
alert('⚠️ You need at least 2 color-filled layers selected!');
} else {
var dialog = new Window("dialog", "Select Shapes");
dialog.orientation = "column";
dialog.alignChildren = "left";
var fgGroup = dialog.add("group");
fgGroup.add("statictext", undefined, "Foreground Shape:");
var fgDropdown = fgGroup.add("dropdownlist", undefined, ["Square", "Circle"]);
fgDropdown.selection = 1;
var bgGroup = dialog.add("group");
bgGroup.add("statictext", undefined, "Background Shape:");
var bgDropdown = bgGroup.add("dropdownlist", undefined, ["Square", "Circle"]);
bgDropdown.selection = 0;
var buttonGroup = dialog.add("group");
var cancelButton = buttonGroup.add("button", undefined, "Cancel");
var okButton = buttonGroup.add("button", undefined, "OK");
okButton.onClick = function () {
var fgShape = fgDropdown.selection.text.toLowerCase();
var bgShape = bgDropdown.selection.text.toLowerCase();
var backgroundSize = selectedItems[0].width;
var foregroundSize = backgroundSize / 2;
function createForeground(y, x, size, fillColor) {
if (fgShape === "square") {
var square = doc.pathItems.rectangle(y - size / 2, x - size / 2, size, size);
square.fillColor = fillColor;
square.position = [x, y];
square.translate(-size / 2, -size / 2);
} else if (fgShape === "circle") {
var circle = doc.pathItems.ellipse(y, x, size, size);
circle.fillColor = fillColor;
circle.position = [x, y];
circle.translate(-size / 2, -size / 2);
}
}
function createBackground(y, x, size, fillColor) {
if (bgShape === "square") {
var square = doc.pathItems.rectangle(y - size / 2, x - size / 2, size, size);
square.fillColor = fillColor;
square.position = [x, y + size / 2];
square.translate(-size / 2, -size / 2);
} else if (bgShape === "circle") {
var circle = doc.pathItems.ellipse(y, x, size, size);
circle.fillColor = fillColor;
circle.position = [x, y + size / 2];
circle.translate(-size / 2, -size / 2);
}
}
var swatchSpacing = selectedItems[1].left - selectedItems[0].left - backgroundSize;
var artbX = selectedItems[0].left + selectedItems[0].width - backgroundSize / 2;
var artbY = selectedItems[0].top + (selectedItems[0].height + swatchSpacing) * 2;
for (var i = 0; i < selectedCount; i++) {
var swatchY = artbY + (i * (backgroundSize + swatchSpacing));
var colorRow = selectedItems[selectedCount - i - 1].fillColor;
for (var j = 0; j < selectedCount; j++) {
var swatchX = artbX + (j * (backgroundSize + swatchSpacing));
var colorColumn = selectedItems[j].fillColor;
createBackground(swatchY, swatchX, backgroundSize, colorRow);
createForeground(swatchY, swatchX, foregroundSize, colorColumn);
}
}
dialog.close();
alert('Shapes created successfully!');
};
cancelButton.onClick = function () {
dialog.close();
};
dialog.show();
}