Copy link to clipboard
Copied
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
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();
}
}
}
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
Copy link to clipboard
Copied
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();
}
}
}
Copy link to clipboard
Copied
Wow!!! Its working greatly. Thanks for your timely support.
Thanks and regards
Thiyagu
Copy link to clipboard
Copied
Morning - I found this while looking for a way to delete one image from over 500 docs - but not being a Java Expert in any way, I have an issue - with "Error 21 : Undefined is not an object:" in this line - "var tmp_name = my_image.itemLink.name;" - what am I missing? - BTW InDesign CC 2022 17.2.1
Thanks
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
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)
Copy link to clipboard
Copied
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 )