23 Replies Latest reply: Sep 14, 2013 8:09 AM by henras1000 Branched to a new discussion. RSS

    Rename and relink images - Indesign CS4 - OSX10.5

      Hi all

      I am looking for a script which will relink images that have changed part of their file name. OR a script which will rename AND relink if this is easier. At the moment I am using Automator to batch rename.

      For instance, I have the following files:
      20283 GN6E451.tif
      20283 56529823.eps
      20283 571589.eps

      I want to change them so they are:
      21483 GN6E451.tif
      21483 56529823.eps
      21483 571589.eps

      So only the first part of the file names change (this is a job number). And then be able to tell InDesign that these are the same files. Is there a script available to do this?

      Many thanks in advance!
        • 1. Re: Rename and relink images - Indesign CS4 - OSX10.5
          Kasyan Servetsky Community Member


          var myDoc = app.activeDocument;
          var myLinks = myDoc.links;

          for (i = myLinks.length-1; i >= 0 ; i--) {
             var myLink = myLinks[i];
             var myFile = new File(myLink.filePath);
             var myNewName = myLink.name.replace("20283", "21483");
             if (myFile.exists) {
                myFile.rename(myNewName);
                myLink.relink(myFile);
                myLink.update();
             }
          }
          • 2. Re: Rename and relink images - Indesign CS4 - OSX10.5
            Community Member
            Thanks Kasyan, though I should have mentioned the 5 digit job number was an example only...both numbers would constantly be changing. I need a script where I could type the numbers into the script panel in InDesign itself...not the raw script. Is this possible?
            • 3. Re: Rename and relink images - Indesign CS4 - OSX10.5
              Kasyan Servetsky Community Member
              Yes, I'm writing a new version.
              • 4. Re: Rename and relink images - Indesign CS4 - OSX10.5
                Kasyan Servetsky Community Member


                #target indesign
                if (app.documents.length == 0) {
                   err("No documents are open. Please open a document and try again.");
                }
                if (app.activeDocument.links.length == 0) {
                   err("The current document contains no images. Please open a document containing images and try again.");
                }
                var myFind, myChange, myResult;
                var myDoc = app.activeDocument;
                var myLinks = myDoc.links;


                var myDialog = app.dialogs.add({name:"Change job number", canCancel:true});
                var myColumn = myDialog.dialogColumns.add();
                var myPanel = myColumn.borderPanels.add();
                var myLabel1 = myPanel.staticTexts.add({staticLabel:"Change "});
                var myText1 = myPanel.textEditboxes.add();
                if (app.extractLabel("kas_myText1") != "") {
                   myText1.editContents = app.extractLabel("kas_myText1");
                }
                var myLabel2 = myPanel.staticTexts.add({staticLabel:" to "});
                var myText2 = myPanel.textEditboxes.add();
                if (app.extractLabel("kas_myText2") != "") {
                   myText2.editContents = app.extractLabel("kas_myText2");
                }

                if(myDialog.show() == true){
                   var myFind = myText1.editContents;
                   var myChange = myText2.editContents;

                   app.insertLabel("kas_myText1", myFind);
                   app.insertLabel("kas_myText2", myChange);

                   for (i = myLinks.length-1; i >= 0 ; i--) {
                      var myLink = myLinks[i];
                      var myFile = new File(myLink.filePath);
                      var myNewName = myLink.name.replace(myFind, myChange);
                      if (myNewName !=  myLink.name) {
                         if (myFile.exists) {
                            myFile.rename(myNewName);
                            myLink.relink(myFile);
                            myLink.update();
                         }
                      }
                   }
                   myDialog.destroy();
                   alert("Done!");
                }
                else{
                   myDialog.destroy();
                }
                function err(e) {
                   alert(e);
                   exit();
                }
                • 5. Re: Rename and relink images - Indesign CS4 - OSX10.5
                  Kasyan Servetsky Community Member
                  P.S. I haven't tested this, so use carefully.
                  • 6. Re: Rename and relink images - Indesign CS4 - OSX10.5
                    Community Member
                    No luck with this one, I get the following error:

                    Error Number -2741
                    Expected expression property or key form, etc. but found unknown token.
                    • 7. Re: Rename and relink images - Indesign CS4 - OSX10.5
                      Kasyan Servetsky Community Member
                      I tested the script in CS3 on Mac and PC and CS4 on Mac, using a simple document, which contains 3 links, named as in your 1st post. And it works for me.
                      Possible solutions:
                      *Send me a screenshot of the error message or run the script in ESTK and see which line it stops and error message it generates.
                      *Send me an archived copy of your document to recreate the error.
                      • 8. Re: Rename and relink images - Indesign CS4 - OSX10.5
                        Community Member
                        Sorry my scripting knowledge is a little limited, I had originally saved it as an applescript file. I've now saved through ESTK as a jsx file and it works perfectly! Thank you so much!

                        On a side thought which I hadn't even considered before, is it at all possible to first copy the files from the original job folder into the new job folder, and then rename and relink? So if I ran the script and it prompted me to select a new destination folder to copy to, and then prompted for the rename info?

                        I could simply copy and paste manually though it is often the case that the new job doesn't always use all of the images from the old job.

                        thanks! :D
                        • 9. Re: Rename and relink images - Indesign CS4 - OSX10.5
                          Kasyan Servetsky Community Member


                          #target indesign
                          if (app.documents.length == 0) {
                             err("No documents are open. Please open a document and try again.");
                          }
                          if (app.activeDocument.links.length == 0) {
                             err("The current document contains no images. Please open a document containing images and try again.");
                          }
                          var myFind, myChange, myResult;
                          var myFolder = Folder.selectDialog ("Choose a new job folder");
                          if (myFolder != null) {
                             var myDoc = app.activeDocument;
                             var myLinks = myDoc.links;

                             var myDialog = app.dialogs.add({name:"Change job number", canCancel:true});
                             var myColumn = myDialog.dialogColumns.add();
                             var myPanel = myColumn.borderPanels.add();
                             var myLabel1 = myPanel.staticTexts.add({staticLabel:"Change "});
                             var myText1 = myPanel.textEditboxes.add();
                             if (app.extractLabel("kas_myText1") != "") {
                                myText1.editContents = app.extractLabel("kas_myText1");
                             }
                             var myLabel2 = myPanel.staticTexts.add({staticLabel:" to "});
                             var myText2 = myPanel.textEditboxes.add();
                             if (app.extractLabel("kas_myText2") != "") {
                                myText2.editContents = app.extractLabel("kas_myText2");
                             }

                             if(myDialog.show() == true){
                                var myFind = myText1.editContents;
                                var myChange = myText2.editContents;

                                app.insertLabel("kas_myText1", myFind);
                                app.insertLabel("kas_myText2", myChange);

                                for (i = myLinks.length-1; i >= 0 ; i--) {
                                   var myLink = myLinks[i];
                                   var myFile = new File(myLink.filePath);
                                   var myNewName = myLink.name.replace(myFind, myChange);
                                   var myNewFile = new File(myFolder + "/" + myNewName);
                                   if (myFile!= myNewFile) {
                                      if (myFile.exists) {
                                         myFile.copy(myNewFile);
                                         myLink.relink(myNewFile);
                                         myLink.update();
                                      }
                                   }
                                }
                                myDialog.destroy();
                                alert("Done!");
                             }
                             else{
                                myDialog.destroy();
                             }
                          }
                          function err(e) {
                             alert(e);
                             exit();
                          }
                          • 10. Re: Rename and relink images - Indesign CS4 - OSX10.5
                            Community Member
                            PERFECT! Thank you so much for your help I really appreciate it!! :D
                            • 11. Re: Rename and relink images - Indesign CS4 - OSX10.5
                              amy_emcdesign Community Member

                              Hi can anyone explain how to install and run this script, I tried to save it using extend script and then placing it in the suitable folder but it wont run form the scripts panel?

                               

                              Thanks in advance

                              Amy

                              • 12. Re: Rename and relink images - Indesign CS4 - OSX10.5
                                John Hawkinson Community Member

                                Did you name it with the .jsx extension? Do you see it in the Scripts panel? What happens when you double-click it?    

                                • 13. Re: Rename and relink images - Indesign CS4 - OSX10.5
                                  korinsama Community Member

                                  wow, that script to rename and relink is just the best thing that happened to me for my work

                                  thanks you sooooo much, it saved my office hours of relinking pain

                                  • 14. Re: Rename and relink images - Indesign CS4 - OSX10.5
                                    MKPETZ Community Member

                                    I agree, this is a very awesome script!

                                     

                                    I use a slight variation of it. We have up to a couple hundred images on an indesign document. All the image files end in AA (" AA.tif" or " AA.eps"). Then we use this script to relink to the white plate file (all ending with " WT.tif")  for each image. Some of the images do not contain white. The script works great, all the images that do not have white just leave the original front image in palce (the one that contains AA) Is there an easy way to script the removal of all the AA links? I don't want the file deleted or anything, i just need the link removed from the indesign document, so an empty box should remain where the AA file was. Right now we just manually delete these images, which is sometimes time consuming. I've looked around for ways to remove/unlink files but can't grasp how to make anything work.

                                     

                                    //DESCRIPTION: Replace files ending in AA.tif or AA.eps that are linked images with their matching file ending with wt.tif alternates
                                    
                                     /*
                                       Front images have " AA" in their names; White plates have " WT.tif"
                                     */
                                     
                                    myDoc = app.activeDocument;
                                    myLinks = myDoc.links;
                                    myFrontLayerName = "Front Colorbar";
                                    myWhiteLayerName = "White Blackbar";
                                    myBoxesLayerName = "boxes";
                                    
                                    app.activeDocument.layers.item(myFrontLayerName).visible = false;
                                    app.activeDocument.layers.item(myWhiteLayerName).visible = true;
                                    app.activeDocument.layers.item(myBoxesLayerName).visible = false;
                                    
                                    for (j = myLinks.length - 1; j >= 0; j--) {
                                      myName = myLinks[j].filePath;
                                      myNewName = myName.split(" AA.eps").join(" wt.tif");
                                      if (myName != myNewName) {
                                        // Original link includes " AA.eps" in name
                                        myNewImageFile = File(myNewName);
                                        if (!myNewImageFile.exists) {
                                         
                                          continue;
                                        }
                                        myLinks[j].relink(myNewImageFile);
                                        myLinks[j].update();
                                      }
                                    }
                                    
                                    for (j = myLinks.length - 1; j >= 0; j--) {
                                      myName = myLinks[j].filePath;
                                      myNewName = myName.split(" AA.tif").join(" wt.tif");
                                      if (myName != myNewName) {
                                        // Original link includes " AA.tif" in name
                                        myNewImageFile = File(myNewName);
                                        if (!myNewImageFile.exists) {
                                         
                                          continue;
                                    
                                    
                                    }
                                        myLinks[j].relink(myNewImageFile);
                                        myLinks[j].update();
                                      }
                                    }
                                    
                                    • 15. Re: Rename and relink images - Indesign CS4 - OSX10.5
                                      LouisHectorMonty2

                                      Just tried the script for renumbering files but I could not get it to work for our situation. We have files where we have to ad a prefix to all the image files, for example the file maybe called graphA.eps and we have to rename it ES_graphA.eps (ES refers to the language, Spanish in this case but it could be any language) and still remain linked. I have just had to do this over 200 times on the last job and we have 3 more. Can the Kasyans script be altered to insert a prefix as opposed to replacing one?

                                      • 16. Re: Rename and relink images - Indesign CS4 - OSX10.5
                                        MKPETZ Community Member

                                        I don't know javascript at all! Try replying directly to one of the posts by Kasyan because they are a genius. In the meantime would something like this work?

                                         

                                        Kasyans script uses this for replacing the old number in the filename to the new number:

                                           var myNewName = myLink.name.replace("20283", "21483");

                                         

                                        can you do something like this?

                                         

                                           var myNewName = myLink.name.replace("graph", "ES_graph");

                                         

                                        I have no idea if the number or characters has to match??

                                        • 17. Re: Rename and relink images - Indesign CS4 - OSX10.5
                                          [Jongware] MVP

                                          MKPETZ wrote:

                                           

                                          can you do something like this?

                                           

                                             var myNewName = myLink.name.replace("graph", "ES_graph");

                                           

                                          I have no idea if the number or characters has to match??

                                           

                                          No, it does not. This is a simple search-and-replace -- the first string is found in the variable on which the 'replace' command is performed (in this case, myLink.name) and only when it's found it is replaced by the second string. (That *does* logically mean that if your first string "graph" does not occur in the name, this command will do *nothing*. But I think it's safe to 'relink' an image with itself again.)

                                          • 18. Re: Rename and relink images - Indesign CS4 - OSX10.5
                                            LouisHectorMonty2 Community Member

                                            Thanks for the response to this MKPETZ and Jongware

                                             

                                            Can this script be altered for this to work. Just to add a language prefix to all linked files.

                                             

                                            Or can it be done another way. So if we start with all linked files which have been translated and then a run a script to add the language prefix and relink at the same time.

                                            • 19. Re: Rename and relink images - Indesign CS4 - OSX10.5
                                              [Jongware] MVP

                                              LouisHectorMonty2 wrote:

                                               

                                              Can this script be altered for this to work. Just to add a language prefix to all linked files.

                                               

                                              That would be as easy as this:

                                               

                                              var myNewName = "ES_"+myLink.name;

                                               

                                              -- where "ES_" is your language prefix. Just to make sure, this line should be changed in Kasyan's very first script in post #1.

                                              • 20. Re: Rename and relink images - Indesign CS4 - OSX10.5
                                                LouisHectorMonty2 Community Member

                                                Hi Jongware

                                                 

                                                Thanks that worked great, infact too great! I just ran it and realised I had changed all the files which had been translated and of course all the graphics that hadn't such as photgraphs etc. Is there a line that could be put in that would only change those with a mod date of today?

                                                • 21. Re: Rename and relink images - Indesign CS4 - OSX10.5
                                                  [Jongware] MVP

                                                  LouisHectorMonty2 wrote:

                                                   

                                                  Is there a line that could be put in that would only change those with a mod date of today?

                                                   

                                                  I think that's a bit too specific ...

                                                   

                                                  So you have files with this prefix "ES_" that you did not translate (yet)? Hmmm ... easiest way out would be to delete, move, or rename them ... That makes sense as well, because if that prefix means you translated the text, these files shouldn't have one.

                                                  • 22. Re: Rename and relink images - Indesign CS4 - OSX10.5
                                                    henras1000

                                                    Hi Kasyan,

                                                     

                                                    I have been trying to adjust your script for my specific need, but can't get it to work . I have an InDesign document with approx. 500 images linked. They all have the prefix "DK_" in the filename for the language. For translation of the document to 3 other languages, I have saved my document in 3 new names and I already have the 500 image files with the prefixes "FI_", "NO_" and "SE_".

                                                     

                                                    So I want to relink all images in the SE-document with images with the same name and instead of "DK_" use the prefix "SE_" - but not rename anything, since the images with different prefixes already exists and are different.

                                                     

                                                    I tried using your script without the line "myFile.rename(myNewName)" to stop it from renaming the "DK_" files which I still need for my DK-document. Any ideas would be appreciated!

                                                    • 23. Re: Rename and relink images - Indesign CS4 - OSX10.5
                                                      henras1000 Community Member

                                                      It seems I actually solved the problem by adjusting another script found here:

                                                      http://jsid.blogspot.dk/2006/03/replacing-links.html

                                                       

                                                       

                                                      //Description: Replace images with prefix to another language

                                                      /*

                                                         Existing images have the prefix "DK"; New have the prefix "SE"

                                                      */

                                                       

                                                      myDoc = app.activeDocument;

                                                      myLinks = myDoc.links;

                                                      for (j = myLinks.length - 1; j >= 0; j--) {

                                                        myName = myLinks[j].filePath;

                                                        myNewName = myName.split("DK").join("SE");

                                                        if (myName != myNewName) {

                                                          // Originale images have the prefix "DK"

                                                          myNewImageFile = File(myNewName);

                                                          if (!myNewImageFile.exists) {

                                                           alert ("Can't find file: " + myNewName);

                                                            continue;

                                                          }

                                                          myLinks[j].relink(myNewImageFile);

                                                          myLinks[j].update();

                                                        }

                                                      }

                                                       

                                                      Hope others can find use of it.