Skip to main content
Inspiring
Jul 7, 2023 4:25 PM UTC
Answered

Resize Anchored Frames with Script

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 is closed to new replies. Start a new post to keep the conversation going.
frameexpert
frameexpertCorrect answer
Community Expert
Jul 7, 2023 4:40 PM UTC

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;
    }
}
www.frameexpert.com

3 replies

frameexpert
Community Expert
frameexpertCommunity ExpertCorrect answer
Community Expert
Jul 7, 2023 4:40 PM UTC

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;
    }
}
Translate
www.frameexpert.com
Inspiring
Jul 7, 2023 4:42 PM UTC

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

Translate
frameexpert
Community Expert
Community Expert
Jul 7, 2023 4:33 PM UTC

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

        AFrame.Height = graphic.Height + (2 * 65535);
Translate
www.frameexpert.com