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

Delete empty picture boxes with specific Object Style applied

Explorer ,
Jan 30, 2024 Jan 30, 2024

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 🙂

TOPICS
Scripting , UXP Scripting
550
Translate
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 2 Correct answers

Community Expert , Jan 30, 2024 Jan 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(); }
}
Translate
LEGEND , Jan 30, 2024 Jan 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();
				}
			};
		}
	}
}

 

 

 

 

 

Translate
Community Expert ,
Jan 30, 2024 Jan 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(); }
}
Translate
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
Explorer ,
Jan 31, 2024 Jan 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.

Translate
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
Explorer ,
Feb 02, 2024 Feb 02, 2024

Thanks @brian_p_dts - this worked like a charm...DeleteIfEmptyTwo-Before.pngexpand imageDeleteIfEmptyTwo-After.pngexpand image

Translate
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
LEGEND ,
Jan 30, 2024 Jan 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();
				}
			};
		}
	}
}

 

 

 

 

 

Translate
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
Community Expert ,
Jan 30, 2024 Jan 30, 2024

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

Translate
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
LEGEND ,
Jan 30, 2024 Jan 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?

 

Translate
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
Explorer ,
Jan 31, 2024 Jan 31, 2024

Thanks so much. I can't test it just yet, as my systems administrator will need to add that file. I will let you know if it works.

Translate
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
Explorer ,
Feb 02, 2024 Feb 02, 2024
LATEST

@Robert at ID-TaskerYours works perfectly too, thank you 🙂DeleteIfEmpty-Before.pngexpand imageDeleteIfEmpty-After.pngexpand image

Translate
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