19 Replies Latest reply on Dec 13, 2011 8:36 AM by [Jongware]

    need to relink missing links to files with extensions

    LSHess Level 1

      I'm working with files received from another company. All the InDesign files have links listed in the Links palette with no extensions. The actual link files themselves HAVE extensions (mostly .jpg, some .psd, .ai, .pdf, etc.). All the links show up as missing, and when I try to relink them I have to do them all individually, even though all the necessary links are in the same folder (just WITH extensions). I've tried "Relink to Folder" and "Relink File Extension," but because there isn't an extension to begin with, it's just not working. It's really time-consuming to relink them one at a time, since there are about 40 InDesign files that average about 40 links each.

       

      I only have one choice for what file to relink to, since the links are supplied. The file name matches perfectly with what is displayed in the Links panel, all except for the extension. For example, "CBZ70.howtoshootbw.beach1" is displayed in the Links panel, and I want to link to "CBZ70.howtoshootbw.beach1.jpg". I know extra periods in the filename is a big no-no. This is another unfortunate aspect of the project. It is the naming convention used for hundreds of links. I have software for batch file renaming, but for this project, I really want to avoid changing the filenames. This is the first of 12 similar projects, all with the same problem, so my hope is to find a solution that will help both now and in the future.

       

      I posted this in the general InDesign forum (http://forums.adobe.com/message/4033140#4033140), and users there thought this should be scriptable. Can anyone offer any help? Thank you!

        • 1. Re: need to relink missing links to files with extensions
          LSHess Level 1

          (responding here to a question in the other thread)

          Hi John,

           

          This is the thread where I pulled the script, hoping to modify it. Not sure if it's got the right structure or not:

           

          http://www.mombu.com/computer_design/indesign/t-rename-and-relink-images-indesign-cs4-osx1 05-3648503.html

          • 2. Re: need to relink missing links to files with extensions
            John Hawkinson Level 5

            No, that's definitely not what you want. That is a script that renames all the links in a document (for instance, to give them some structured naming), and then renames files to match. It is fundamentally about renaming and not about finding missing links.

             

            You want one of the scripts that is designed to fix broken links by finding the files (in a directory you specify, typically), and then adjust it to look more broadly than for the one filename it is looking for.

            • 3. Re: need to relink missing links to files with extensions
              [Jongware] Most Valuable Participant

              Does this Javascript work for you?

              It checks all links and attempts to find 'any' file for unlinked ones. If it finds one (and only one) file, it relinks to it.

               

              I have no idea how you would test if it works correctly

               

              doc = app.activeDocument;
              imgs = doc.links;
              path = doc.filePath;
              
              
              for (i=0; i<imgs.length; i++)
              {
                        // Missing?
                        if (imgs[i].status == LinkStatus.LINK_MISSING)
                        {
                                  // Do we have a file?
                                  imglist = Folder(path).getFiles (imgs[i].name+".*");
                                  if (imglist.length == 1)
                                  {
                                            // Oh yeah
                                            imgs[i].relink(imglist[0]);
                                  }
                        }
              }
              
              • 4. Re: need to relink missing links to files with extensions
                [Jongware] Most Valuable Participant

                [Jongware] wrote:

                 

                I have no idea how you would test if it works correctly

                 

                On further reflection, if you're not feeling too confident about it I could make it generate a report of what it changed.

                • 5. Re: need to relink missing links to files with extensions
                  John Hawkinson Level 5

                  Sigh, you're right. I don't know why I was so resistant to doing it from scratch, there really wasn't all that much to leverage.

                  I suppose I should contribute something. Since LSHess told us she has 40 documents to do, we might as well make the script operate on all currently open documents, so she can open them all and run the script once:

                   

                   

                  var i, j;
                  for (j=0; j<app.documents.length; j++) {
                      doc = app.documents[j];
                    
                      imgs = doc.links;
                      path = doc.filePath;
                      
                      for (i=0; i<imgs.length; i++)
                      {
                          // Missing?
                          if (imgs[i].status == LinkStatus.LINK_MISSING)
                          {
                              // Do we have a file?
                              imglist = Folder(path).getFiles (imgs[i].name+".*");
                              if (imglist.length == 1)
                              {
                                  // Oh yeah
                                  imgs[i].relink(imglist[0]);
                              }
                          }
                      }
                  }
                  

                   

                  I would imagine the Links panel is sufficient feedback?

                  • 6. Re: need to relink missing links to files with extensions
                    LSHess Level 1

                    Thanks very much for your reponses. I tried the last script (from John), but when I ran it on one of my InDesign docs, nothing happened. Should this script ask me to point it to the right directory for where to find the link?

                    • 7. Re: need to relink missing links to files with extensions
                      [Jongware] Most Valuable Participant

                      LSHess wrote:

                       

                      .... Should this script ask me to point it to the right directory for where to find the link?

                       

                      Well no, since you stated

                       

                      even though all the necessary links are in the same folder (just WITH extensions)

                       

                      so the script first retrieves the active document's path (line #5 in John's script) and uses that to try to find the image in there.

                      • 8. Re: need to relink missing links to files with extensions
                        [Jongware] Most Valuable Participant

                        Here's a mogrification to have it search in the original file path:

                         

                        doc = app.activeDocument;
                        imgs = doc.links;
                        path = doc.filePath;
                        
                        
                        for (i=0; i<imgs.length; i++)
                        {
                                  // Missing?
                                  if (imgs[i].status == LinkStatus.LINK_MISSING)
                                  {
                                            // Do we have a file?
                                  //          alert ("path was "+File(imgs[i].filePath).path+"\rName: "+imgs[i].name);
                                            imglist = Folder(File(imgs[i].filePath).path).getFiles (imgs[i].name+".*");
                                            if (imglist.length == 1)
                                            {
                                                      // Oh yeah
                                                      imgs[i].relink(imglist[0]);
                                            }
                                  }
                        }
                        
                        • 9. Re: need to relink missing links to files with extensions
                          John Hawkinson Level 5

                          Bummer. Well, here's a modification of the script to print out what it's doing. It'll do so in the Javascript Console window of the ExtendScript Toolkit, which should launch automatically when you run the script.

                           

                           

                          var i, j;
                          for (j=0; j<app.documents.length; j++) {
                              doc = app.documents[j];
                            
                              imgs = doc.links;
                              path = doc.filePath;
                              
                              $.writeln("Processing document "+j+" from path "+path);
                          
                              for (i=0; i<imgs.length; i++)
                              {
                                  // Missing?
                              $.writeln("Image "+i);
                                  if (imgs[i].status == LinkStatus.LINK_MISSING)
                                  {
                                  $.writeln("Missing: "+imgs[i].name);
                                      // Do we have a file?
                                      imglist = Folder(path).getFiles (imgs[i].name+".*");
                                  $.writeln("Found "+imglist.length+" possible replacements.");
                                      if (imglist.length == 1)
                                      {
                                          // Oh yeah
                                          imgs[i].relink(imglist[0]);
                                  $.writeln("Relinked to "+imglist[0]);
                                      }
                                  }
                              }
                          }
                          
                          • 10. Re: need to relink missing links to files with extensions
                            LSHess Level 1

                            Thanks. I ran this latest script from John... here's an excerpt of the report in the JavaScript Console window:

                             

                            Image 6

                            Missing: CBZ70.bw_feat1.abstract_bw

                            Found 0 possible replacements.

                             

                            (it was the same "Found 0 possible replacements" report for all the links)

                             

                            Jongware, What I meant by

                            when I try to relink them I have to do them all individually, even though all the necessary links are in the same folder (just WITH extensions)

                            is that all the links for the InDesign document are in one folder, which would usually mean once you find one link, InDesign will find the rest of the links located in that folder.

                             

                            The location of the folder containing the links is much different now, since the files now live on my company's server instead of the previous location (at the company from whom we got the files). Sorry, I was unclear. For each of the 40-some InDesign files, there is a folder of links. I'd love for the script to ask me where to find the files. I could either point it to one of the 40 folders (which I would prefer), or I could combine all the links into one folder. Either way, I think I need to tell it where to look.

                             

                            I really appreciate both of your help, and your patience as I try to explain exactly what I'm trying to do.

                            • 11. Re: need to relink missing links to files with extensions
                              John Hawkinson Level 5

                              Well, one of the above scripts would have worked if you jsut moved the ID files in with the links, but here you go, with prompting:

                              var i, j, searchfolder;
                              for (j=0; j<app.documents.length; j++) {
                                  doc = app.documents[j];
                              
                                  imgs = doc.links;
                                  path = doc.filePath;
                              
                                  $.writeln("Processing document "+j+" from path "+path);
                                  searchfolder = searchfolder || new Folder(path);
                                  searchfolder = searchfolder.selectDialog(
                                  "Find a path to "+doc.name+"'s images. Should contain "+
                                      imgs[0].name+".*");
                              
                                  for (i=0; i<imgs.length; i++)
                                  {
                                      // Missing?
                                  $.writeln("Image "+i);
                                      if (imgs[i].status == LinkStatus.LINK_MISSING)
                                      {
                                      $.writeln("Missing: "+imgs[i].name+" lookign in "+searchfolder);
                                          // Do we have a file?
                                          imglist = searchfolder.getFiles (imgs[i].name+".*");
                                      $.writeln("Found "+imglist.length+" possible replacements.");
                                          if (imglist.length == 1)
                                          {
                                              // Oh yeah
                                              imgs[i].relink(imglist[0]);
                                      $.writeln("Relinked to "+imglist[0]);
                                          }
                                      }
                                  }
                              }
                              

                               

                              Edit: err, fixed a typo.

                              1 person found this helpful
                              • 12. Re: need to relink missing links to files with extensions
                                LSHess Level 1

                                Here's what I got when I tried this script (I named it "Find_Missing_Links5.jsx"):

                                Screen shot 2011-11-19 at 4.15.46 PM.png

                                • 13. Re: need to relink missing links to files with extensions
                                  John Hawkinson Level 5

                                  That's the danger of not testing this stuff...change searchfolder.selectDialog on line 10 to searchfolder.selectDlg.

                                  1 person found this helpful
                                  • 14. Re: need to relink missing links to files with extensions
                                    LSHess Level 1

                                    Brilliant!!!! It works. You guys get a HUGE gold  star. Awesome. I can't thank you enough.

                                    • 15. Re: need to relink missing links to files with extensions
                                      LSHess Level 1

                                      One more thing... Could this script be modified to search any subfolders within the folder I target? This would save me even MORE time. Thank you!

                                      • 16. Re: need to relink missing links to files with extensions
                                        [Jongware] Most Valuable Participant

                                        (Last night just as I was typing my answer, my modem blew out! ... So I went to sleep first. )

                                         

                                        Can you test this? I don't have a file at hand with links with missing extensions (and in subfolders to boot), but according to the debug output it should be looking down into whatever folders are there.

                                         

                                        var i, j, searchfolder;
                                        for (j=0; j<app.documents.length; j++) {
                                            doc = app.documents[j];
                                        
                                            imgs = doc.links;
                                            path = doc.filePath;
                                        
                                            $.writeln("Processing document "+j+" from path "+path);
                                            searchfolder = searchfolder || new Folder(path);
                                            searchfolder = searchfolder.selectDlg(
                                            "Find a path to "+doc.name+"'s images. Should contain "+
                                                imgs[0].name+".*");
                                        
                                            for (i=0; i<imgs.length; i++)
                                            {
                                                // Missing?
                                            $.writeln("Image "+i);
                                                if (imgs[i].status == LinkStatus.LINK_MISSING)
                                                {
                                                $.writeln("Missing: "+imgs[i].name+" lookign in "+searchfolder);
                                                    // Do we have a file?
                                                    imglist = findAnyFileDownThere(imgs[i].name, searchfolder);
                                                $.writeln("Found "+imglist.length+" possible replacements.");
                                                    if (imglist.length == 1)
                                                    {
                                                        // Oh yeah
                                                        imgs[i].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 [];
                                        }
                                        
                                        • 17. Re: need to relink missing links to files with extensions
                                          John Hawkinson Level 5

                                          Hmm, that's a bit more annoying.  Does it have to search to infinite depth? Or just folders 1 level beneath the specified folder?

                                          • 18. Re: need to relink missing links to files with extensions
                                            LSHess Level 1

                                            Sorry... been off for a few days.

                                             

                                            Jongware, this new script does just what I need it to! Thanks so much.

                                             

                                            John, just to answer your question... I only need it to search folders one level beneath the specified folder.

                                             

                                            You guys are great!

                                            • 19. Re: need to relink missing links to files with extensions
                                              [Jongware] Most Valuable Participant

                                              Just in case: this new version first searches in the folder you point it to, and then in any and all sub-folders. It does so recursively: it searches in the first sub-folder and then in all folders inside that; if it doesn't find your image in there it backs up one step and searches in the next sub-folder and all of its sub-sub-folders.

                                               

                                              So the search order could be not what you'd expect -- well, that's computers for you ... If you find you get unexpected results because of this, it's also possible to limit the search to one level deep.