10 Replies Latest reply on May 27, 2015 12:48 PM by Daniel Sterchi

    Change link path

    sketcg

      I am looking for a way to change the file path of all my links in a document. I just need to remove part of the file path...

      If the original file path is
      /Volumes/My Passport/HiRes/990/9906.psd

      and I need to make it

      HiRes/990/990.psd

       

      Is there an action script or some hidden trick to rename all the links?

      I'm working on a mac in OS 10.9.5

       

      Thanks for any help!

        • 1. Re: Change link path
          Daniel Sterchi Adobe Community Professional

          Hi sketcg

           

          I have put together a few lines. Try them if the meet your needs.

          Would like to hear what you think.

           

          kind regards

           

          Daniel (from Switzerland)

           

          /*
           * written by Daniel Sterchi moonsoft.ch GmbH
          */
          
          main();
          exit();
          
          function main() {
              
              if (app.documents.length > 0) {
                  
                  const delimiter = "/";      // "/" for mac "\\" for windows at lieast I think so (have a mac)
          
                  // the active document
                  var myDoc = app.activeDocument;
          
                  // get all the links of this document
                  var oldLinks = myDoc.links.everyItem().getElements();
                  
                  // if there are any links
                  if (oldLinks.length > 0) {
          
                      // open a dialog
                      var newPath = new Folder(oldLinks[0].filePath).selectDlg("Choose a directory").fsName;
          
                      for (var i = 0; i < oldLinks.length; i++) {
                          
                          // to get the filename of the Link.
                          // for windows you can use the folowing code
                          //      var myFileName = oldLinks[i].filePath;
                          //      myFileName = myFileName.substr(myFileName.lastIndexOf(delimiter) + 1, myFileName.length);
                          //      var newFile = File(newPath + delimiter + myFileName);
                          
                          // This way you dont have to bother with HFS+ or UFS directory names with macs
                          var oldFile = File(oldLinks[i].filePath);
                          var newFile = File(newPath + delimiter + oldFile.name);
          
                          oldLinks[i].relink(newFile);
                      }
                  }
                  else alert("Information\nThis document has no links to pictures");
              }
              else alert("Warning\nNo document open");
              
              alert("Information\nI have done my job, what about yours");
          }
          
          • 2. Re: Change link path
            BSKTCreation Level 2

            Hi Daniel,

             

            On line 12 in your comments you mention Windows possibly being back-slashes, that is true for Windows explorer but for InDesign JavaScript it is forward-slashes.

             

            For example:

             

            "/v/Folder Name/File Name.indd"

             

            or

             

            "//server.address.com/Folder Name/File Name.jsx"

             

            Regards,

            Brett

            • 3. Re: Change link path
              Daniel Sterchi Adobe Community Professional

              Hi Brett

               

              Thats good news. I just don't have windows so I can't test it.

              Thanks for your input.

               

              Daniel (from Switzerland)

              • 4. Re: Change link path
                sketcg Level 1

                Daniel! Thank you so much for taking the time to look at this. I am a novice with the actionscript.

                I have taken your code and tried to use it but I get an error messageerror copy.jpg

                I will continue on trying to figure what I have wrong, but please let me know if you have a suggestion.

                Again Thanks!

                • 5. Re: Change link path
                  Daniel Sterchi Adobe Community Professional

                  Hi sketcg

                   

                  What the error message is telling you is, that there is a quotation mark missing at line 24 or above. And actually it is a JavaScript script ;-)

                   

                  Can you provide us the changed script, then I can tell you what goes wrong.

                   

                  kind regards

                   

                  Daniel (from Switzerland)

                  • 6. Re: Change link path
                    Vamitul Level 4

                    Regarding the Windows vs OSX delimiter:
                    Since the script uses ".fsName" you will have to worry about them because fsName will return the path in the standard platform format (eg. "c:\test\folder" or "/Users/Test/Folder").
                    My approach for these kinds of situations is to always use the Posix type of path for everything ("/c/test/folder"  works just fine on windows) and use fsName only for display purposes.

                    • 7. Re: Change link path
                      Daniel Sterchi Adobe Community Professional

                      Hi Vamitul

                       

                      The problem with the mac is that have HFS+ and UFS. I have to make sure that the old path and the new path are in the same format. Otherwise you have something like

                       

                      newpath UFS: "/Users/…"

                      oldPath HFS+ "Macintosh HD:Users:USERNAME:directory

                       

                      I just could do some tests on windows.

                      Here is what I get for Mac

                      Screen Shot 2015-05-26 at 10.44.10.png

                      and here what I get from Windows

                      pfade_windows.PNG

                      Based on what I just saw i would change the script from:

                      var newPath = new Folder(oldLinks[0].filePath).selectDlg("Choose a directory").fsName;
                      

                       

                      to:

                      var newPath = new Folder(oldLinks[0].filePath).selectDlg("Choose a directory").fullName;
                      

                       

                      kind regards

                       

                      Daniel (from Switzerland)

                      • 8. Re: Change link path
                        Daniel Sterchi Adobe Community Professional

                        Hi sketcg

                         

                        This is a much smaller variation of the code which also works. It is also faster as the first one.

                         

                        kind regards

                         

                        Daniel (from Switzerland)

                         

                        /*
                        * written by Daniel Sterchi moonsoft.ch GmbH
                        */
                        
                        main();
                        exit();
                        
                        function main() {
                            
                            if (app.documents.length > 0) {
                                
                                const delimiter = "/";
                        
                                // the active document
                                var myDoc = app.activeDocument;
                        
                                // get all the links of this document
                                var oldLinks = myDoc.links.everyItem().getElements();
                                
                                // if there are any links
                                if (oldLinks.length > 0) {
                        
                                    // open a dialog
                                    var newPath = new Folder(oldLinks[0].filePath).selectDlg("Choose a directory").fullName;
                                  
                                    // traverse the array and relink
                                    for (var i = 0; i < oldLinks.length; i++) {
                                        
                                        var newFile = File(newPath + delimiter + oldLinks[i].name);
                                        oldLinks[i].relink(newFile);
                                    }
                                }
                                else alert("Information\nThis document has no links");
                            }
                            else alert("Warning\nNo document open");
                            
                            alert("Information\nI have done my job, what about yours");
                        }
                        
                        
                        • 9. Re: Change link path
                          [Jongware] Most Valuable Participant

                          It looks like the end double quote got converted to a curly one (hence "unterminated string constant" because the starting quote is still okay). If that's the case, it is kind of weird because it is correct in Daniel's code.

                          • 10. Re: Change link path
                            Daniel Sterchi Adobe Community Professional

                            Hi sketcg


                            Did you get it to work? Found the problem?


                            kind regards


                            Daniel (from Switzerland)