9 Replies Latest reply on Nov 7, 2008 3:44 PM by (3poulakia)

    Find text and replace with image - Help needed

      Hi,<br /><br />We need to place the images as inline in the appropriate places.<br /><br />texttexttext<<test1.eps>>texttexttexttexttexttext<<test2.eps>>texttexttexttexttexttext< <test3.eps>>texttexttexttexttexttext<<test4.eps>>texttexttexttexttexttext<<test5.eps>>text texttext<br /><br />This code is helpful for placing a single image at a time, we are unable to place all the images in one shot, can anyone help me out.<br /><br />I am not a programmer.<br /><br />-----------<br />var myDoc = app.activeDocument; <br />app.findPreferences = app.changePreferences = null; <br />var math=document.search("test.eps"); <br />for (i=math.length-1; i >=0; i--)<br />{ myDir = Folder.selectDialog(); <br />AllGraphics = Folder(myDir).getFiles('test.eps') <br />for (i=0; i<math.length; i++) { app.select(anil1[i]); <br />     myDoc.place(AllGraphics[i],false); } }<br />-----------<br /><br />Note: I have taken this code from forum and we have made some changes on this.<br /><br />Kavya
        • 1. Re: Find text and replace with image - Help needed
          Level 1
          Any help on this..............

          Kavya
          • 2. Re: Find text and replace with image - Help needed
            [Jongware] Most Valuable Participant
            I see you are using old style 'search' -- that means you are using CS, right? You don't mention this, but it is important.

            >This code is helpful for placing a single image at a time, we are unable to place all the images in one shot [..]

            You are searching for the text 'test.eps' and then replacing it with the image called 'test.eps'. That's the one image you get, on the one place you find.

            Make an array of all possible filenames and loop over these, searching and placing one name/file at a time. I'd recommend searching for "<<"+filename+">>", because it seems those are the exact markings in the text.
            • 3. Re: Find text and replace with image - Help needed
              Level 1
              Thanks for the response,

              I am using Indesign CS3, this code is working for me.

              I am having problem in fixing the Array's to find the exact thing.

              Please help.

              kavya
              • 4. Re: Find text and replace with image - Help needed
                [Jongware] Most Valuable Participant
                Yeah, that code works, but for one file at the time. There are no arrays in your sample code.<br /><br /><pre>var heyItsAnArray = new Array (<br /><br />  "it contains this line!",<br /><br />  "as well as this one",<br /><br />  "or even more!",<br /><br />  "test.eps"<br /><br />);</pre><br /><br />So all you have to do is re-write your previous code as a function:<br /><br /><pre>function replaceImg (name)<br /><br />{<br /><br />var myDoc = app.activeDocument; <br /><br />app.findPreferences = app.changePreferences = null; <br /><br />var math=document.search(name);<br /><br />for (i=math.length-1; i >=0; i--) <br /><br />{ myDir = Folder.selectDialog(); <br /><br />AllGraphics = Folder(myDir).getFiles(name) <br /><br />for (i=0; i<math.length; i++) { app.select(anil1[i]); myDoc.place(AllGraphics[i],false); } }<br /><br />}</pre><br /><br />and do this:<br /><br /><pre>for (arrayCount=0; arrayCount<heyItsAnArray.length; arrayCount++)<br /><br />{<br /><br />  replaceImg (heyItsAnArray[arrayCount]);<br /><br />}</pre>But I'm wondering how your code could ever work, as I see no declaration of 'anil1'. And using 'math' as a variable name is harmless, but unnecessary and potentially confusing.
                • 5. Re: Find text and replace with image - Help needed
                  Level 1
                  Hi Jongware,

                  could you please post the complete script so I can copy past it.
                  I wouldn't know how to handle this as it is in parts.

                  Thank you
                  3poulakia
                  • 6. Re: Find text and replace with image - Help needed
                    [Jongware] Most Valuable Participant
                    <pre>var heyItsAnArray = new Array (<br /><br />   "it contains this line!",<br /><br />   "as well as this one",<br /><br />   "or even more!",<br /><br />   "test.eps" );<br /><br />for (arrayCount=0; arrayCount<heyItsAnArray.length; arrayCount++)<br /><br />{<br /><br />   replaceImg (heyItsAnArray[arrayCount]);<br /><br /> }<br /><br />function replaceImg (name)<br /><br />{ var myDoc = app.activeDocument;  app.findPreferences = app.changePreferences = null;  var math=document.search(name); for (i=math.length-1; i >=0; i--)  { myDir = Folder.selectDialog();  AllGraphics = Folder(myDir).getFiles(name)  for (i=0; i&lt;math.length; i++) { app.select(anil1[i]); myDoc.place(AllGraphics[i],false); } } }</pre>Unfortunately, copying and re-copying messes up the formatting -- but this is the logical order.<br /><br />And I repeat, although I copied the replaceImg function directly from your own ("working") code, I don't see how it could work.
                    • 7. Re: Find text and replace with image - Help needed
                      Level 1
                      Jongware,<br /><br />I try running it but gives errors:<br /><br />Error Number: 55<br />Error String: Object does not support the property or method 'changePreferences'<br />Line: 24<br />Source: app.findPreferences = app.changePreferences = null;<br /><br />This is the code I used<br /><br />// Find text and replace with image for InDesign CS3 <br />// http://www.adobeforums.com/webx?128@@.3bbf275d.59b6f012<br />var heyItsAnArray = new Array ( <br /><br />   "it contains this line!", <br /><br />   "as well as this one", <br /><br />   "or even more!", <br /><br />   "test.eps" ); <br /><br />for (arrayCount=0; arrayCount<heyItsAnArray.length; arrayCount++) <br /><br />{ <br /><br />   replaceImg (heyItsAnArray[arrayCount]); <br /><br /> } <br /><br />function replaceImg (name) <br /><br />{ var myDoc = app.activeDocument;  <br />     app.findPreferences = app.changePreferences = null;  <br />     var math=document.search(name); <br />     for (i=math.length-1; i >=0; i--)  <br />     { myDir = Folder.selectDialog();  <br />          AllGraphics = Folder(myDir).getFiles(name)  <br />          for (i=0; i<math.length; i++) { app.select(anil1[i]); <br />               myDoc.place(AllGraphics[i],false); <br /><br />               } <br /><br />          } <br /><br />     }<br /><br />Michael
                      • 8. Re: Find text and replace with image - Help needed
                        [Jongware] Most Valuable Participant
                        The initial code sample wasn't mine and I didn't try it. The poster said it worked 'as is' but needed refinement to work with arrays. I added support for arrays and cast my doubts on the rest :-|

                        Fortunately, your error message might as well be in a FAQ. It means you are trying CS (ID3.0) syntax in a later version -- CS2, CS3, or maybe even CS4. If you are really really in a hurry, create a folder "Version 3.0 Scripts" inside your Scripts folder and put the script there. ID will emulate the older version.

                        If you feel like tinkering, for CS2 and later the property is called "findTextPreferences" (and for change, "changeTextPreferences"), because you can find & change other things than text as well. The next error that will pop up is that you no longer can use "search("your text"), you have to place your text-to-find in the .findWhat field of findTextPreferences and use "findText()" (and, ditto, "changeText()").

                        Hope it helps.
                        • 9. Re: Find text and replace with image - Help needed
                          Level 1
                          Iongware,
                          I did as you said. That is I placed the script in a the "Version 3.0 Scripts" folder and presto. NO errors.
                          BUT. No results either. Nothing happens. Shouldn't the script prompt me for a file to choose from? Anyway I put my graphics in the same folder as the indd document but still no go.
                          Any suggestions? Do you probably have a link of where I can get in touch with the author? As for the rest I know nothing about scripting so I couldn't follow your thoughts.

                          Thanx
                          michael