// Replace bottom objects with top object
// Modification by Sergey Osokin (https://github.com/creold)
// Added 9 reference points
main();
function main() {
var refPointArr = new Array(9);
var refPointDef = [false, false, false, false, true, false, false, false, false];
var refHints = ["Top Left", "Top", "Top Right", "Left", "Center", "Right", "Bottom Left", "Bottom", "Bottom Right"];
if (app.documents.length === 0) return;
mySelection = activeDocument.selection;
if (mySelection.length < 2 || mySelection.typename === "TextRange") return;
// DIALOG
var win = new Window("dialog", "Zamena");
win.orientation = "column";
win.opacity = .97;
var wrapper = win.add("group");
wrapper.orientation = "row";
wrapper.alignChildren = ["left", "top"];
wrapper.spacing = 25;
var refPnl = wrapper.add("panel", undefined, "Reference Point");
refPnl.orientation = "row";
refPnl.bounds = [0, 0, 116, 116];
// Create reference point matrix
var idx = 0;
for (var i = 0; i < 3; i++) {
for (var j = 0; j < 3; j++) {
refPointArr[idx] = addRadio(refPnl, j, i, refPointDef[idx], refHints[idx]);
idx++;
}
}
var cancel = win.add("button", undefined, "Cancel", {name: "cancel"});
var ok = win.add("button", undefined, "OK", {name: "ok"});
ok.onClick = okClick;
function okClick() {
var refPoint = getReferencePoint(refHints, refPointArr);
var goal = mySelection[0];
for (var i = 1; i < mySelection.length; i++) {
var currItem = mySelection[i];
newItem = goal.duplicate();
changePosition(newItem, currItem, refPoint);
newItem.artworkKnockout = currItem.artworkKnockout;
newItem.clipping = currItem.clipping;
newItem.isIsolated = currItem.isIsolated;
newItem.evenodd = currItem.evenodd;
if (currItem.polarity) {
newItem.polarity = currItem.polarity;
}
newItem.moveBefore(currItem);
currItem.remove()
}
win.close();
}
cancel.onClick = win.close;
function getReferencePoint(hints, arr) {
var str = hints[4];
for (var i = 0; i < arr.length; i++) {
if (arr[i].value) {
str = hints[i];
break;
}
}
return str.replace(/\s+/g, "").toUpperCase();
}
win.show();
}
function addRadio(place, x, y, val, info) {
var rb = place.add("radiobutton", undefined, x);
var step = 30;
var x0 = y0 = 20;
var d = 14; // Padding
x = x0 + step * x;
y = y0 + step * y;
rb.bounds = [x, y, x + d, y + d];
rb.value = val;
rb.helpTip = info;
return rb;
}
function changePosition(objA, objB, ref) {
var deltaX = objB.geometricBounds[0] - objA.geometricBounds[0];
var deltaY = objB.geometricBounds[1] - objA.geometricBounds[1];
var deltaWidth = objB.width - objA.width;
var deltaHeight = objA.height - objB.height;
var offsetX = deltaWidth * 0.5;
var offsetY = (objB.height - objA.height) * 0.5;
var translateX = 0;
var translateY = 0;
switch (ref.toUpperCase()) {
case "TOPLEFT":
translateX = deltaX;
translateY = deltaY;
break;
case "TOP":
translateX = deltaX + offsetX;
translateY = deltaY;
break;
case "TOPRIGHT":
translateX = deltaWidth + deltaX;
translateY = deltaY;
break;
case "LEFT":
translateX = deltaX;
translateY = deltaY - offsetY;
break;
case "CENTER":
translateX = deltaX + offsetX;
translateY = deltaY - offsetY;
break;
case "RIGHT":
translateX = deltaWidth + deltaX;
translateY = deltaY - offsetY;
break;
case "BOTTOMLEFT":
translateX = deltaX;
translateY = deltaHeight + deltaY;
break;
case "BOTTOM":
translateX = deltaX + offsetX;
translateY = deltaHeight + deltaY;
break;
case "BOTTOMRIGHT":
translateX = deltaWidth + deltaX;
translateY = deltaHeight + deltaY;
break;
default:
break;
}
objA.translate(translateX, translateY);
}