Skip to main content
Chris Panny
Inspiring
September 29, 2020
Question

JavaScript To Replace Multiple Images With New Images

  • September 29, 2020
  • 1 reply
  • 727 views

Windows 10 64bit or iMac 2019 - both running Adobe CC 2020 apps.

 

I have multiple InDesign files that are about 250 pages each. My client wants me to replace anywhere from 30 - 40 pictures per file with new images. Is there a JavaScript file that can do this? I imagine I would have to enter the existing image file names along with the new file names into such a script, if it exists.

This topic has been closed for replies.

1 reply

brian_p_dts
Community Expert
Community Expert
September 29, 2020

There is the relink panel. You can hold shift to multi-select. Otherwise, yeah, something like: 

 

 

 

var arr = [
   ["oldName.jpg", "full/path/to/newimage.jpg"],
   ["oldName2.pdf", "full/path/to/newName2.pdf"],
   //etc. 
];
var theLink;
var theDoc = app.documents[0];
for (var i =0; i < arr.length; i++) {
   theLink = theDoc.links.itemByName(arr[i][0]);
   theLink.relink(File(arr[i][1]);
}

 

 

 

You'll run into issues, though, if the same link is used multiple times in a doc. 

 

You can also look at this existing relink script: https://indesignsecrets.com/a-script-for-relinking-images.php

Chris Panny
Inspiring
September 30, 2020

Thanks for your reply! What kind of issue would I run into if the same pic is used multiple times? Won't it just replace all instances of it with the new pic?

It's only an island if you look at it from the water.
brian_p_dts
Community Expert
Community Expert
September 30, 2020

Because itemByName will only grab one link. We probably have to iterate through the whole link list instead. Maybe something like this, but could probably use refinement. Note the syntax change from using an array to object: 

 

var theObj = {
   "oldName.jpg": "full/path/to/newimage.jpg",
   "oldName2.pdf": "full/path/to/newName2.pdf",
   //etc. 
};
var theLinks = app.documents[0].links.everyItem().getElements();
for (var i =0; i < theLinks.length; i++) {
   if (theObj.hasOwnProperty(theLinks[i].name)) {
      try { 
            theLinks[i].relink(File(theObj[theLinks[i].name]));
      } catch(e) {}
   }
}