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

JavaScript To Replace Multiple Images With New Images

Engaged ,
Sep 29, 2020 Sep 29, 2020

Copy link to clipboard

Copied

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

Views

297

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

Copy link to clipboard

Copied

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

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

Copy link to clipboard

Copied

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.

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

Copy link to clipboard

Copied

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

 

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