Skip navigation
Currently Being Moderated

Rename and relink images - Indesign CS4 - OSX10.5

Jan 28, 2009 5:44 AM

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!
Branched to a new discussion.
 
Replies
  • Currently Being Moderated
    Jan 28, 2009 7:20 AM   in reply to (carly7)


    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();
       }
    }
     
    |
    Mark as:
  • Currently Being Moderated
    Jan 28, 2009 8:34 AM   in reply to (carly7)
    Yes, I'm writing a new version.
     
    |
    Mark as:
  • Currently Being Moderated
    Jan 28, 2009 8:54 AM   in reply to (carly7)


    #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();
    }
     
    |
    Mark as:
  • Currently Being Moderated
    Jan 28, 2009 8:58 AM   in reply to (carly7)
    P.S. I haven't tested this, so use carefully.
     
    |
    Mark as:
  • Currently Being Moderated
    Jan 29, 2009 2:52 AM   in reply to (carly7)
    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.
     
    |
    Mark as:
  • Currently Being Moderated
    Jan 29, 2009 5:52 AM   in reply to (carly7)


    #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();
    }
     
    |
    Mark as:
  • Currently Being Moderated
    Mar 2, 2010 7:52 AM   in reply to (carly7)

    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

     
    |
    Mark as:
  • John Hawkinson
    5,572 posts
    Jun 25, 2009
    Currently Being Moderated
    Mar 2, 2010 9:02 AM   in reply to amy_emcdesign

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

     
    |
    Mark as:
  • Currently Being Moderated
    Jan 10, 2011 9:04 AM   in reply to Kasyan Servetsky

    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

     
    |
    Mark as:
  • Currently Being Moderated
    Jan 11, 2011 11:52 AM   in reply to Kasyan Servetsky

    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();
      }
    }
    
     
    |
    Mark as:
  • Currently Being Moderated
    Jun 1, 2012 9:22 AM   in reply to MKPETZ

    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?

     
    |
    Mark as:
  • Currently Being Moderated
    Jun 7, 2012 5:47 PM   in reply to LouisHectorMonty2

    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??

     
    |
    Mark as:
  • Currently Being Moderated
    Jun 8, 2012 1:35 AM   in reply to MKPETZ

    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.)

     
    |
    Mark as:
  • Currently Being Moderated
    Jun 8, 2012 2:02 AM   in reply to [Jongware]

    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.

     
    |
    Mark as:
  • Currently Being Moderated
    Jun 8, 2012 2:05 AM   in reply to LouisHectorMonty2

    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.

     
    |
    Mark as:
  • Currently Being Moderated
    Jun 8, 2012 2:39 AM   in reply to [Jongware]

    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?

     
    |
    Mark as:
  • Currently Being Moderated
    Jun 8, 2012 5:02 AM   in reply to LouisHectorMonty2

    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.

     
    |
    Mark as:
  • Currently Being Moderated
    Sep 14, 2013 6:56 AM   in reply to (carly7)

    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!

     
    |
    Mark as:
  • Currently Being Moderated
    Sep 14, 2013 8:09 AM   in reply to henras1000

    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.

     
    |
    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)