11 Replies Latest reply on Jan 21, 2016 8:44 AM by chillbot75

    Help with renaming and relinking specific file types

    chillbot75

      Hi.

       

      I have several INDD Files that have links of varying file types. I'm in the process of renaming each linked image according to file type and in sequence.

       

      Example

      ImageAB001_IL01.eps

      ImageAB001_IL02.eps

      ImageAB001_IL03.eps

      ImageAB001_ART01.tif

      ImageAB001_ART02.tif

      ImageAB001_ART03.tif

      ImageAB001_LYRD01.psd

      ImageAB001_LYRD02.psd

      ImageAB001_LYRD03.psd

       

      I've seen a couple scripts that can rename and relink. But, none are specific for file type.

      Is this possible to do?

       

      Any help would be greatly appreciated.

      Thanks!

      -chill

        • 1. Re: Help with renaming and relinking specific file types
          LeoMari Level 2

          Hello .. I have worked with that but I did not understand if that's what you seek.

           

          if(app.documents.length != 0){
           if((app.documents.item(0).allGraphics.length != 0)&&(app.selection.length != 0)){
            //A production version of this script would probably include
            //a more complete list of graphic/page item types.
            switch(app.selection[0].constructor.name){
             //If a frame is selected with the Selection tool...
             case "Rectangle":
             case "Oval":
             case "Polygon":
              if(app.selection[0].graphics.length != 0){
               myDisplayDialog(app.selection[0].graphics.item(0));
              }
              break;
             //If a graphic is selected with the Direct Selection tool...
             case "Image":
             case "EPS":
             case "PDF":
              myDisplayDialog(app.selection[0]);
              break;
            }
           }
          }
          function myDisplayDialog(myGraphic){
           //A production version of this script would probably check the link
           //status before proceeding to get the link file.
           var myLink = myGraphic.itemLink;
           var myLinkName = myLink.name;
           var myLinkFile = File(myLink.filePath);
           //A production version of this script would check to see if
           //the link file is present, and whether it's an alias/shortcut
           //rather than a file.
           var myDialog = app.dialogs.add({name:"Rename Graphic"});
           with(myDialog.dialogColumns.add()){
            with(dialogRows.add()){
             with(dialogColumns.add()){
              staticTexts.add({staticLabel:"File name:"});
             }
             with(dialogColumns.add()){
              var myFileNameField = textEditboxes.add({editContents:myLinkName, minWidth:200});
             }
            }
           }
           var myResult = myDialog.show();
           if(myResult == true){
            var myFileName = myFileNameField.editContents;
            myDialog.destroy();
            myRenameGraphic(myLink, myLinkFile, myFileName);
           }
           else{
            myDialog.destroy();
           }
          }
          function myRenameGraphic(myLink, myLinkFile, myFileName){
           //A production version of the script would put up an error if the
           //file cannot be renamed (for whatever reason).
           myLinkFile.rename(myFileName);
           myLink.relink(myLinkFile);
           myLink.update();
          }
          

           

          Hug.

          Leonardo

          • 2. Re: Help with renaming and relinking specific file types
            LeoMari Level 2

            or this:

            var myDoc = app.activeDocument;
            var myAllLinks = myDoc.allGraphics;
            var mySeen = Object;
            var myPrepend = prompt("Example: thebook_08765", "Job description", "Please enter job description");
            var response = confirm("Warning: You are about to rename all images linked to the foremost Indesign Document - proceed? Keep in mind - it is not reversible!");
            
            
            if ( response == true )
            {
              for ( k = 0; k < myAllLinks.length; k++ )
              {
              var myLinkName = myAllLinks[k].itemLink.name;
              if ( mySeen[myLinkName] )
              {
              mySeen[myLinkName]++;
              } else {
              mySeen[myLinkName] = 1;
              }
              }
            
            
              var myPages = myDoc.pages;
            
            
              for ( p = 0; p < myPages.length; p++ )
              {
              var myPageNumber = myPages[p].name;
            
            
              var myLinks = myPages[p].allGraphics;
              var myASCII = 97;
              for ( k = myLinks.length - 1; k >= 0; k-- )
              {
              var myLink = myLinks[k].itemLink;
              var myOldLinkName = myLink.name;
              if ( mySeen[myOldLinkName] == 1 )
              {
              var myExtension = myOldLinkName.substr(myOldLinkName.lastIndexOf( "." ));
              var myNewLinkName = myPrepend + '_p' + myPageNumber + String.fromCharCode( myASCII ) + myExtension;
              var myOldImageHDfile = new File( myLink.filePath );
              myOldImageHDfile.rename( myNewLinkName );
              myLink.relink( myOldImageHDfile );
              try {
              myLink.update();
              }
              catch(err) {}
              myASCII++;
              }
              }
              }
            
            
            
            
            
            
              var myMasterSpreads = myDoc.masterSpreads;
            
              for ( m = 0; m < myMasterSpreads.length; m++ )
              {
              var myMastSpr = myMasterSpreads[m];
              var myPageNumber = myMastSpr.name;
              var myPrefix = myMastSpr.namePrefix;
              var myLinks = myMastSpr.allGraphics;
              var myASCII = 97;
              for ( n = myLinks.length - 1; n >= 0; n-- )
              {
              var myLink = myLinks[n].itemLink;
              var myOldLinkName = myLink.name;
              if ( mySeen[myOldLinkName] == 1 )
              {
              var myExtension = myOldLinkName.substr(myOldLinkName.lastIndexOf( "." ));
              var myLinkLetter = (myLinks.length == 1) ? "" : String.fromCharCode( myASCII );
              var myNewLinkName = myPrepend + '_master_' + myPrefix + myLinkLetter + myExtension;
              var myOldImageHDfile = new File( myLink.filePath );
              myOldImageHDfile.rename( myNewLinkName );
              myLink.relink( myOldImageHDfile );
              try {
              myLink.update();
              }
              catch(err) {}
              myASCII++;
              }
              }
              }
            }
            
            
            function pad000(myNumber) {
              if (myNumber >= 1 && myNumber <= 9) {
              x =  "0" + "0" + myNumber;
              } else if (myNumber >= 10 && myNumber <= 99) {
              x = "0" + myNumber;
              } else if (myNumber >= 100 && myNumber <= 999) {
              x = myNumber;
              }
              return x;
            }
            
            • 3. Re: Help with renaming and relinking specific file types
              chillbot75 Level 1

              thanks LeoMari.

              I tried both with no luck. I couldn't get the first one to run. I would get an error on line 42 on the second.

              • 4. Re: Help with renaming and relinking specific file types
                LeoMari Level 2

                You need to select frame by frame and run the script 1-1..

                Here the two work well, work on the CS6 version

                • 5. Re: Help with renaming and relinking specific file types
                  chillbot75 Level 1

                  Got it. First script works. Thanks!

                  Still not getting the second one to run. I'm on CC 2015.

                  Sorry, I'm not a scripter.

                  I should have been a bit more clear... I need to Batch Rename Relink. The reason for this is because I have so many files to relink. It becomes very time consuming.

                  Is there a way to batch this process?

                  • 6. Re: Help with renaming and relinking specific file types
                    LeoMari Level 2

                    The second option that you spent performs all links at once.It works well in InDesign CS6 version.

                    • 7. Re: Help with renaming and relinking specific file types
                      chillbot75 Level 1

                      Oh, I see... Yes, you're correct. I got this to work. I just need to make some minor adjustments to the script to get the correct naming I'm looking for.

                      Thanks.

                      Would you know if there's a way to select multiple images and batch them together?

                      • 8. Re: Help with renaming and relinking specific file types
                        LeoMari Level 2
                        var doc = app.activeDocument,  
                            links = doc.allGraphics, count = 1, eqcount = 1, a = "0000";  
                        for(var i=links.length-1;i>=0;i--)  
                        {  
                                if(links[i].itemLink.name.match(/^EQ.+/))  
                                {  
                                        var ext = links[i].itemLink.name.substr(links[i].itemLink.name.lastIndexOf(".")),  
                                            old = File(links[i].itemLink.filePath),  
                                            eqnum = (eqcount++).toString(),  
                                            newname = "EQ_EF6_ESP_C1_" + a.substring(0, a.length - eqnum.length) + eqnum + ext;  
                                        old.rename(newname);  
                                        links[i].itemLink.relink(File(old.toString().replace(links[i].itemLink.name,newname)))  
                                    }  
                                else  
                                {  
                                        var ext = links[i].itemLink.name.substr(links[i].itemLink.name.lastIndexOf(".")),  
                                            old = File(links[i].itemLink.filePath),  
                                            num = (count++).toString(),  
                                            newname = "EF6_ESP_C1_" + a.substring(0, a.length - num.length) + num + ext  
                                        old.rename(newname);  
                                        links[i].itemLink.relink(File(old.toString().replace(links[i].itemLink.name,newname)))  
                                    }  
                            }  
                        
                        
                        
                        

                        You have several options

                        • 9. Re: Help with renaming and relinking specific file types
                          LeoMari Level 2
                          var myDoc = app.activeDocument;
                          var myAllLinks = myDoc.allGraphics;
                          var myPrepend = prompt("Example: thebook_08765", "Job description", "Please enter job description");
                          var response = confirm("Warning: You are about to rename all images linked to the foremost Indesign Document - proceed? Keep in mind - it is not reversible!");
                          
                          
                          if ( response == true )
                          {
                              for ( k = 0; k < myAllLinks.length; k++ )
                              {
                                  var myLinkName = myAllLinks[k].itemLink.name;
                              }
                          
                          
                              crearLabels();
                          
                          
                              var myPages = myDoc.pages;
                          
                          
                              for ( p = 0; p < myPages.length; p++ )
                              {
                                  var myPageNumber = pad000(myPages[p].name);
                          
                          
                                  var myLinks = myPages[p].allGraphics;
                                  var myASCII = 97;
                                  for ( k = myLinks.length - 1; k >= 0; k-- )
                                  {
                                      var myLink = myLinks[k].itemLink;
                                      if (myLink.extractLabel("relinked") != "yes") {
                                          var myOldLinkName = myLink.name;
                                          var myLinkUsage = LinkUsage( myLink );
                                          var myExtension = myOldLinkName.substr(myOldLinkName.lastIndexOf( "." ));
                                          if (myLinkUsage == 1)
                                          {
                                              var myNewLinkName = myPrepend + myPageNumber + String.fromCharCode( myASCII ) + myExtension;
                                              var myOldImageHDfile = new File( myLink.filePath );
                                              myOldImageHDfile.rename( myNewLinkName );
                                              myLink.insertLabel("relinked", "yes");
                                              myLink.relink( myOldImageHDfile );
                                              try {
                                                  myLink = myLink.update();
                                              }
                                              catch(err) {}
                                              myASCII++;
                                          }
                                          else
                                          {
                                              processMultiUsedLinks(myLink);
                                          }
                                      }
                                  }
                              }
                          
                          
                              var myMasterSpreads = myDoc.masterSpreads;
                              
                              for ( m = 0; m < myMasterSpreads.length; m++ )
                              {
                                  var myMastSpr = myMasterSpreads[m];
                                  var myPageNumber = myMastSpr.name;
                                  var myPrefix = myMastSpr.namePrefix;
                                  var myLinks = myMastSpr.allGraphics;
                                  var myASCII = 97;
                                  for ( n = myLinks.length - 1; n >= 0; n-- )
                                  {
                                      var myLink = myLinks[n].itemLink;
                                      if (myLink.extractLabel("relinked") != "yes") {
                                          var myOldLinkName = myLink.name;
                                          var myExtension = myOldLinkName.substr(myOldLinkName.lastIndexOf( "." ));
                                          var myLinkLetter = (myLinks.length == 1) ? "" : String.fromCharCode( myASCII );
                                          var myNewLinkName = myPrepend + '_master_' + myPrefix + myLinkLetter + myExtension;
                                          var myOldImageHDfile = new File( myLink.filePath );
                                          myOldImageHDfile.rename( myNewLinkName );
                                          myLink.insertLabel("relinked", "yes");
                                          myLink.relink( myOldImageHDfile );
                                          try {
                                              myLink.update();
                                          }
                                          catch(err) {}
                                          myASCII++;
                                      }
                                  }
                              }
                          }
                          
                          
                          //------------------------- Functions -------------------------------------
                          // Check how many times the link was placed
                          function LinkUsage(myLink) {
                              var myLinkCounter = 0;
                                  for (var c =  0; c < myDoc.links.length; c++) {
                                  if (myLink.filePath == myDoc.links[c].filePath) {
                                      myLinkCounter += 1;
                                  }
                              }
                              return myLinkCounter
                          }
                          // Relink the links placed more than once
                          function processMultiUsedLinks(myLink) {
                              var myMultiUsedLink = new Array();
                              var myPagesArr = new Array();
                              var myAllLinks = myDoc.links;
                              for (var d = 0; d < myAllLinks.length; d++)  {
                                  if (myAllLinks[d].filePath == myLink.filePath) {
                                      myMultiUsedLink.push(myAllLinks[d]);
                                  }
                              }
                              for (e = myMultiUsedLink.length-1; e >= 0 ; e--) {
                                  myPagesArr.push(myMultiUsedLink[e].parent.parent.parent.name);
                              }
                              var myPageNames = myPagesArr.sort().join("_");
                              var myNewLinkName = myPrepend + '_p' + myPageNames + myExtension;
                              var myOldImageHDfile = new File( myLink.filePath );
                              myOldImageHDfile.rename( myNewLinkName );
                              myLink.insertLabel("relinked", "yes");
                              myLink.relink( myOldImageHDfile );
                              try {
                                  myLink = myLink.update();
                              }
                              catch(err) {}
                          
                          
                              for (f = myMultiUsedLink.length-2; f >= 0 ; f--)
                              {
                                  var myCurrLink = myMultiUsedLink[f];
                                  myCurrLink.insertLabel("relinked", "yes");
                                  myCurrLink.relink( myOldImageHDfile );
                          
                          
                                  try {
                                      myCurrLink = myLink.update();
                                  }
                                  catch(err) {}
                              }
                              UpdateAllOutdatedLinks();
                          }
                          // Clear labels in case this script has been already run on the current document
                          function crearLabels() {
                              for (var f =  0; f < myDoc.links.length; f++) {
                                  if (myDoc.links[f].extractLabel("relinked") != "") {
                                      myDoc.links[f].insertLabel("relinked", "");
                                  }
                              }
                          }
                          //---------------------------------------------------------------------------------------- ----------------------
                          function UpdateAllOutdatedLinks() {
                              for(var myCounter = myDoc.links.length-1; myCounter >= 0; myCounter--){
                                  var myLink = myDoc.links[myCounter];
                                  if (myLink.status == LinkStatus.linkOutOfDate){
                                      myLink.update();
                                  }
                              }
                          }
                          //---------------------------------------------------------------------------------------- ----------------------
                          function pad000(myNumber) {
                              if (myNumber >= 1 && myNumber <= 9) {
                                  x =  "0" + "0" + myNumber;
                              } else if (myNumber >= 10 && myNumber <= 99) {
                                  x = "0" + myNumber;
                              } else if (myNumber >= 100 && myNumber <= 999) {
                                  x = myNumber;
                              }
                              return x;
                          }
                          
                          • 10. Re: Help with renaming and relinking specific file types
                            LeoMari Level 2

                            I think only now understand your problem, you need to rename by extension ... This looks a bit more complicated .. I will try and find post here .. sorry :.S

                            • 11. Re: Help with renaming and relinking specific file types
                              chillbot75 Level 1

                              Yes, I would appreciate that very much! =]