3 Replies Latest reply on Aug 19, 2014 12:30 AM by Laubender

    Finding a graphics object "near" a text found with grep

    cschweden

      Hi,

       

      I just started with JS scripting in InDesign CS6. The goal is to find all item codes in the document, add a hyperlink to them and add the same link to a graphics object directly following the item code.

       

      Adding the link to the text found with GREP is not the problem, but how can I "find" the next graphics obejct in the affected paragraph?

       

      Many thanks!

        • 1. Re: Finding a graphics object "near" a text found with grep
          Trevorׅ Adobe Community Professional

          Hi cschweden,

           

          Are the graphics anchored (either in line or not) objects?

          If yes then you can change your Grep to include the anchored object marker ~a.

           

          For example if you wanted to collect the next graphic within the same paragraph as the number 999 you could use something like.

          app.findGrepPreferences = app.changeGrepPreferences = null;
          app.findGrepPreferences.findWhat = "999[^\r]*~a";
          !app.properties.activeDocument && exit();
          var doc = app.activeDocument,
              finds = doc.findGrep(true),
              l = finds.length, graphs = [], c = 0;
          while (l--) graphs[c++] = finds[l].characters[-1].allGraphics[0];
          

           

          The graphs array will contain all the relevant graphics.

          HTH

           

          Trevor

          • 2. Re: Finding a graphics object "near" a text found with grep
            Laubender Adobe Community Professional & MVP

            @cschweden – additionally to that what Trevor said and posted:

             

            You can look at every frame that is anchored in two different ways:
            1. You can select it with the Text Tool. So consider it as a single character; a special character of course.

            2. You can select the frame with the Selection Tool or the Direct Selection Tool. So now it is something like a rectangle, oval, polygon, text frame, that is anchored in the text.

             

            If we do a GREP search, we look at the text level. So #1 will apply here. And happily GREP is providing a special notation for anchored objects (equal what they are): ~a


            If one of these objects is found, a single character, it is indeed containing the fully anchored object.

            You can easily find that out, if you do the following:


            Have some text with an anchored object in it.
            Select the anchored object with the text tool. That means select the special character that represents this object in the Story.

            Now run the following code from the ESTK (ExtendScript Toolkit):

             

            var myAnchoredThing = app.selection[0].texts[0].pageItems[0].getElements()[0];
            $.writeln(myAnchoredThing);
            

             

            The result would be [object Rectangle] , if you anchored a rectangle.

             

            In case you want to add an hyperlink to the container object of an image itself, you're done. You found the object you have to deal with. In case you want dig deeper and working with the image or the images, or the graphic or the graphics inside that container, you have to go a step further.

             

            You now do a loop through the allGraphics array of that object. In case this rectangle is itself holding a group of images pasted inside, there may be more than one images or graphics inside you want to hyperlink:

             

            var myAnchoredThing = app.selection[0].texts[0].pageItems[0].getElements()[0];
            var myGraphics = myAnchoredThing.allGraphics;
            
            for(var n=0;n<myGraphics.length;n++){
                //DummyCode
                addHyperlinkForGraphic(myGraphics[n]);
                };
            
            //DummyCode that writes the names of the graphics to the JavaScript Console of the ESTK
            //Could fail, if there is no itemLink, because a graphic was pasted inside the document!
            function addHyperlinkForGraphic(aGraphic){
                $.writeln(aGraphic.itemLink.name);
                return;
                };
            

             

            Uwe

            • 3. Re: Finding a graphics object "near" a text found with grep
              Laubender Adobe Community Professional & MVP

              Let me add: if the graphic is not part ot the same story where your code is found, you will have hard times to find the right graphic. Then you need a list of codes together with the names of the graphics to have a good starting point. Or if the the text frame that holds the code is grouped together with the graphic. Some unique rules of relationship between the code and the graphic are needed to write a reliable script.

               

              Uwe