Skip to main content
Inspiring
January 30, 2024
Answered

Delete empty picture boxes with specific Object Style applied

  • January 30, 2024
  • 2 replies
  • 959 views

Hi,

I am looking for an Indesign .jsx script that...


1) Finds all picture boxes in the active document with a certain Object Style applied to them.
2) Then finds all of the above that are empty (ie have no image inside), then deletes them.


Something like this (my apologies for the rubbish attempt at Javascript, I don't know it!)...

// create a variable for finding all objects with the Object Style called "Delete If Empty"

var MyObjectStyle = app.findObjectPreferences.appliedObjectStyles = "Delete If Empty";

// write an if statement so if the frames are empty, delete them

if (MyObjectStyle.GraphicFrame.length < 0;) {

MyObjectStyle.GraphicFrame.delete;

}

Any help would be most appreciated,

Thank you 🙂

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

Another way - probably not as effective / speedy as @brian_p_dts's solution:

 

 

main();
function main(){
var myPageItems = app.documents[0].allPageItems;

for(var n = myPageItems.length-1 ; n>=0 ; n--)
	{
		if(myPageItems[n].hasOwnProperty("graphics"))
		{
			if(!myPageItems[n].graphics.length)
			{
				if(myPageItems[n].appliedObjectStyle.name == "Delete If Empty")
				{
					myPageItems[n].remove();
				}
			};
		}
	}
}

 

 

 

 

 

2 replies

Robert at ID-Tasker
Robert at ID-TaskerCorrect answer
Legend
January 30, 2024

Another way - probably not as effective / speedy as @brian_p_dts's solution:

 

 

main();
function main(){
var myPageItems = app.documents[0].allPageItems;

for(var n = myPageItems.length-1 ; n>=0 ; n--)
	{
		if(myPageItems[n].hasOwnProperty("graphics"))
		{
			if(!myPageItems[n].graphics.length)
			{
				if(myPageItems[n].appliedObjectStyle.name == "Delete If Empty")
				{
					myPageItems[n].remove();
				}
			};
		}
	}
}

 

 

 

 

 

brian_p_dts
Community Expert
Community Expert
January 30, 2024

You'd probably also want to do allPageItems to catch anything anchored or grouped.

Robert at ID-Tasker
Legend
January 30, 2024
quote

You'd probably also want to do allPageItems to catch anything anchored or grouped.


By @brian_p_dts

 

Yeah, you are right, I've "ignored" it in the first, test version.

 

Fixed.

 

BUT ... it will probably fail - yours as well as you don't have try...catch either - when Group have only two items - like in one of the recent posts?

 

brian_p_dts
Community Expert
Community Expert
January 30, 2024

Think this should do it. 

app.findObjectPreferences = null;
app.findObjectPreferences.appliedObjectStyles = "Delete If Empty";
var finds = app.activeDocument.findObject();
var i = finds.length;
while(i--) {
  if (!finds[i].graphics.length) { finds[i].remove(); }
}
Inspiring
January 31, 2024

Thanks so much for helping. I just need to get my systems administrator to add that file. I will let you know how it goes.

Inspiring
February 2, 2024

Thanks @brian_p_dts - this worked like a charm...