Skip to main content
traceyh44535949
Participant
December 8, 2023
Answered

Problems running JS to remove empty image placeholders in a text frame.

  • December 8, 2023
  • 1 reply
  • 362 views

Any advice or assistance appreciated.

 

I have a merged InDesign document with multiple instances of a text frame called "OverlayFrame." Those instances of "OverlayFrame" contain image placeholders named "Image1", "Image 2", "Image3", etc. all the way up to "Image10". They are all on Layer 2. On Layer 1, there are unnamed text frames that contain tables, no image placeholders. I also have a Master Page element that is also an unnamed text frame with a table in it, also on Layer 1. I have a script that should go through all the instances of "OverlayFrame" and remove image placeholders within them that have no image in them.

 

Here is the script:

// Function to remove empty image placeholders within instances of OverlayFrame
function removeEmptyImagePlaceholders() {
var doc = app.activeDocument;

// Array of specific names for your image placeholders
var specificImageNames = ["Image1", "Image2", "Image3", "Image4", "Image5", "Image6", "Image7", "Image8", "Image9", "Image10"];

// Loop through all text frames in the document
for (var i = 0; i < doc.textFrames.length; i++) {
var textFrame = doc.textFrames[i];

// Check if the text frame has an overlaying rectangle (change "OverlayFrame" to match your overlaying frame name)
if (textFrame.name.indexOf("OverlayFrame") !== -1 && textFrame.itemLayer.name === "Layer 2") {
// Loop through the specific image names and remove each corresponding image
for (var j = 0; j < specificImageNames.length; j++) {
var specificImage = textFrame.pageItems.itemByName(specificImageNames[j]);

// Check if the specific image exists in the overlaying frame and has no link
if (specificImage.isValid && specificImage.graphics.length > 0 && specificImage.graphics[0].itemLink.filePath === "") {
specificImage.remove();
}
}
}
}
}

// Call the function to remove empty image placeholders
removeEmptyImagePlaceholders();

The problem is that when I run the script in InDesign, nothing happens.  I am running InDesign v.19.0.1 x64.

I don't know if there is or how to access a JS console in InDesign to see where the problem is when I run the script.  Is there something in the code I'm missing?

 

Need help!  I'll name my next pet child after you, if you can help!

This topic has been closed for replies.
Correct answer Robert at ID-Tasker

I think this is your problem - but I'm not JS guy:

 

 

if (specificImage.isValid && specificImage.graphics.length > 0 && specificImage.graphics[0].itemLink.filePath === "")

 

 

This checks:

1) "specificImage.isValid" - so the image with the label has been found,

AND

2) "specificImage.graphics.length > 0" - should rather be "== 0" - you want to delete WHEN IT IS EMPTY - so can't have any graphic objects "inside",

AND

3) "specificImage.graphics[0].itemLink.filePath === "" " - completely unnecessary.

 

So I think it should be like this:

 

if (specificImage.isValid && specificImage.graphics.length == 0)

 

 

1 reply

Robert at ID-Tasker
Robert at ID-TaskerCorrect answer
Legend
December 8, 2023

I think this is your problem - but I'm not JS guy:

 

 

if (specificImage.isValid && specificImage.graphics.length > 0 && specificImage.graphics[0].itemLink.filePath === "")

 

 

This checks:

1) "specificImage.isValid" - so the image with the label has been found,

AND

2) "specificImage.graphics.length > 0" - should rather be "== 0" - you want to delete WHEN IT IS EMPTY - so can't have any graphic objects "inside",

AND

3) "specificImage.graphics[0].itemLink.filePath === "" " - completely unnecessary.

 

So I think it should be like this:

 

if (specificImage.isValid && specificImage.graphics.length == 0)

 

 

traceyh44535949
Participant
December 8, 2023

My next maltipoo will be named Bob.

 

Thank you!