Fit graphic frame to content doesn't work

Explorer ,
Jan 13, 2021

Copy link to clipboard

Copied

I have created a simple function to walk through my document, find all Rectangle objects and force the frame to fit to the content. We are importing XML documents, and many of the graphic frames come in cropped. When I manually select Fitting > Fit Frame to Content in the UI, it does exactly what I want it to do. However, when I apply a script with this function, nothing changes.

function fitToFrame(){
    var thisDoc=app.activeDocument;
    app.findObjectPreferences = null;
    var objList = thisDoc.findObject();
    for (var i=0; i<objList.length; i++){
        if (objList[i] instanceof Rectangle){
            objList[i].fit(FitOptions.FRAME_TO_CONTENT);
        }
    }
 }

I know that it's doing something because after running it, the Undo menu in the UI changes to "Undo Set Frame Fitting Options," but none of the frames actually change.

Any insight on why this doesn't change anything or what I might need to do to make it work correctly?

Hello tcarano65,

Give the below code a try......it works on you sample file with images placed in cells.

 

var doc = app.documents[0];
var myImage = doc.allGraphics;

for (var i=0; myImage.length>i; i++){
 myImage[i].fit(FitOptions.FRAME_TO_CONTENT)
}

 

Regards,

Mike

TOPICS
Scripting

Views

150

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more

Fit graphic frame to content doesn't work

Explorer ,
Jan 13, 2021

Copy link to clipboard

Copied

I have created a simple function to walk through my document, find all Rectangle objects and force the frame to fit to the content. We are importing XML documents, and many of the graphic frames come in cropped. When I manually select Fitting > Fit Frame to Content in the UI, it does exactly what I want it to do. However, when I apply a script with this function, nothing changes.

function fitToFrame(){
    var thisDoc=app.activeDocument;
    app.findObjectPreferences = null;
    var objList = thisDoc.findObject();
    for (var i=0; i<objList.length; i++){
        if (objList[i] instanceof Rectangle){
            objList[i].fit(FitOptions.FRAME_TO_CONTENT);
        }
    }
 }

I know that it's doing something because after running it, the Undo menu in the UI changes to "Undo Set Frame Fitting Options," but none of the frames actually change.

Any insight on why this doesn't change anything or what I might need to do to make it work correctly?

Hello tcarano65,

Give the below code a try......it works on you sample file with images placed in cells.

 

var doc = app.documents[0];
var myImage = doc.allGraphics;

for (var i=0; myImage.length>i; i++){
 myImage[i].fit(FitOptions.FRAME_TO_CONTENT)
}

 

Regards,

Mike

TOPICS
Scripting

Views

151

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Jan 13, 2021 0
Enthusiast ,
Jan 13, 2021

Copy link to clipboard

Copied

Hello tcarano65,

Give the below code a try......

 

 

 

var myRectangles = app.documents[0].rectangles.everyItem().getElements();

  for (var i=0; i<myRectangles.length; i++){
   if (myRectangles[i].contentType == ContentType.GRAPHIC_TYPE){
    myRectangles[i].fit(FitOptions.FRAME_TO_CONTENT);
   }
 }

 

 

 

 

Regards,

Mike

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jan 13, 2021 0
Explorer ,
Jan 14, 2021

Copy link to clipboard

Copied

Thank you for your suggestion, @mikeb41294032. Unfortunately, this didn't work, even with @crazyPanda's suggestion to delete app.findObjectPreferences=null and to use 2 equal signs in the if statement. In this case, it is ignoring the loop so it appears that it doesn't find a collection of graphics at all.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jan 14, 2021 0
Participant ,
Jan 14, 2021

Copy link to clipboard

Copied

I haven't tested your codes but here are my assumptions why they might not work

 

@tcarano65 

When you use this command:

app.findObjectPreferences = null;

 That means that you are telling Indesign to search for nothing.

 

@mikeb41294032 

Your code might not work because you forgot to use 2 equal signs at:

if (myRectangles[i].contentType = ContentType.GRAPHIC_TYPE){...

 

Kind Regards

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jan 14, 2021 0
Explorer ,
Jan 14, 2021

Copy link to clipboard

Copied

Thank you for the feedback @crazyPanda. I thought setting the app.findObjectPreferences to null cleared any search preferences that might be cached so that I can start with a fresh search. I have it set in my other script that does a similar search, and it doesn't affect the search results. However, I deleted it so I could test it, and it did not change anything. I also tried it along with your second suggestion with @mikeb41294032's code, and it still didn't work. Thanks for your help. I welcome any additional suggestions you might have!

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jan 14, 2021 0
Enthusiast ,
Jan 14, 2021

Copy link to clipboard

Copied

Hello tcarano65,

 

My code is working for me with files i have setup, is it possable for you to post a sample .indd file for us to test with?

 

Regards,

Mike

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jan 14, 2021 0
Explorer ,
Jan 14, 2021

Copy link to clipboard

Copied

 

The information is confidential, so I had to pare it down quite a bit and put it in some fake text. This has minimal content, but I have tested the script on this with no success. Some things to note:

  • This uses imported XML content
  • I have embedded the graphics, but usually they are just linked. It did not make a difference in my testing.
  • There isn't a single text flow. There are 3 separate text frames that each flow to the 2nd page. Each of these 3 frames is linked to different XML tags. The first text frame, on the right with the table, is linked to the Graphics XML tag. The second text frame, in the middle with text, is linked to the first Language Group XML tag. And the third text frame, the empty frame on the right, is linked to the second Language Group XML tag.

Download InDesign file 

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jan 14, 2021 0
Explorer ,
Jan 14, 2021

Copy link to clipboard

Copied

Forgot to tag you on this, @mikeb41294032.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jan 14, 2021 0
Enthusiast ,
Jan 14, 2021

Copy link to clipboard

Copied

Hello tcarano65,

Give the below code a try......it works on you sample file with images placed in cells.

 

var doc = app.documents[0];
var myImage = doc.allGraphics;

for (var i=0; myImage.length>i; i++){
 myImage[i].fit(FitOptions.FRAME_TO_CONTENT)
}

 

Regards,

Mike

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jan 14, 2021 0
Explorer ,
Jan 19, 2021

Copy link to clipboard

Copied

THANK YOU! Thank you so much, @mikeb41294032, for helping me resolve this issue. This absolutely worked, and I'm very grateful for your help and for taking the time to look into this for me!

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jan 19, 2021 0