• Small Javascript to reset to "zero" the rotate value of a placed item.
Hi -
I try to make a very "simple" Adobe Illustrator Javascript based on the below script (I found it somewhere years ago on internet, tbh can't remeber where).
The script should simply reset to zero (0°) the "rotate" value of the current selected placed item.
I tried already a lot of stuff, but can't make it works :-P:
I found information like "app.getRotationMatrix(…)". But yeah… How to make it work? 🤷🏻
Here is the source code (base), not the final script:
selection = app.activeDocument.selection;
for (i=0; i<selection.length; i++) {
if(selection[i].typename == "TextFrame" || selection[i].typename == "PlacedItem" || selection[i].typename == "RasterItem") {
get_matrix = selection[i].matrix;
if(selection[i].typename == "PlacedItem")
get_matrix = concatenateScaleMatrix(get_matrix, 100, -100);
invMatrix = invertMatrix(get_matrix);
invMatrix.mValueTX = 0;
invMatrix.mValueTY = 0;
selection[i].transform(invMatrix);
}
}
I tried something (w/ the help of ChatGPT), visually we are close to a result, but the placed item rotate value is just not reset to "zero" (0°):
function resetPlacedItemRotation() {
var myDoc_DC = app.activeDocument;
var IMG_selected = myDoc_DC.selection;
if (IMG_selected.length === 0) {
alert("Please select a placed item.");
return;
}
for (var i = 0; i < IMG_selected.length; i++) {
var item = IMG_selected[i];
if (item.typename === "PlacedItem") {
var confirmReset = confirm("Do you want to reset the rotation for the selected placed item?");
if (confirmReset) {
// Get the current transformation matrix
var currentMatrix = item.matrix;
// Calculate the current rotation angle in degrees
var currentRotation = Math.atan2(currentMatrix.mValueB, currentMatrix.mValueA) * (180 / Math.PI);
// Apply the exact opposite of the current rotation
var rotationToApply = -currentRotation; // Opposite direction
// Rotate around the center of the placed item
item.rotate(rotationToApply, true, true, true, true, Transformation.CENTER);
alert("Rotation has been reset to 0° by applying " + rotationToApply.toFixed(3) + "°.");
} else {
alert("Operation canceled.");
}
} else {
alert("Selected item is not a placed item.");
}
}
}
resetPlacedItemRotation();
Thank you,
Enjoy your day,
- Dimitri
