7 Replies Latest reply on Oct 3, 2008 4:15 PM by (Dave_Saunders)

    [CS2][JS] Search text & replace with inline Image

    Level 1
      I am working with a large layout with many entries. There is a text "placeholder" that I need to search for and replace with an inline image in that exact spot.
      I need to apply an Object Style to that image.

      There is a loop above this for each find/replace to perfom
      I have supplied samples to show what the variables will hold
      The following assumes that an Objectstyle named "image" exists

      //places the image
      app.changePreferences = null;
      findName = "OR-00014500-24-0000-1";
      fileName = "Ads:OR-00014500-24-0000-1:OR-00014500-24-0000-1.eps"
      myFinds = app.search(findName)
      for (j = myFinds.length - 1; j >= 0; j--) {
      app.select(myFinds[j]);
      app.place(fileName, false,{appliedObjectStyle:"image"});
      }

      This doesn't apply the style to the placed object and I have tried many different ways to apply this in the properties.

      Any help would be appreciated.
        • 1. Re: [CS2][JS] Search text & replace with inline Image
          Level 1
          app.place requires a file reference not just a filename. And that path doesn't look like a complete path either.

          Dave
          • 2. Re: [CS2][JS] Search text & replace with inline Image
            Level 1
            Ah, but the text of your question suggests that it's working nonetheless.

            Have you tried applying the object style as a separate step? Hmm, but app.place returns nothing. So you'll need to get at the placed object by using the index of myFinds[j]. Something like:

            myStory = myFinds[j].parent;
            myIndex = myFinds[j].index;

            do that before placing. Then after placing:

            myStory.characters[myIndex].pageItems[0].applyObjectStyle(myStyle);

            where you will need to have set myStyle to the object style, not just its name (I seem to recall -- CS2 seems like such a long time ago now).

            Dave
            • 3. Re: [CS2][JS] Search text & replace with inline Image
              Level 1
              Hi Dave,

              First I'd like to thank you not only for your reply but for the volumes of information you have supplied for folks like me. I wouldn't have made it this far without you. The script is a modified version of something you posted (probably long ago).

              The file path is complete, the equivalent would be /Volumes/Ads/######/#######.ext.
              This laid out the strange way our corporation demands our servers to be, the same people who say CS2 is good enough for us lowly designers.

              Your method worked, now I need to test it on the big loop.

              Thank you again for all your help here and beyond. You've been more valuable than the Scripting Reference and Scripting Guide combined, because you explain why it works not just how it does.
              • 4. Re: [CS2][JS] Search text & replace with inline Image
                Level 1
                I've seem to hit another hurdle....

                The code works well for searching and replacing the images. It applies the style to the image, but these images are not inline they are anchored. I ended up with a nice stack of images in the same corner.
                Ok... I need to then apply styles based on the images that have been placed already..

                This works well for cycling through the pages and with a bit more will change the style...
                myDoc = app.activeDocument;
                pagecount = myDoc.pages.length;
                for ( page=0; page < pagecount; page++){
                if (myDoc.pages.item(page).textFrames.length != 0){
                app.select(myDoc.pages.item(page).textFrames.item(0));
                piccount = app.selection[0].rectangles.length;
                if (piccount != 0){
                for ( bob=0; bob < piccount; bob++){
                app.select(myDoc.pages.item(page).textFrames.item(0));
                app.select(app.selection[0].rectangles.item(bob));
                // Get geometric bounds
                // alert(app.selection[0].geometricBounds);
                // Get object Style
                // alert(app.selection[0].appliedObjectStyle.name);
                // Do more here....
                }
                }
                }
                }

                I can't use this though :(

                I need to determine how many images have already been placed on the page before placing the image in the search and replace....

                myFinds = app.search(replaceName)
                for (j = myFinds.length - 1; j >= 0; j--) {
                app.select(myFinds[j]);
                myStory = myFinds[j].parent;
                myIndex = myFinds[j].index;
                //This will change using a series of Case Switches
                // Here is where I need to find the page and run the previous script or something like it myStyle = app.activeDocument.objectStyles.item("image");
                app.place(fileName, false);
                myStory.characters[myIndex].pageItems[0].applyObjectStyle(myStyle);
                app.select(myStory.characters[myIndex].pageItems[0]);
                //alert(app.selection[0].appliedObjectStyle.name);
                }

                What I need to do is kind of combine the two....
                But I can't seem to get the current page of the selected image....

                :(

                I could use the page by page script, but it moves the character anchor point of images and pushes them to the next page. This throws off the pretty layout I'm going for.

                What am I doing wrong here ?
                • 5. Re: [CS2][JS] Search text & replace with inline Image
                  Level 1
                  Lets make this a whole lot easier to grasp.

                  I have some text selected:

                  app.selection[0].parent is the Story

                  How do I get the text frame ID or Page # that the selected text is on?
                  app.select(app.selection[0].textFrames[0]);
                  that freaks out pretty good saying it received nothing.....

                  alert(app.selection[0].textFrames.itemByID.id);
                  returns undefined...
                  • 6. Re: [CS2][JS] Search text & replace with inline Image
                    [Jongware] Most Valuable Participant
                    The selection does not contain text frames, it contains "text". If you query the parent, you get a Story, and although that
                    i does
                    have a frame property ("textFrames"), it contains all text frames that story runs through, not just of your selection.

                    However, the text object you selected
                    i also
                    has a "textFrames" property -- all the text frames
                    i in
                    your selection. That would be any inline text frames...

                    Now,
                    b I can't check right now
                    but I think

                    >app.selection[0].parentTextFrames[0]

                    points the first text frame -- the one at the start of your selection, as it may span multiple frames --, and from that you can get to

                    >app.selection[0].parentTextFrames[0].parent

                    which is "a Spread, MasterSpread, PageItem, Oval, Rectangle, Polygon, GraphicLine, Group, State, Document, Layer, Page, Button, TextFrame, Story, Text, Character, Word, Line, TextColumn, Paragraph, TextStyleRange, Cell, Table, InsertionPoint or Footnote" (as the Help helpfully states). Now, if it is a simple document without any nested frames, the parent typically will be a Page. From that on, you can get the actual page number:

                    >app.selection[0].parentTextFrames[0].parent.name

                    Use "name" because this will return the page number as a string, because it can be any one of "1", "a", "i", or even "Section1:1".
                    • 7. Re: [CS2][JS] Search text & replace with inline Image
                      Level 1
                      Search this forum for "findPage" -- there's a function posted that will find the page of anything.

                      Dave