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

JavaScript To Replace Multiple Images With New Images

Engaged ,
Sep 29, 2020 Sep 29, 2020

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.


It's only an island if you look at it from the water.
TOPICS
How to , Scripting
586
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 ,
Sep 29, 2020 Sep 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

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
Engaged ,
Sep 29, 2020 Sep 29, 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.
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 ,
Sep 29, 2020 Sep 29, 2020
LATEST

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) {}
   }
}

 

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