16 Replies Latest reply on Oct 29, 2012 5:26 AM by anightingale

    Replace images on specific layer

    anightingale Level 1

      Hi,

      till now i used the Script-function in InDesign only for "findandchange"-Actions. It works fine and is very time-saving.

      Now I would like to replace images on specific Layers, like I do this with textcontent with the "Findandchange"-Script.

      Is this possible?

       

      I try to explain...
      Example:

      - Find all images "image-a1.tif"(path:folder/folder/image-a1.tif) on specific layer "mylayer"(or only activ and not locked layers)

       

      - Replace all this images with this image "image-a2"tif(path:folder/folder/image-a2.tif)

       

      - Find all images "image-b1.tif"(path:folder/folder/image-b1.tif) on specific layer "mylayer"(or only activ and not locked layers)

       

      - Replace all this images with this image "image-b2"tif(path:folder/folder/image-b2.tif)

       

      - Find all images "image-c1.tif"(path:folder/folder/image-c1.tif) on specific layer "mylayer"(or only activ and not locked layers)

       

      - Replace all this images with this image "image-c2"tif(path:folder/folder/image-c2.tif)
      ...


      Background Info:
      These images have all the same size and resolution. They distinguish only in content (english-/france/german elements).
      It's a "multilanguage" InDesign-Document. Every "Languagecontent" have its own layer.

       

      Thanks for helping...

        • 1. Re: Replace images on specific layer
          Jump_Over Level 5

          Hi,

           

          That's possible.

           

          To catch images on specific, let say "graphic", doc layer use:

           

          myDoc = app.activeDocument;

          myGraph = myDoc.layers.item("graphic").allGraphics;

           

          //myGraph is an array, so you can open a loop and relink each item step by step:

          //to relink images you can use:

           

          for (k=0; k<myGraph.length; k++)

          {

               oldLink = myGraph[k].itemLink.filePath;     //whole path

               oldFileName = File(oldLink).name;              //only file name

               newFileName = oldFileName.replace("1","2");     // replace "1" with "2" (or another way you choose)

               newLink = File(oldLink.replace(oldFileName,newFileName) );    // replace file names within path

               myGraph[k].itemLink.relink(newLink);     //relink graphic

          }

           

           

          hope...

          • 2. Re: Replace images on specific layer
            anightingale Level 1

            Sorry, but i think i'am more noob as you think

             

            Can you tell me exactly wich elements in the code i must replace?

             

            My old filename: "image_de.eps" (Link: M:\Folder\Folder\image_de.eps)

            My new filename: "image_en.eps" (Link: M:\Folder\Folder\image_en.eps)

             

            My layer: "english"

             

            All images ("image_de.eps") must replace with "image_en.eps" (Link: M:\Folder\Folder\image_en.eps).

             

            I think i have used your code wrong and replace not the right parts.

             

            Many thanks

            • 3. Re: Replace images on specific layer
              anightingale Level 1

              I think my first post was awkwardly formulated.

               

              I don't have  "string"-like imagenames (image-a1, image-a2, image-a3, ...).

              I have just images wich must replace with others in a specific layer: "auto_de.eps" -> "car_en.eps" for example.

              • 4. Re: Replace images on specific layer
                Jump_Over Level 5

                Hi,

                 

                The greatest reason to use a script is to automate some operations.

                If each of your find_replace pair of names of images need to be edited separatelly - so it is hard to define any condition to this - no matter if you would relink them manually, I guess.

                How many images are we talking about?

                • 5. Re: Replace images on specific layer
                  anightingale Level 1

                  I have a Document with 100 pages. Each page have ca. 10-15 images (logos(eps and pdf files) with german text).

                  My idea is to dublicate the layer and use the new layer for the english "version" and replace the images with other (logos(eps and pdf files) with english text).

                   

                  The most images are 30 times linked in one document.

                   

                  The same for a "french version"...

                   

                  One document have ca. 1.000 images. And I have 10 documents of this kind.

                  • 6. Re: Replace images on specific layer
                    anightingale Level 1

                    For the simple Text replacement I found a solution with this script:

                     

                    app.findGrepPreferences = NothingEnum.nothing;

                    app.findGrepPreferences.findWhat= "germantext";

                    app.findGrepPreferences.appliedCharacterStyle = "Bingobongo";

                    app.changeGrepPreferences = NothingEnum.nothing;

                    app.changeGrepPreferences.changeTo= "englishtext";

                    app.changeGrepPreferences.appliedCharacterStyle = "Bingobongo";

                    app.changeGrepPreferences.appliedParagraphStyle = "Bongobingo";

                    app.activeDocument.changeGrep();

                     

                    app.changeGrepPreferences = NothingEnum.nothing;

                     

                    Now It would be nice when I can do the same with linked images. It would be the best when I can do this in the same script doc(at the end).

                     

                    Backgroundinfo:

                    The documents are datasheets in different language versions,with text in note form, pictures and logos.

                    So it should be a "translation"-Script which replace "german" text whith "english" text and "german" linked images with "english" images.

                     


                    • 7. Re: Replace images on specific layer
                      Jump_Over Level 5

                      Hi,

                       

                      Whole this image relink operation could be managed by script pretty well and the only thing you need is to answer one question:

                      How to explain to the script which filePath should be the new one?

                       

                      If you  make it manually - what will be the way you detect this?

                      Just because you read a file name or is there any rule about this?

                      • 8. Re: Replace images on specific layer
                        anightingale Level 1

                        Hi,

                         

                        I do not know whether I understood your question correctly.

                         

                        There is no special rule to replace an image.

                         

                        Every image as long as it is on the same layer and has the same name, must be replaced with another image with another link and another filename.


                        If I do it manually, I'm doing the following (example with the picture named "logo_de.eps"):

                         

                        - I open the window "Links" (sort by layers)

                        - select all links with the name logo_de.eps wich are used on the layer named "english"

                        - relink this Links with the new image "logo_en.eps" wich is located in another folder

                         

                        So every Image(with the same name on the same layer) I must replace have only one possible "Replacement"-Link

                        • 9. Re: Replace images on specific layer
                          Jump_Over Level 5

                          Hi,

                           

                          If a chalange is to replace "someName_en.eps" with "someName_de.eps" we've got a rule:

                          replacing "_en" with "_de" give us a valid path which script can use to relink.

                           

                          But if a chalenge is to replace "car_en.eps" with "auto_de.eps" - how script could detect this?

                          • 10. Re: Replace images on specific layer
                            anightingale Level 1

                            The linked files d'ont have a special rule: Like replacing "car_en.eps" with "car_de.eps"

                             

                            C'ant I give the script the explicit command like

                             

                            -> all linked files named "car.eps" replace with "auto.eps" located "C:\folder\folder"?

                            -> all linked files named "bongo.eps" replace with "bingo.eps" located "C:\folder\folder"?

                             

                            Andy

                            • 11. Re: Replace images on specific layer
                              anightingale Level 1

                              I know for one document I not save much more time with a script like this.

                              But I have more documents like this... much more...

                              • 12. Re: Replace images on specific layer
                                Jump_Over Level 5

                                Hi,

                                 

                                Yes you can. I.e using dialog box.

                                It could be like this:

                                 

                                ____________________________

                                myDoc = app.activeDocument;

                                myGraph = myDoc.layers.item("graphic").allGraphics;     // modify layer name

                                 

                                for (k=0; k<myGraph.length; k++)

                                {

                                     oldLink = myGraph[k].itemLink.filePath;     //whole path

                                     oldFileName = File(oldLink).name;              //only file name

                                    newFileName = myDialog(oldFileName);     // new name is read from dialog

                                     if ( !newFileName ) continue;                         // if user press "cancel" button - no action

                                     newLink = File(oldLink.replace(oldFileName,newFileName) );    // replace file names within path

                                     if (File(newLink) instanceof File)                     // if wrong newLink - no action

                                        myGraph[k].itemLink.relink(newLink);     //relink graphic

                                }

                                 

                                function myDialog(oldName)

                                {

                                    var myWindow = new Window ("dialog", "Replacing images");

                                    var myInputGroup = myWindow.add ("group");

                                    myInputGroup.orientation = "column";

                                    myInputGroup.alignChildren = "left";

                                    myInputGroup.add ("statictext", undefined, "Old Name: " + oldName);

                                    myInputGroup.add ("statictext", undefined, "New Name:");

                                    var myText = myInputGroup.add ("edittext", undefined, ".eps");

                                    myText.characters = 20;

                                    myText.active = true;

                                    var myButtonGroup = myWindow.add ("group");

                                    myButtonGroup.alignment = "right";

                                    myButtonGroup.add ("button", undefined, "OK");

                                    myButtonGroup.add ("button", undefined, "Cancel");

                                    var OKbutton = myWindow.show ();

                                        if (OKbutton == 1) myRes = myText.text;

                                        else myRes = false;

                                    return myRes;

                                }

                                ________________________

                                • 13. Re: Replace images on specific layer
                                  anightingale Level 1

                                  Hi,

                                   

                                  with your first reply you had a good nose. I was on the wrong path. I rename now the linked files in = "car_en" & "car_de"

                                  It is the best long-term solution. So I can use your first script, wich works great.

                                   

                                  A big thank you for your help.

                                  • 14. Re: Replace images on specific layer
                                    anightingale Level 1

                                    Ok, i have only one question. My linked images for the replacement have a different paths.

                                    So I modify your script a bit(in italic). But it doesen't work ... as I expected

                                    InDesign give me an error for the following line:  " myGraph[k].itemLink.relink(newLink); ..."

                                    Can you see my mistake?

                                     

                                    myDoc = app.activeDocument;

                                    myGraph = myDoc.layers.item("englisch").allGraphics;

                                     

                                    oldFolder = "M:/Logos/deutsch/";

                                    newFolder = "M:/Logos/englisch/";

                                     

                                    for (k=0; k<myGraph.length; k++)

                                    {

                                         oldLink = myGraph[k].itemLink.filePath;

                                         oldFileName = File(oldLink).name;

                                         newFileName = oldFileName.replace("_de","_en");

                                         newLink = File(oldLink.replace(oldFileName,newFileName)oldFolder.replace (oldFolder,newFolder) );

                                         myGraph[k].itemLink.relink(newLink);

                                    }

                                    • 15. Re: Replace images on specific layer
                                      Jump_Over Level 5

                                      hi,

                                       

                                      all you need is to make replace twice:

                                      .....

                                      newLink = oldLink.replace(oldFileName, newFileName);

                                      newLink = newLink.replace("deutsch", "english");
                                      newLink  = File(newLink);
                                      myGraph[k].itemLink.relink(newLink);

                                      .....

                                      • 16. Re: Replace images on specific layer
                                        anightingale Level 1

                                        Works great.

                                         

                                        Thank you