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

Delete empty picture boxes with specific Object Style applied

Explorer ,
Jan 30, 2024 Jan 30, 2024

Copy link to clipboard

Copied

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

Views

403

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 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(); }
}

Votes

Translate

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

 

 

 

 

 

Votes

Translate

Translate
Community Expert ,
Jan 30, 2024 Jan 30, 2024

Copy link to clipboard

Copied

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(); }
}

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

Copy link to clipboard

Copied

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.

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

Copy link to clipboard

Copied

Thanks @brian_p_dts - this worked like a charm...DeleteIfEmptyTwo-Before.pngDeleteIfEmptyTwo-After.png

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

Copy link to clipboard

Copied

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();
				}
			};
		}
	}
}

 

 

 

 

 

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

Copy link to clipboard

Copied

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

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

Copy link to clipboard

Copied

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?

 

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

Copy link to clipboard

Copied

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.

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

Copy link to clipboard

Copied

LATEST

@Robert at ID-TaskerYours works perfectly too, thank you 🙂DeleteIfEmpty-Before.pngDeleteIfEmpty-After.png

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