#target illustrator // Rotate About Point v2 // - Custom centre of rotation (X / Y) with live preview // - Units: pt, px, mm, cm, in, pica (defaults to your ruler units) // - Coordinates: Artboard (origin top-left of active artboard, Y down - matches // the Transform panel) or Document (origin bottom-left, Y up) (function () { if (app.documents.length === 0) { alert("Open a document first."); return; } var doc = app.activeDocument; if (!doc.selection || doc.selection.length === 0) { alert("Select one or more objects first."); return; } // Work in document coordinates internally; restore the user's setting after. var prevCS = app.coordinateSystem; app.coordinateSystem = CoordinateSystem.DOCUMENTCOORDINATESYSTEM; try { run(); } finally { app.coordinateSystem = prevCS; } function run() { // ---------- Setup ---------- var sel = doc.selection; var ab = doc.artboards[doc.artboards.getActiveArtboardIndex()].artboardRect; // [l,t,r,b] var L = Infinity, T = -Infinity, R = -Infinity, B = Infinity; for (var i = 0; i < sel.length; i++) { var b = sel[i].geometricBounds; if (b[0] < L) L = b[0]; if (b[1] > T) T = b[1]; if (b[2] > R) R = b[2]; if (b[3] < B) B = b[3]; } var defaultCentre = { x: (L + R) / 2, y: (T + B) / 2 }; var artboardCentre = { x: (ab[0] + ab[2]) / 2, y: (ab[1] + ab[3]) / 2 }; var cur = { x: defaultCentre.x, y: defaultCentre.y }; // truth: points, document coords var UNITS = [ { n: "pt", f: 1, s: 1 }, { n: "px", f: 1, s: 1 }, { n: "mm", f: 72 / 25.4, s: 1 }, { n: "cm", f: 72 / 2.54, s: 0.1 }, { n: "in", f: 72, s: 0.01 }, { n: "pica", f: 12, s: 1 } ]; var unitNames = []; for (var u = 0; u < UNITS.length; u++) unitNames.push(UNITS[u].n); // Default unit from the ruler preference (0 in, 1 mm, 2 pt, 3 pica, 4 cm, 6 px) var unitIdx = 0; try { var map = { 0: "in", 1: "mm", 2: "pt", 3: "pica", 4: "cm", 6: "px" }; var name = map[app.preferences.getIntegerPreference("rulerType")]; for (var k = 0; k < UNITS.length; k++) if (UNITS[k].n === name) unitIdx = k; } catch (e) {} // ---------- Conversion ---------- function artboardMode() { return csDrop.selection.index === 0; } function fromDoc(p) { // document points -> displayed units var x = p.x, y = p.y; if (artboardMode()) { x = p.x - ab[0]; y = ab[1] - p.y; } var f = UNITS[unitDrop.selection.index].f; return { x: x / f, y: y / f }; } function toDoc(vx, vy) { // displayed units -> document points var f = UNITS[unitDrop.selection.index].f; var x = vx * f, y = vy * f; if (artboardMode()) { x = ab[0] + x; y = ab[1] - y; } return { x: x, y: y }; } function fmt(n) { return String(Math.round(n * 1000) / 1000); } function num(s) { return parseFloat(String(s).replace(",", ".")); } // ---------- Dialog ---------- var w = new Window("dialog", "Rotate"); w.alignChildren = "fill"; var pAngle = w.add("panel", undefined, "Rotate"); pAngle.orientation = "row"; pAngle.add("statictext", undefined, "Angle:"); var angleIn = pAngle.add("edittext", undefined, "0"); angleIn.characters = 8; pAngle.add("statictext", undefined, "\u00B0"); var pC = w.add("panel", undefined, "Centre of rotation"); pC.alignChildren = "fill"; var gOpt = pC.add("group"); gOpt.add("statictext", undefined, "Coordinates:"); var csDrop = gOpt.add("dropdownlist", undefined, ["Artboard (Y down)", "Document (Y up)"]); csDrop.selection = 0; gOpt.add("statictext", undefined, "Units:"); var unitDrop = gOpt.add("dropdownlist", undefined, unitNames); unitDrop.selection = unitIdx; var gXY = pC.add("group"); gXY.add("statictext", undefined, "X:"); var xIn = gXY.add("edittext", undefined, "0"); xIn.characters = 9; gXY.add("statictext", undefined, "Y:"); var yIn = gXY.add("edittext", undefined, "0"); yIn.characters = 9; var gBtn = pC.add("group"); var resetBtn = gBtn.add("button", undefined, "Selection centre"); var artboardBtn = gBtn.add("button", undefined, "Artboard centre"); var copyChk = w.add("checkbox", undefined, "Copy"); var prevChk = w.add("checkbox", undefined, "Preview"); prevChk.value = true; var btns = w.add("group"); btns.alignment = "right"; var okBtn = btns.add("button", undefined, "OK"); var cancelBtn = btns.add("button", undefined, "Cancel"); function writeFields() { var v = fromDoc(cur); xIn.text = fmt(v.x); yIn.text = fmt(v.y); } writeFields(); // ---------- Rotation + preview ---------- function buildMatrix(angle) { var m = app.getTranslationMatrix(-cur.x, -cur.y); m = app.concatenateRotationMatrix(m, angle); return app.concatenateTranslationMatrix(m, cur.x, cur.y); } function applyRotation(angle, copy) { var s = doc.selection, items = []; for (var i = 0; i < s.length; i++) items.push(s[i]); var m = buildMatrix(angle); for (var j = 0; j < items.length; j++) { var t = copy ? items[j].duplicate() : items[j]; t.transform(m, true, true, true, true, true, Transformation.DOCUMENTORIGIN); } } var previewApplied = false; function clearPreview() { if (previewApplied) { app.undo(); previewApplied = false; } } function updatePreview() { clearPreview(); if (prevChk.value) { var a = num(angleIn.text); if (!isNaN(a) && (a !== 0 || copyChk.value)) { applyRotation(a, copyChk.value); previewApplied = true; } } app.redraw(); } // ---------- Events ---------- angleIn.onChanging = updatePreview; function onXY() { var vx = num(xIn.text), vy = num(yIn.text); if (!isNaN(vx) && !isNaN(vy)) cur = toDoc(vx, vy); updatePreview(); } xIn.onChanging = onXY; yIn.onChanging = onXY; csDrop.onChange = writeFields; // same point, shown in new system/units unitDrop.onChange = writeFields; resetBtn.onClick = function () { cur = { x: defaultCentre.x, y: defaultCentre.y }; writeFields(); updatePreview(); }; artboardBtn.onClick = function () { cur = { x: artboardCentre.x, y: artboardCentre.y }; writeFields(); updatePreview(); }; // Up/Down arrows nudge the value. Shift = x10, Alt/Option = x0.1. function bindArrows(edit, getStep, onChange) { edit.addEventListener("keydown", function (k) { if (k.keyName !== "Up" && k.keyName !== "Down") return; var v = num(edit.text); if (isNaN(v)) return; var d = getStep() * (k.shiftKey ? 10 : (k.altKey ? 0.1 : 1)); edit.text = fmt(v + (k.keyName === "Up" ? d : -d)); k.preventDefault(); onChange(); }); } function unitStep() { return UNITS[unitDrop.selection.index].s; } bindArrows(xIn, unitStep, onXY); bindArrows(yIn, unitStep, onXY); bindArrows(angleIn, function () { return 1; }, updatePreview); copyChk.onClick = updatePreview; prevChk.onClick = updatePreview; okBtn.onClick = function () { var a = num(angleIn.text); if (isNaN(a) || isNaN(num(xIn.text)) || isNaN(num(yIn.text))) { alert("Angle, X and Y must be numbers."); return; } w.close(1); }; cancelBtn.onClick = function () { w.close(0); }; updatePreview(); angleIn.active = true; var result = w.show(); if (result !== 1) { // Cancel / closed: remove any preview clearPreview(); app.redraw(); return; } if (!previewApplied) { // preview off (or never applied): apply now var angle = num(angleIn.text); if (angle !== 0 || copyChk.value) applyRotation(angle, copyChk.value); } app.redraw(); // Remember the settings so RotateAgain.jsx can repeat them try { var f = new File(Folder.userData + "/RotateAboutPoint_last.txt"); f.open("w"); f.write([num(angleIn.text), cur.x, cur.y, copyChk.value ? 1 : 0].join("|")); f.close(); } catch (e) {} } })();