Skip to main content
Known Participant
August 26, 2009
Answered

find image and then delete...

  • August 26, 2009
  • 3 replies
  • 1569 views

Hi everyone,

I want to search particular images in the document with it's name and i want to delete those images from the document. How it can be done through script. Your help will be much appreciated.

Regards

Thiyagu

This topic has been closed for replies.
Correct answer Peter Kahrel

If you're after a certain image, e.g. sunflower.png, use this:

app.documents[0].links.item('sunflower.png').parent.parent.remove();

If you want to remove that from 500 documents, use the above one-liner in this script:

https://creativepro.com/files/kahrel/indesign/batch_convert.html

Details on how to organise that are in the web page.

Peter

 

3 replies

Community Expert
October 3, 2022

Hi @Clive5E0C ,

when this thread was moved to the new Adobe InDesign forum at the end of 2019 the code from 2009 was damaged!

Below the repaired version that I did not test:

 

var del_list = ["foo.psd", "var.jpg", "hoge.tif"];

var my_doc = app.activeDocument;
var my_image = my_doc.allGraphics;

for (var i = (my_image.length - 1); i >= 0; i--)
{

	var tmp_name = my_image[i].itemLink.name;

	for (var ii = 0; ii < del_list.length; ii++)
	{

		if (del_list[ii] === tmp_name)
		{
			 my_image[i].parent.locked = false;
			 my_image[i].remove();
		}

	}
}

 

Let's discuss this a bit, code from 2009, written perhaps with InDesign CS4 or CS5 in mind:

There is a loop through the allGraphics array of the document.

With a current version of InDesign this means that also graphics could be stored in that array that have no itemLink at all and would error out the script. Pixels that are copy/pasted from e.g. PhotoShop inside an InDesign layout.

 

So best stay with Peter Kahrel's approach with a placed and linked image or graphic that can be addressed directly with the name property of the item's link:

https://www.indesignjs.de/extendscriptAPI/indesign16/#Link.html

 

The parent of that link will return the graphic itself. See the allGraphics array of a document.

The parent of that parent is the frame that contains the graphic.

Removing that frame will remove the graphic from the document.

 

Regards,
Uwe Laubender
( Adobe Community Expert )

Peter KahrelCorrect answer
Community Expert
September 28, 2022

If you're after a certain image, e.g. sunflower.png, use this:

app.documents[0].links.item('sunflower.png').parent.parent.remove();

If you want to remove that from 500 documents, use the above one-liner in this script:

https://creativepro.com/files/kahrel/indesign/batch_convert.html

Details on how to organise that are in the web page.

Peter

 

New Participant
September 30, 2022

Awesome, thank you! I use Batch process all the time - never occurred to me. Maybe that's why I manage a Studio not an IT dept 🙂 (probably best)

 

seuzo-oJiFme
Participating Frequently
August 26, 2009

var del_list = ["foo.psd", "var.jpg", "hoge.tif"];

var my_doc = app.activeDocument;

var my_image = my_doc.allGraphics;

for (var i = (my_image.length - 1); i >= 0; i--) {

     var tmp_name = my_image.itemLink.name;

     for (var ii = 0; ii < del_list.length; ii++) {

          if (del_list[ii] === tmp_name) {

              my_image.parent.locked = false;

              my_image.remove();

          }

     }

}

--
seuzo
http://www.seuzo.jp/
dhishokAuthor
Known Participant
August 27, 2009

Wow!!! Its working greatly. Thanks for your timely support.

Thanks and regards

Thiyagu