Skip to main content
Inspiring
July 7, 2023
Answered

Resize Anchored Frames with Script

  • July 7, 2023
  • 2 replies
  • 257 views

Hi, I have a number of anchored frames throughout a book that are oddly sized. I would like to create an automatic script that resizes them to the graphic, but I'm having some trouble. After piecing together other posts I am currently working with:

 

var doc = app.ActiveDoc;

var graphic = doc.FirstGraphicInDoc;

while( graphic.ObjectValid( ) )

{

     if( graphic.constructor.name == "AFrame" )

     {

        AFrame.Height = graphic.Height + (2 * 65535);

     }

     graphic = graphic.NextGraphicInDoc;

}

 But when I run the script nothing happens. I've checked the console and there are no errors, so I assume I have something incorrect here. The FM scripting guides I've found don't seem to cover anything graphic related. Any assistance is appreciated!

This topic has been closed for replies.
Correct answer frameexpert

I haven't tested this, but this should work to resize your anchored frames to the containing graphic.

#target framemaker

main ();

function main () {
    
    var doc;
    
    doc = app.ActiveDoc;
    if (doc.ObjectValid () === 1) {
        processDoc (doc);
    }
}

function processDoc (doc) {

    var aframe, graphic;
    
    // Loop through the graphics in the document,
    // looking for anchored frames.
    aframe = doc.FirstGraphicInDoc;
    while (aframe.ObjectValid () === 1) {
        if (aframe.constructor.name === "AFrame") {
            // Assume that there is a single graphic in 
            // the anchored frame.
            graphic = aframe.FirstGraphicInFrame;
            if (graphic.ObjectValid () === 1) {
                // Resize the anchored frame to the graphic.
                aframe.Height = graphic.Height;
                aframe.Width = graphic.Width;
                // Position the graphic at the upper-left of
                // the anchored frame.
                graphic.LocY = 0;
                graphic.LocX = 0;
            }
        }
        aframe = aframe.NextGraphicInDoc;
    }
}

2 replies

frameexpert
Community Expert
frameexpertCommunity ExpertCorrect answer
Community Expert
July 7, 2023

I haven't tested this, but this should work to resize your anchored frames to the containing graphic.

#target framemaker

main ();

function main () {
    
    var doc;
    
    doc = app.ActiveDoc;
    if (doc.ObjectValid () === 1) {
        processDoc (doc);
    }
}

function processDoc (doc) {

    var aframe, graphic;
    
    // Loop through the graphics in the document,
    // looking for anchored frames.
    aframe = doc.FirstGraphicInDoc;
    while (aframe.ObjectValid () === 1) {
        if (aframe.constructor.name === "AFrame") {
            // Assume that there is a single graphic in 
            // the anchored frame.
            graphic = aframe.FirstGraphicInFrame;
            if (graphic.ObjectValid () === 1) {
                // Resize the anchored frame to the graphic.
                aframe.Height = graphic.Height;
                aframe.Width = graphic.Width;
                // Position the graphic at the upper-left of
                // the anchored frame.
                graphic.LocY = 0;
                graphic.LocX = 0;
            }
        }
        aframe = aframe.NextGraphicInDoc;
    }
}
Inspiring
July 7, 2023

Wow that worked great, thank you so much for the quick reply!

frameexpert
Community Expert
Community Expert
July 7, 2023

This line doesn't refer to your anchored frame object:

        AFrame.Height = graphic.Height + (2 * 65535);