Skip to main content
tann90111232
Participant
Oct 3, 2017 8:35 AM UTC
Answered

Relink missing links with script

HI everyone, does anyone have a script that can: choose missing links from the active document and relink them to the folder that we also put its path into the script, so that i only need press 1 button to do it?

Kasyan Servetsky
Kasyan ServetskyCorrect answer
Legend
Oct 4, 2017 12:32 PM UTC

Thank you so much Kasyan. I make over 100 documents per day and about 100 photos in each document need to be relinked to only 1 folder. Yes the feature "Relink to folder" is the way I am doing. But in each document, some photos are already relinked and some are not. I just want to speed up workflow by only "pressing a button" and the scipt will: choose missing links in the document I am working on, and relink them to the folder I want (and i want to pass through the dialog box "choosing folder" because I have only 1 folder)

And i am apreciated about the script above but sadly it didnt work .


Here I wrote a quick & dirty script for you.

Change the path variable in line 9 to whatever the path you want.

Select an image with black or white arrow and run the script.

var scriptName = "Relink selected link to folder",

doc;

PreCheck();

//===================================== FUNCTIONS ======================================

function Main(image) {

    var file,

    path = "/E/Archive/My folder/", // type the path to your folder here

    link = image.itemLink;

   

    if (link.status == LinkStatus.LINK_MISSING) {

        file = new File(path + link.name);

       

        if (file.exists) {

            link.relink(file);

        }

    }

}

//--------------------------------------------------------------------------------------------------------------------------------------------------------

function PreCheck() {

    var image;

    if (app.documents.length == 0) ErrorExit("Please open a document and try again.", true);

    doc = app.activeDocument;

    if (doc.converted) ErrorExit("The current document has been modified by being converted from older version of InDesign. Please save the document and try again.", true);

    if (!doc.saved) ErrorExit("The current document has not been saved since it was created. Please save the document and try again.", true);

    if (app.selection.length == 0) ErrorExit("Nothing is selected.", true);

   

    if (app.selection[0].constructor.name == "Image") {

        image = app.selection[0];

    }

    else if (app.selection[0].images.length == 1) {

        image = app.selection[0].images[0];

    }

    else {

        ErrorExit("There's no image in selection.", true);

    }

    Main(image);

}

//--------------------------------------------------------------------------------------------------------------------------------------------------------

function ErrorExit(error, icon) {

    alert(error, scriptName, icon);

    exit();

}

//--------------------------------------------------------------------------------------------------------------------------------------------------------

18 replies

sirokolabs
Participant
Aug 6, 2026 4:35 PM UTC

This is an old thread, but for anyone finding it while looking for the same workflow: I’ve released a free script that does this without requiring the folder path to be hard-coded.

Smart Relinker Lite can:

  • relink missing links from a chosen folder,
  • search its subfolders recursively,
  • relink all document links after a project has been moved,
  • skip ambiguous duplicate filename matches instead of guessing.

It uses exact filename matching and is free:

https://sirokolabs.gumroad.com/l/SmartRelinkerLite

Full disclosure: I’m the developer.

Translate
Community Expert
Dec 14, 2020 8:12 PM UTC

Hi abdullah,

as far as I can see from your screenshot you run the script in PhotoShop.

Kasyan's script is for InDesign documents.

 

Best,
Uwe Laubender

( ACP )

Translate
infomamun
Inspiring
Dec 11, 2020 6:29 AM UTC

Why nothing is selected is showing while I have selected one image there?

Translate
Kasyan Servetsky
Legend
Oct 4, 2017 6:48 AM UTC

Why don't you want to use the Relink to Folder feature?

Translate
Anantha Prabu G
Legend
Oct 3, 2017 9:58 AM UTC
Design smarter, faster, and bolder with InDesign scripting.
tann90111232
Participant
Oct 4, 2017 1:49 AM UTC

Thank Prabu G, its nearly exactly what I am looking for. But one more thing, it is possible to put the path of the folder into the script, so I dont need to choose the folder anymore.

Translate
Kasyan Servetsky
Legend
Oct 4, 2017 7:05 AM UTC

But one more thing, it is possible to put the path of the folder into the script, so I dont need to choose the folder anymore.

I didn't look carefully into the script, but I'd made the following adjustments:

Note: the script processes all open documents and looks for the images starting from the 'root' folder and down there.

Below is my (quick & dirty) modified (totally untested) version:

var i, j, searchfolder; 

for (j=0; j<app.documents.length; j++) { 

    doc = app.documents; 

 

    imgs = doc.links; 

    path = "/E/Archive";  // type here the path for the folder to start

    searchfolder = new Folder(path); 

    for (i=0; i<imgs.length; i++) 

    { 

        // Missing? 

    $.writeln("Image "+i); 

        if (imgs.status == LinkStatus.LINK_MISSING) 

        { 

        $.writeln("Missing: "+imgs.name+" lookign in "+searchfolder); 

            // Do we have a file? 

            imglist = findAnyFileDownThere(imgs.name, searchfolder); 

        $.writeln("Found "+imglist.length+" possible replacements."); 

            if (imglist.length == 1) 

            { 

                // Oh yeah 

                imgs.relink(imglist[0]); 

        $.writeln("Relinked to "+imglist[0]); 

            } 

        } 

    } 

} 

function findAnyFileDownThere (filename, path) 

{ 

var result, folderList, fl; 

result = Folder(path).getFiles (filename+".*"); 

if (result.length > 0) 

  return result; 

folderList = Folder(path).getFiles(function(item) { return item instanceof Folder && !item.hidden; }); 

 

for (fl=0; fl<folderList.length; fl++) 

{ 

        $.writeln("Looking in: "+path+"/"+folderList[fl].name); 

  result = findAnyFileDownThere (filename, path+"/"+folderList[fl].name); 

  if (result.length > 0) 

   return result; 

} 

return []; 

} 

Translate