14 Replies Latest reply on Aug 26, 2008 12:55 AM by Kasyan Servetsky

    Indd - JS: Searching an image in subfolders and relinking

      Hi,

      Can any one help me....
      In an Indesign Document I want to relink all the images to a new path.
      Please see the below script, it search for the images in a specified path only. But i want to search the images in a particular directory along with its sub-folder too.

      var myFolder = Folder.selectDialog("Select Folder for Images");
      var new_path = myFolder+ '/'
      imgs = app.activeDocument.allGraphics
      for (i = 0; i < imgs.length; i++)
      {

      img = imgs[i].itemLink

      myFile = File (new_path + img.name);

      if (myFile.exists) {
      img.relink (File (new_path + img.name))
      img.update()
      }
      }

      Thanks in advance,
      Bharathi Raja G.
        • 1. Re: Indd - JS: Searching an image in subfolders and relinking
          Martin Fischer Level 2
          Hi,

          I am just working on a script like yours.
          To catch files in subfolders I use the following function:


          var topFolder = Folder.selectDialog();
          var myResult = scanSubFolders( topFolder );

          function scanSubFolders( tFolder )
          {
          var sFolders = new Array();
          var allFiles = new Array();
          sFolders[0] = tFolder;
          for ( var j = 0; j < sFolders.length; j++ ) // loop through folders
          {
            var procFiles = sFolders[j].getFiles();
            for ( var k = 0; k < procFiles.length; k++ ) // loop through this folder contents
            {
             if ( procFiles[k] instanceof File )
              allFiles.push( procFiles[k] );
              else if ( procFiles[k] instanceof Folder )
              sFolders.push( procFiles[k] );
             }
            }
          return allFiles;
          }


          If you wish to you can return the subfolders too:

          return {files:allFiles, folders:sFolders};


          Martin
          • 2. Re: Indd - JS: Searching an image in subfolders and relinking
            Martin Fischer Level 2
            Hello Bharathi,

            you will have to take care of one thing:
            > img = imgs[i].itemLink

            There may be images without a link (e.g. embedded images).
            With them you would run into an error adressing their itemLink.

            Martin
            • 3. Re: Indd - JS: Searching an image in subfolders and relinking
              Level 1
              Here's what I use to locate files in a tree:

              function findFiles(folder, mask) {
              var files = folder.getFiles(mask);
              var folders = folder.getFiles(function(f) { return f instanceof Folder; });

              for (var i = 0; i < folders.length; i++) {
              var f = folders[i];
              var ffs = findFiles(f, mask);

              // Array.concat occasionally fails in CS/CS2. Do it manually
              while (ffs.length > 0) {
              files.push(ffs.shift());
              }
              }
              return files;
              };

              This has been sliced out of some other code

              The mask parameter can be a string, RegExp, or a function. In your case, the
              call would probably look something like this:

              myFile = findFile(img.name);

              -X
              --
              for photoshop scripting solutions of all sorts
              contact: xbytor@gmail.com
              • 4. Re: Indd - JS: Searching an image in subfolders and relinking
                Kasyan Servetsky Level 5
                Hi Bharathi,

                I recently wrote a similar script, you can download it from here:
                http://www.businessweekly.h.com.ua/relink.html

                See discussion here:
                http://www.adobeforums.com/webx?128@@.59b50466

                Kasyan
                • 5. Re: Indd - JS: Searching an image in subfolders and relinking
                  Level 1
                  Hi,

                  Thanks for your immediate response and timely help.
                  I will check with it.

                  Bharathi Raja G.
                  • 6. Re: Indd - JS: Searching an image in subfolders and relinking
                    Level 1
                    Hi Kasyan,

                    The script in below links is not working for MAC. But in PC it works well. Can you please check with it

                    http://www.businessweekly.h.com.ua/relink.html

                    Thanks
                    Bharathi Raja G.
                    • 7. Re: Indd - JS: Searching an image in subfolders and relinking
                      Loic.Aigon Adobe Community Professional
                      Something that work on PC should work on mac & the only reason it fails may have only one reason : It is the way to talk to folders.
                      on PC file paths will be like "c:/xxx/yyyy/zzz" or "/c/xxx/yyy/zzz" when it's more like ":Macintosh HD:Users:John Doe:xxx:yyy:zzz"

                      So I will check for file and folder paths as a priority degug way.
                      Loic
                      • 8. Re: Indd - JS: Searching an image in subfolders and relinking
                        Level 1
                        The filePath property takes two forms. When it is the property of a link, it is a platform-dependent string. The way to work with this property in a cross-platform or platform-independent way is to always do this:

                        myLinkedFilePath = File(myLink.filePath).fullName;

                        that will give you exactly what myLink.filePath purports to give you but in a platform independent format that works on either platform even if the link in question started life on the other platform.

                        Dave
                        • 9. Re: Indd - JS: Searching an image in subfolders and relinking
                          Kasyan -- your script works great. Thank you!!

                          -- Rick
                          • 10. Re: Indd - JS: Searching an image in subfolders and relinking
                            Kasyan Servetsky Level 5
                            Hi Bharathi Raja G,

                            I just returned from vacation so I couldnt respond you earlier, but I hope you are still reading this forum.

                            > The script in below links is not working for MAC.

                            Actually, I wrote it for both platforms and I tested it, although superficially, on Mac and PC.

                            > Can you please check with it

                            Yes, but I need more information: what error message you get, the line number the script stops.

                            Kasyan
                            • 11. Re: Indd - JS: Searching an image in subfolders and relinking
                              Level 1
                              Hi Kasyan,

                              Sorry for the Late reply.
                              When I run the script on mac I got error with the progress bar.

                              Especially I am getting error with the below coding

                              ------------------------------------------------------------
                              var w = new Window ( 'window', myCurrFolderName );
                              var pb = w.add ('progressbar', [12, 12, 350, 24], 0, stop);
                              var txt = w.add('statictext');
                              txt.bounds = [0, 0, 340, 20];
                              txt.alignment = "left";
                              w.show();
                              var a = 1;
                              ------------------------------------------------------------
                              But, the scripts runs well by removing the progress bar coding.

                              Also, Can you keep a checkpoint(to exit the process) that if all the images was linked successfully the script should stop searching for the further folders.
                              For Example: If I search an Image in a directory contains 10 sub folders in which the required image is in 1st sub folder. The script
                              should not search for the remaining 2-10 sub folders.

                              Thanks for your response,
                              Bharathi Raja G.
                              • 12. Re: Indd - JS: Searching an image in subfolders and relinking
                                Kasyan Servetsky Level 5
                                >When I run the script on mac I got error with the progress bar.

                                It looks like you have CS2. Recently I received an e-mail from a guy who had a similar problem so I posted two versions of the script, both for CS2 and CS3.

                                >Can you keep a checkpoint(to exit the process) that if all the images was linked successfully the script should stop searching for the further folders.

                                I'll work on it when I have time.

                                Kasyan
                                • 13. Re: Indd - JS: Searching an image in subfolders and relinking
                                  Hi Kasyan

                                  Good Day. I have received Indesign files from Copenhagen. I dont know why the all the links are missing.

                                  The links looks like this:

                                  Dell:ImageBank:Products_Frontcover:None_environment_front:Printers_nev_fc:1720_laser_prn_n ev:layouts:1720_prn_nev_02b_fc.psd

                                  All the link files are in our server.

                                  ImageBank\Products_Frontcover\None_environment_front\Printers_nev_fc\1720_laser_prn_nev:la youts\1720_prn_nev_02b_fc.psd

                                  The problem is the file location shows : instant of \

                                  Can you please help me out we are using Indesign CS.

                                  Thanks

                                  Shanto
                                  • 14. Re: Indd - JS: Searching an image in subfolders and relinking
                                    Kasyan Servetsky Level 5
                                    Hi Shanto,

                                    You can download a script for updating path names in links: http://www.businessweekly.h.com.ua/relink.html
                                    You select a folder, and, for every image in the active document, it searches for the file that has the same name as the old one, and if it's found, relinks it.
                                    If the file with the exact name is not found, the script tries to find a file with the same base name but a different extension (".psd", ".tif", ".jpg", ".ai", ".eps", ".pdf", ".indd").
                                    If the script doesn't find the new file in the chosen folder, it repeats the above-mentioned process for every subfolder.
                                    But this script was written for CS2/CS3 and I have doubt that it would work in CS. I can’t check this as I don’t have CS any more.

                                    To all appearance, Indesign files, you received, were created on Mac, but you are on PC. I remember this problem in CS – it has been corrected since CS2.
                                    In such cases I use the following workaround:
                                    1.Copy Indesign file to the folder where links located.
                                    2.Open it, Indesign should find all lost links in this folder and its subfolders.
                                    3.Save and close the document.
                                    4.Copy/move the Indesign file to its original location.

                                    Kasyan