3 Replies Latest reply on Dec 17, 2014 8:13 AM by dsmithdcom

    Change links path to new server

    dsmithdcom

      Hi,

       

      This could also (perhaps more accurately) be titled: "Change a character in link path".

       

      We're migrating to a new NAS file server, and for compatibility with NFS and SAMBA shares, I've had to alter some folder names, by replacing instances of "/" with "-" in the names (don't ask).

       

      So e.g. "Brochures/Literature", a folder name in the link path needs to be changed to "Brochures-Literature", so the link can be re-established on the new server.

       

      I was not aware that Indesign supports scripting until I googled it just now, and I'm hopeful this may present an easier method of updating links 'on the fly' than manual re-linking.

       

      Has anyone created and/or used such a script you can point me to? I have some familiarity with javascript in web development however, I obviously have no experience with using scripts in Indesign, in case that warrants some extra detail on how it's done.

       

      Thanks!

       

      Message was edited by: David Smith for clarity

        • 1. Re: Change links path to new server
          Chinnadk Level 4

          Hi,

           

          Try this,

           

          var doc = app.activeDocument,
              links = doc.links.everyItem().getElements(),
              _folder = Folder.selectDialog("Select new path"),
              _files = [];
          
          
          getAllFiles(_folder);
          
          var len = links.length;
          while(len--)
          {
              for(var i=0;i<_files.length;i++)
              {
                  if(links[len].date.toString() == _files[i].modified.toString()){
                      if(_files[i].name == links[len].name)
                      {
                         links[len].relink(new File(_files[i].fsName));
                      }
                  }
              }
          }
          
          function getAllFiles(folder)
          {
              var filelist = folder.getFiles();
              for(var i =0;i<filelist.length;i++)
              {
                  if(filelist[i] instanceof Folder)
                  {
                      getAllFiles(filelist[i]);
                  }
                  else if(filelist[i] instanceof File)
                  {
                      _files.push(filelist[i]);
                  }
              }
          }
          

           

          Regards,

          Chinna

          • 2. Re: Re: Change links path to new server
            dsmithdcom Level 1

            Hi,

             

            Thanks for your reply. This is what I've rolled so far:

            ---

            main ();

            function main(){

                app.scriptPreferences.userInteractionLevel = UserInteractionLevels.interactWithAll;

                var myDoc=app.activeDocument;

                fixLinks(myDoc);

            }

            function fixLinks(myDoc) {

                var theLinks = myDoc.links;

                for ( var i = 0; i < theLinks.length; i++ ) {

                    var thePath = theLinks[i].filePath;

                    if ( thePath.match(/\//) ) {

                        var newPath = thePath.replace(/\//g,'-');

                        theLinks[i].relink(newPath);

                    }

                }

            }

            ---

            It *almost* works: calling newPath in an alert returns each corrected path (with "/" replaced by "-"), however, I've got the syntax of the last line "theLinks[i].relink(newPath)" wrong and it returns an error.

             

            Can anyone help me out with that?

             

            Thanks!

            ----------------------

            The error ^^^ was #35869: Cannot create the link resource from the given URI

             

            I tried reconfiguring the function:

            ---

            function fixLinks(myDoc) {

                var myLinks = myDoc.links;

                for ( var i = 0; i < myLinks.length; i++ ) {

                    var theLink = myLinks[i];

                    var thePath = theLink.filePath;

                    if ( thePath.match(/\//) ) {

                        var newPath = thePath.replace(/\//g,'-');

                        var newLink = new File(newPath);

                        theLink.relink(newLink);

                    }

                }

            }

            ---

            However this is throwing a new error (progress, I suppose) #29446: Either the file does not exist, you do not have permission, or the file may be in use by another application. Since I am not using update(), why is it checking the file? Do I need to do stuff like rewrite mac path ":" delimiters, and spaces in folder & filenames?

             

            I need to see the definition of a valid URI (if that's the issue).

            • 3. Re: Change links path to new server
              dsmithdcom Level 1

              OK so this works pretty well:

              ---

              main ();

              function main(){

                  app.scriptPreferences.userInteractionLevel = UserInteractionLevels.interactWithAll;

                  var myDoc=app.activeDocument;

                  fixLinks(myDoc);

              }

              function fixLinks(myDoc) {

                  var myLinks = myDoc.links;

                  for ( var i = 0; i < myLinks.length; i++ ) {

                      var theLink = myLinks[i];

                      var thePath = theLink.filePath;

                      if ( thePath.match(/\//) ) {

                          var newPath = thePath.replace(/\//g,'-');

                          var newPath2 = newPath.replace(/\:/g,'/');

                          var newLink = new File(newPath2);

                          theLink.relink(newLink);

                      }

                  }

              }

              ---

              The key seems to have been converting ":" to "/", to get Indesign to accept it as a valid path (even though it is run from a Mac).

               

              But here's a wrinkle: the script wants to validate the link, and if it can't (because e.g. there's no such file) then it halts (with an error message), without updating the paths (actully it updates all the changed paths up to the first fail, then it stops).

               

              Is there a way to bypass this link-checking, and have it complete the path fixes, so the user can hunt down missing links and then update from the Indesign Links palette, just as they would after opening any Indesign file with 'bad' links?

               

              I think our users would find this less confusing.

               

              Thanks