Skip to main content
Inspiring
March 10, 2023
Answered

Loop over graphics with InDesign

  • March 10, 2023
  • 1 reply
  • 282 views

I can fit frame to content on all graphics with 

var g   = app.activeDocument.allGraphics;
for (var i = 0; i < g.length; i++)
{
   g[i].fit(FitOptions.FRAME_TO_CONTENT);
}

 

Yah! ...Only, I want to find out the specific graphically cropped offenders:

 

for (var i = 0; i < g.length; i++)
{

   alert(g[i].name); // also not working
   var sel = g[i]; // not working
   var ff = sel.frameFittingOptions;
   var cropped = false;
   if (ff.topCrop > 0)    cropped = true;
   if (ff.bottomCrop > 0) cropped = true
   if (ff.leftCrop > 0)   cropped = true
   if (ff.rightCrop > 0)  cropped = true;

   if (cropped == true)
   {
      alert("cropped!");
      sel.fit(FitOptions.FRAME_TO_CONTENT);
      cropped = false;
   }
}

That second snippet won't fully work as each graphic is not selected and the alert won't work as required.

Long time Photoshop scripter, first time INDD caller. Two things:

1, Do I have to select each graphic in turn or can I just create a reference to one?

2, Where am I going wrong with .name convention?

This topic has been closed for replies.
Correct answer rob day

Hi @Ghoul Fool , If you are looking for a placed image’s file name it would be the name of the itemLink, and frameFittingOptions are a property of the container:

 

var g   = app.activeDocument.allGraphics;
for (var i = 0; i < g.length; i++){
   $.writeln(g[i].itemLink.name)
   //returns the placed file’s name
   
   $.writeln(g[i].parent.frameFittingOptions.topCrop)
   //frameFittingOptions are a property of the graphic’s container—its parent
}

 

Also, you could simplify the is cropped test by comparing the image bounds with its parent’s bounds, if they are not the same fit the frame to content (this would also fit a parent frame that’s larger than the image):

 

 

var g   = app.activeDocument.allGraphics;
for (var i = 0; i < g.length; i++){
    if (g[i].geometricBounds.toString() != g[i].parent.geometricBounds.toString()) {
	    $.writeln("Image frame is not fit")
        g[i].parent.fit(FitOptions.FRAME_TO_CONTENT)
    }
}

 

1 reply

rob day
Community Expert
rob dayCommunity ExpertCorrect answer
Community Expert
March 10, 2023

Hi @Ghoul Fool , If you are looking for a placed image’s file name it would be the name of the itemLink, and frameFittingOptions are a property of the container:

 

var g   = app.activeDocument.allGraphics;
for (var i = 0; i < g.length; i++){
   $.writeln(g[i].itemLink.name)
   //returns the placed file’s name
   
   $.writeln(g[i].parent.frameFittingOptions.topCrop)
   //frameFittingOptions are a property of the graphic’s container—its parent
}

 

Also, you could simplify the is cropped test by comparing the image bounds with its parent’s bounds, if they are not the same fit the frame to content (this would also fit a parent frame that’s larger than the image):

 

 

var g   = app.activeDocument.allGraphics;
for (var i = 0; i < g.length; i++){
    if (g[i].geometricBounds.toString() != g[i].parent.geometricBounds.toString()) {
	    $.writeln("Image frame is not fit")
        g[i].parent.fit(FitOptions.FRAME_TO_CONTENT)
    }
}