4 Replies Latest reply on Apr 27, 2009 7:41 AM by Kasyan Servetsky

    [ID3 JS] Problem relinking files

    r_olsen

      Hi folks, I need to copy specific filetypes from a document to my disk. I've wrote the script below to help me automate this task. When I use the copyLink method InDesign updates the document to use new link location. To fix this I relinks all affected links back to the original filepath. However when relinking InDesign won't copy all my links. If I disable the relinking all files gets copied. My relinking function is obviously not well designed. I was hoping one of you could give me a hint or two?

      var myDocument = app.documents[0];
      var files = myDocument.links;
      var myStr = prompt ("Substring");
      if (myStr != null) {   var myFolder = Folder.selectDialog ("To where");   }
      if (myFolder != null) 
      {
            for (var i = 0; i < files.length; i++) {
               copyFiles(files[i], myFolder+"/"+myStr, true);
            }
      }
      
      function copyFiles(myLink, linkTo, doRelink) {
         var myOldFilePath = myLink.filePath;
         var myFolder = new Folder (linkTo);
         var myFile = new File(myFolder+"/"+myLink.name);
         if (!myFolder.exist) {
            myFolder.create();
         }   
         if (!myFile.exists) {
            myLink.copyLink(myFile);
            myFile.close();
            if (doRelink) {
               relinkTo(myLink,myOldFilePath);
            }
         }   
      }
      function relinkTo(myLink, linkTo) {
            var myFile = new File (linkTo);
            myLink.relink (myFile);
            myLink.update();
      }

      Regards, Rasmus