Skip to main content
ascotto
Known Participant
January 27, 2015
Answered

relink images with different names

  • January 27, 2015
  • 2 replies
  • 9446 views

Hi all, I need to relink lots of -missing- images (i.e. "my_favourite_pic_001_LR.jpg") to their high resolution versions (i.e. "my_favourite_pic_001_HR.jpg"). So, is there a way to tell InDesign to 'read' all the characters but the last two before the dot, while searching for links? HR images are all in one folder, so there's no danger to relink wrong ones. I'm pretty a noob with js or applescript, but I'll be glad if someone of you could help me

This topic has been closed for replies.
Correct answer Jump_Over

Hi,

Here is javascript (place and run from InDesign Script Panel):

var

  sourceFolder = Folder.selectDialog("Show me a source folder"),

  mLinks = app.activeDocument.links.everyItem().getElements(),

  cLink, cFile;

while ( cLink = mLinks.pop() ) {

  if (cLink.status == LinkStatus.NORMAL) continue;

  cFile = File(sourceFolder + "/" + cLink.name.replace("LR.","HR.") );

  if ( !cFile.exists ) continue;

  cLink.relink(cFile);

  }

Jarek

2 replies

Inspiring
January 11, 2021

Worked perfectly! Thanks Jump_Over. I had over 150 images I had to quickly rename (using NameChanger) adding 'GG' (GG.jpeg). I thought I'd have to find/update each image one by one in Links panel. 

Jump_Over
Jump_OverCorrect answer
Legend
January 27, 2015

Hi,

Here is javascript (place and run from InDesign Script Panel):

var

  sourceFolder = Folder.selectDialog("Show me a source folder"),

  mLinks = app.activeDocument.links.everyItem().getElements(),

  cLink, cFile;

while ( cLink = mLinks.pop() ) {

  if (cLink.status == LinkStatus.NORMAL) continue;

  cFile = File(sourceFolder + "/" + cLink.name.replace("LR.","HR.") );

  if ( !cFile.exists ) continue;

  cLink.relink(cFile);

  }

Jarek

Participating Frequently
January 14, 2019

Hi Jump_Over!

I, too, have a modification request

I copy project folder every week, and need to rename files with week number, eg. week02.pdf -> week03.pdf. Doing this by hand is sometimes time consuming.

Is it possible to have a search/replace kind of code, which asks:

1. source folder

2. search term (week02)

3. replace term (week03)

and then does the renaming to all missing files.

xxxxxxxxxxxxxxxxxxxxyyyy
Participating Frequently
January 14, 2019

Hi johanna.pakkala​,

you can try this script

#targetengine batchRelink

var desktopFolder = new Folder("~/desktop");

sourceFolder = desktopFolder.selectDlg("Please select folder");

if (!sourceFolder) {exit();}

var files = sourceFolder.getFiles();

if (files.length == 0) {exit();}

var w = new Window ("palette");

var div = w.add("group");

div.orientation = "column";

var inputDim = [0, 0, 200, 25];

var searchgrp = div.add("group");

searchgrpLabel = searchgrp.add("statictext", undefined, "Search Term: ");

var searchinput = searchgrp.add("edittext", inputDim, undefined);

searchinput.active = true;

var replacegrp = div.add("group");

replacegrpLabel = replacegrp.add("statictext", undefined, "Replace Term: ");

var replaceinput = replacegrp.add("edittext", inputDim, undefined);

var buttons = div.add("group");

buttons.orientation = "row";

var renamebtn = buttons.add("button", undefined, "Rename and relink");

var closebtn = buttons.add("button", undefined, "exit");

closebtn.onClick = function () {w.close();}

renamebtn.onClick = function () {w.close(); renameRelink(files);}

w.show();

function renameRelink (filesArray)

    {

        var searchterm = searchinput.text, replaceterm = replaceinput.text, doc = app.activeDocument;

       

        for (var x = 0; x < filesArray.length; x++)

            {

                var oldname = filesArray.name, newname = "";

               

                if (oldname.search(searchterm) != -1)

                    {

                        newname = oldname.replace(searchterm, replaceterm);

                       

                        filesArray.rename(newname);               

                       

                        for (var i = 0; i < doc.links.length; i++)

                            {

                                if (doc.links.name == oldname)

                                    {

                                        doc.links.relink(File(sourceFolder + "/" + newname));

                                    }

                            }

                       

                    }

            }

       

    }