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

Script to batch relink files that have a uniform partial text string change in the file name

Contributor ,
Jun 19, 2023 Jun 19, 2023

Greetings,

I used ChatGPT to generate a script that would allow me to relink to a bunch of images that were now broken links because of a batch partial text string change in the file names. For example if the original image names change from "apples - image 1.png", "apples - image 2.png", etc  to the revised "apples for sale - image 1.png", "apples for sale - image 2.png". I tried it out and it works as expected, as long as the renamed images remain in the same exact location as the originals. Try it out and let me know how it goes. 

 

// Prompt the user to enter the old partial text string
var oldPartialText = prompt("Enter the old partial text string to be replaced", "");

// Prompt the user to enter the new partial text string
var newPartialText = prompt("Enter the new partial text string", "");

// Get all the links in the active document
var links = app.activeDocument.links;

// Loop through each link and relink based on the original file paths
for (var i = 0; i < links.length; i++) {
  var link = links[i];
  var originalFilePath = link.filePath;
  
  // Check if the original file path contains the old partial text string
  if (originalFilePath.indexOf(oldPartialText) !== -1) {
    // Construct the new file path by replacing the old partial text string with the new partial text string
    var newFilePath = originalFilePath.replace(oldPartialText, newPartialText);
    
    // Create a new File object with the updated file path
    var newFile = new File(newFilePath);
    
    try {
      // Relink the image
      link.relink(newFile);
    } catch (error) {
      alert("Error relinking file: " + link.name + "\nOriginal Path: " + originalFilePath + "\nNew Path: " + newFilePath + "\n\n" + error);
    }
  }
}

// Alert when the relinking process is complete
alert("Batch relinking completed!");

 

TOPICS
How to , Scripting
195
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 ,
Jul 18, 2023 Jul 18, 2023
LATEST

If it works for you then great - not really sure I would have use for such a script. 

 

There's a few threads with similar themes already
https://community.adobe.com/t5/indesign-discussions/a-script-to-batch-rename-and-relink-images/m-p/6...

 

Thanks for posting
If there's any specific help  you need with the script let us know.

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