5 Replies Latest reply on Jun 28, 2016 1:25 AM by drone_creative

    Replace Text with Image and Stroke Image

    SoccerGuy3x

      I need to be able to insert an image in place of text in a story and have the image placed with a 1pt black stroke. I had a script that did this for me, but it is very old and it has stopped working with CS5. I managed to cobble together a new script by using stuff in the forums that handles the replacement, but cannot figure out how to adapt it for the stroke. Help!

       

      Current script:

       

       

      if(app.documents.length != 0){

       

      var myFolder = Folder.selectDialog ('Choose a folder with images');

       

        if(myFolder != null){

       

      // reset the Find/Change dialog

       

      app.findGrepPreferences = app.changeGrepPreferences = null;

       

      // formulate a grep search string

       

      app.findGrepPreferences.findWhat = '@.+?@';

       

      // find all occurrence, last one first

       

      f = app.activeDocument.findGrep (true);

       

      for (i = 0; i < f.length; i++)

       

         {

       

         // construct file name

       

         name = f[i].contents.replace (/@/g, '');

       

         // place the image

       

         f[i].insertionPoints[0].place (File (myFolder.fsName + '/' + name));

       

         }

       

      // delete all @??@ codes

       

      app.activeDocument.changeGrep()

        }

       

      }

       

      else{

       

         alert('Please open a document and try again.');

       

      }

        • 1. Re: Replace Text with Image and Stroke Image
          milligramme Level 3

          Hi,

          I tried your code  on InDesign CS4 (not CS5) and changed a little, it works for me.

           

          When search results replace to the images,

          at first create graphic frame

          And place images instead of  placing image into story directly.

          Fit graphic frame to content.

          Set  stroke weight and color (1pt / Black) to graphic frame.

           

          if(app.documents.length != 0){
              var myFolder = Folder.selectDialog ('Choose a folder with images');
              if(myFolder != null){
                  // reset the Find/Change dialog
                  app.findGrepPreferences = app.changeGrepPreferences = null;
                  // formulate a grep search string
                  app.findGrepPreferences.findWhat = '@.+?@';
                  // find all occurrence, last one first
                  f = app.activeDocument.findGrep (true);
                  for (i = 0; i < f.length; i++){
                      // construct file name
                      name = f[i].contents.replace (/@/g, '');
                      
                      // place the image
                      var tempRect = f[i].insertionPoints[0].rectangles.add();
                      tempRect.place (File (myFolder.fsName + '/' + name));
                      tempRect.fit(FitOptions.FRAME_TO_CONTENT);
                      tempRect.strokeWeight = "1pt";
                      tempRect.strokeColor = "Black";
                      
                  }
              // delete all @??@ codes
              app.activeDocument.changeGrep();
              }
          }
          else{
             alert('Please open a document and try again.');
          }
          

           

           

          mg

          • 2. Re: Replace Text with Image and Stroke Image
            [Jongware] Most Valuable Participant

            Milligramme (& Soccerguy),

             

            No need to use this:

             

            // place the image
                        var tempRect = f[i].insertionPoints[0].rectangles.add();
                        tempRect.place (File (myFolder.fsName + '/' + name));
                        tempRect.fit(FitOptions.FRAME_TO_CONTENT);
                        tempRect.strokeWeight = "1pt";
                        tempRect.strokeColor = "Black";

             

            since "place" returns the placed object(s). The possible plural is because "place" always returns an array! (Typically, this will only have one element.)

             

            This ought to work as well. Untested.

             

                        var placedObjects = place (File (myFolder.fsName + '/' + name));
                        placedObjects[0].parent.strokeWeight = "1pt";
                        placedObjects[0].parent.strokeColor = "Black";

             

            The placed object itself is an "Image", so you need to set stroke weight and color for its parent, the containing rectangle.

            • 3. Re: Replace Text with Image and Stroke Image
              milligramme Level 3

              Jongware (& Soccerguy),

               

              Thank you for your advice.

              your code not works, but it solved with the code below.

                   // place the image
              
                   var placedObjects = f[i].insertionPoints[0].place (File (myFolder.fsName + '/' + name));
                   placedObjects[0].parent.strokeWeight = "1pt";
                   placedObjects[0].parent.strokeColor = "Black";
              

               

               

               

              mg

              • 4. Re: Replace Text with Image and Stroke Image
                SoccerGuy3x Level 1

                Sweet guys! Thanks for the help!

                • 5. Re: Replace Text with Image and Stroke Image
                  drone_creative

                  Milligramme (& Soccerguy),

                   

                  thanks for sharing this script, it works great, but I couldn't figure out how I could also define a specified height for the placed image frame. Do You have any suggestions for this?

                  Thanks in advance!