Answered
m1bCorrect answer
Community Expert
Jul 23, 2023 11:05 PM UTC
Hi @M.Hasanin, thank you for doing this, very well done. Your scripting is going very well. As it happened (one minute later than you!) I was about to post a version of my same script that handled scaling also. I will post it here so you may see if I did anything differently.
- Mark
/**
* Apply scale and rotation to graphics according to their script label.
* eg. if label is "scale: 110, rotate: 15" then the graphic will be
* scaled to 110% and rotated to 15° counter-clockwise.
* @author m1b
* @discussion https://community.adobe.com/t5/indesign-discussions/script-to-rotate-selected-image-inside-frame-data-merge/m-p/13955151
*/
function main() {
var doc = app.activeDocument,
images = doc.allGraphics,
rotationMatcher = /\brotate:\s*(-?\d+\.?\d*)/i,
scaleMatcher = /\bscale:\s*(\d+\.?\d*)/i,
label,
scale,
rotation;
for (var i = 0; i < images.length; i++) {
label = images[i].label || images[i].parent.label;
if (!label)
continue;
rotation = label.match(rotationMatcher);
scale = label.match(scaleMatcher);
if (
!rotation
|| rotation.length < 2
|| isNaN(rotation = Number(rotation[1]))
)
rotation = undefined;
else
rotation -= images[i].absoluteRotationAngle;
if (
!scale
|| scale.length < 2
|| isNaN(scale = Number(scale[1]))
)
scale = undefined;
else
scale = scale / images[i].absoluteHorizontalScale;
if (!rotation && !scale)
continue;
var tm = app.transformationMatrices.add({
counterclockwiseRotationAngle: rotation,
horizontalScaleFactor: scale,
verticalScaleFactor: scale,
});
images[i].transform(
CoordinateSpaces.pasteboardCoordinates,
AnchorPoint.centerAnchor,
tm,
);
}
};
app.doScript(main, ScriptLanguage.JAVASCRIPT, undefined, UndoModes.ENTIRE_SCRIPT, 'Transform Graphics');