• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
7

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

New Here ,
Dec 08, 2023 Dec 08, 2023

Copy link to clipboard

Copied

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!

TOPICS
Scripting

Views

144

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
community guidelines

correct answers 1 Correct answer

Community Expert , Dec 08, 2023 Dec 08, 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 unnecessar

...

Votes

Translate

Translate
Community Expert ,
Dec 08, 2023 Dec 08, 2023

Copy link to clipboard

Copied

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)

 

 

Votes

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
community guidelines
New Here ,
Dec 08, 2023 Dec 08, 2023

Copy link to clipboard

Copied

LATEST

My next maltipoo will be named Bob.

 

Thank you!

Votes

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
community guidelines