Skip to main content
Known Participant
March 17, 2025
Question

Indesign script - frameFittingOptions crop in combination fittingOnEmptyFrame fittingAlignment

  • March 17, 2025
  • 1 reply
  • 155 views

Hi,

 

I am trying to run a script that resets image fitting. I am having issues with the cropping. I don't understand the hirarchy of the geormetricbounds vs cropping vs fitting of an image in a rectangle. 

 

I want to be able to swap image within a rectangle in a clean predicatble way witout crop values of the original image in the rectangle. I want to reset the crop to be 0 on all 4 sides like i can in the interface. set the image rectangle to be fill proportionally, autofit and be able to set an align from. When I run my script I get back and image with a crop factor on it instead of crop being [0,0,0,0]

 

Here is a code snippet from what I have so far: 

targetObject.frameFittingOptions.autoFit = frameFittingOptions.autoFit;
// fitting
targetObject.frameFittingOptions.fittingOnEmptyFrame = convertFromString(frameFittingOptionsFittingDefinition, frameFittingOptions.fitting);
// alignment
targetObject.frameFittingOptions.fittingAlignment = convertFromString(frameFittingOptionsAlignmentDefinition, frameFittingOptions.fittingAlignment);
// crop
targetObject.frameFittingOptions.topCrop = frameFittingOptions.crop[0];
targetObject.frameFittingOptions.leftCrop = frameFittingOptions.crop[1];
targetObject.frameFittingOptions.bottomCrop = frameFittingOptions.crop[2];
targetObject.frameFittingOptions.rightCrop = frameFittingOptions.crop[3];
 
Thanks for nay help or insights?
 
Nick

1 reply

Community Expert
March 20, 2025

Does this get you closer to what you need? 

 

function main() {
    if (app.selection.length === 0) {
        alert("Please select a valid image frame.");
        return;
    }
    
    var targetObject = app.selection[0];
    
    // Check if the target object supports frame fitting options and contains graphics.
    if (typeof targetObject.frameFittingOptions === "undefined" || !targetObject.graphics || targetObject.graphics.length === 0) {
        alert("Selected object is not a valid image container or does not contain an image!");
        return;
    }
    
    var graphic = targetObject.graphics[0];
    
    // Reset any transformations (scaling, rotation)
    graphic.absoluteHorizontalScale = 100;
    graphic.absoluteVerticalScale = 100;
    graphic.rotationAngle = 0;
    
    // Clear any previously set frame fitting options to remove cropping and adjustments.
    targetObject.clearFrameFittingOptions();
    
    // Reset the graphic's bounds to match the frame's bounds.
    // This effectively removes any residual cropping.
    graphic.geometricBounds = targetObject.geometricBounds;
    
    // Temporarily disable autoFit to prevent interference.
    targetObject.frameFittingOptions.autoFit = false;
    
    // Apply the desired fitting method. Here we use Fill Proportionally.
    targetObject.fit(FitOptions.FILL_PROPORTIONALLY);
    
    // Optionally, re-enable autoFit if that's part of your workflow.
    targetObject.frameFittingOptions.autoFit = true;
    
    alert("Image fitting reset successfully on a " + targetObject.constructor.name + ".");
}

main();