3 Replies Latest reply on Jul 25, 2011 1:31 AM by gorak43634645

    Listing the objects or graphics that intersect (overlap) a text frame

    gorak43634645 Level 1

      Hi there,

       

      I'm trying to script a more flexible 'Live Caption' facility, which reads an image's keywords, formats them a bit, then saves to a text frame.

      The following succesfully reads the selected images keywords:

       

      myOut = app.activeDocument.selection[0].graphics[0].itemLink.linkXmp.keywords;

       


       

      I'd like to be able draw a text box that intersects (overlaps) an image, select it, and run the script.  The script would:

       

      (a) work out what image is intersected to the selected text box

      (b) read the keywords

      (c) write it into the selected text box

       

      I'm having trobule with (a) - I cannot work out how you return a list of any intersected objects or graphics.

       

      I have tried the following with a text frame selected:

       

      app.activeDocument.selection[0].allGraphics[0]

      app.activeDocument.selection[0].allPageItems[0]

       

      The TextFrame object appears to have the above properties, and according to my test, If a text frame is selected, app.activeDocument.selection[0] will return a text frame type object.

       

      However, it just returns an error:

       

      Error 21 - Error String - unitentified is not an object

       

      Anyone here know how to detect the intersected objects?

       

      Thanks

        • 1. Re: Listing the objects or graphics that intersect (overlap) a text frame
          John Hawkinson Level 5

          There is no built-in way to return a list of intersected objects.

          I'm having trobule with (a) - I cannot work out how you return a list of any intersected objects or graphics.

           

          I have tried the following with a text frame selected:

           

          app.activeDocument.selection[0].allGraphics[0]

          app.activeDocument.selection[0].allPageItems[0]

           

          The TextFrame object appears to have the above properties, and according to my test, If a text frame is selected, app.activeDocument.selection[0] will return a text frame type object.

          These don't ahve anything to do with intersection. They would show you items that are part of your TextFrame, such as anchored objects.

          That is not what you want.

           

          You can loop over all your graphics frames and compare them to your text frame with a function like this:

           

          // Do rectangles a and b overlap?
          overlap = function (a, b) {
                  var
                          ab = fromBounds(a.visibleBounds),
                          bb = fromBounds(b.visibleBounds);
                  
            return (ab.x1 <= bb.x2 &&
                    bb.x1 <= ab.x2 &&
                    ab.y1 <= bb.y2 &&
                    bb.y1 <= ab.y2);
          };
          

           

          Oh, I guess that depends on this helper function:

           

          function fromBounds(gB) {
                  return { y1: gB[0], x1: gB[1], y2: gB[2], x2: gB[3],
                          height: gB[2]-gB[0], width: gB[3]-gB[1]};
          }
          
          • 2. Re: Listing the objects or graphics that intersect (overlap) a text frame
            gorak43634645 Level 1

            Great, I'll take a look at this concept.  Also, there's no reason that the text boxes and graphics cannot be grouped, anchored, or identified in some other way as a pair, rather than just being overlapped.  I had a short go at idetifying groups, did not succeed, but will have another go.

             

            Lastly, if you tag a text box, can you identify the tag using script?  This would be great so the script can customise the caption based on the boxes tag.  If there's a one word answer for this, it would be great to know.  Thanks

            • 3. Re: Listing the objects or graphics that intersect (overlap) a text frame
              gorak43634645 Level 1

              Think I got it sussed now.  This is the (working but incomplete) code I have, which does the following:

               

              (a) identifies groups of 1 text box and 1 graphic only.

              (b) reads the graphics keywords.

              (c) checks the "script label" of the text box, to see see what type of caption should go in the box.  In this case, only one label has been defined, "caption1", which just inserts the caption as-is.

              (d) writes the caption into the text box.

              (e) warns if any groups of 1 text box and 1 graphic only are found, but a script label has not been set for the text box.  For groups where this occurs, the text box is left unchanged.

               

              main();

              function main(){

                  numGroups = app.activeDocument.groups.length; //the number of groups in the document


                  for (i=0;i<numGroups;i++) {

                          numGraphics = app.activeDocument.groups[i].allGraphics.length; //determine how many graphics in the group

                          numTextFrames = app.activeDocument.groups[i].textFrames.length; //determine how many text frames in the group

                          if (numGraphics == 1 && numTextFrames == 1){  //only groups that contain 1 text frame and 1 graphic  will be processed

                                  graphicKeywords = app.activeDocument.groups[i].allGraphics[0].itemLink.linkXmp.keywords[0]; //get keywords of graphic in current group

                                  if (app.activeDocument.groups[i].textFrames[0].label == "caption1"){

                                      app.activeDocument.groups[i].textFrames[0].contents = graphicKeywords;  //set the text frame with the keywords

                                  }

                                  else {

                                      alert("A textbox that is suitable for a caption does not have its label set.");

                                  }

                             }

                          else {

                                  }

                         }

              }

               

              For part (c) I used script labels as I got it working straight away, I did not work out if tags (using the tags panel) could be identified using the script.

               

              Later I will add more caption types "caption1", "caption2" etc. and the necessary regular expressions for each that will format the captions differently.

               

              If I have any trouble again I'll post - but hopefully should be good for now...    thanks for all the help