Inspiring
September 20, 2024
Answered
• Small javascript that gives back the scale and the rotate of a selected "placed img"
- September 20, 2024
- 3 replies
- 1881 views
Hello -
I made a small script (w/ the help of ChatGPT) that gives back the current scale and the rotate of a placed and selected image…
It works prefectly as a "standalone" script.
But when I try to embed in a palette and call it via a button it doesn't work".
Could someone helps me on this?
Here is the code:
#target Illustrator
#targetengine main
var myPalette_XPOS = 2150;
var myPalette_YPOS = 30;
// PALETTE
// =======
var myPalette = new Window('palette', 'DC TOOLS'); // , {resizeable: true}
// myPalette.text = "DC TOOLS";
myPalette.orientation = "column";
myPalette.alignment = "center"; // Center the text horizontally
myPalette.alignChildren = ["center","top"];
myPalette.spacing = 1;
myPalette.margins = 2;
var button10 = myPalette.add("button", undefined, undefined, {name: "button10"});
button10.text = "IMG Scale";
button10.alignment = ["center","top"];
myPalette.frameLocation = [ myPalette_XPOS, myPalette_YPOS ];
myPalette.show();
// Function to show a dialog with SCALE & ROTATION
function showScaleAndRotation() {
// Ensure there is an active document
if (app.documents.length === 0) {
alert("No document open.");
return;
}
var IMG_selected = app.activeDocument.selection;
// Check if something is selected
if (IMG_selected.length === 0) {
alert("you called me, is an image seleced?");
alert("Please select an object.");
return;
}
var selectedItem = IMG_selected[0];
// Check if it's a linked image (PlacedItem) or a raster image (RasterItem)
if (selectedItem.typename !== "PlacedItem" && selectedItem.typename !== "RasterItem") {
alert("The selected object is not a linked or raster image.");
return;
}
// Get the transformation matrix of the selected object
var matrix = selectedItem.matrix;
// alert("you called me? So it s an IMG and its selected");
// Calculate the scaling based on the transformation matrix
var scaleX = Math.sqrt(matrix.mValueA * matrix.mValueA + matrix.mValueB * matrix.mValueB) * 100;
var scaleY = Math.sqrt(matrix.mValueC * matrix.mValueC + matrix.mValueD * matrix.mValueD) * 100;
// Calculate the rotation in degrees
var rotation = Math.atan2(matrix.mValueB, matrix.mValueA) * (180 / Math.PI);
// Show the result in a dialog box
alert("Scale X: " + scaleX.toFixed(2) + "%\n" + "Scale Y: " + scaleY.toFixed(2) + "%\n" + "Rotation: " + rotation.toFixed(2) + "°\n");
}
