3 Replies Latest reply on Nov 22, 2013 7:55 AM by Laubender

    Verify if textframe is found in front of an Image


      Hi everyone,


      I'm struggling with this since some days.  Actually I'm working on a script in JS, which needs to identify any textframe which overlays (partly or completely) on an image (see below screenshot). The mathematical comparison of their geometric bounds work successfully.  No problem on that part.  But I need a way to  find whether the textframe is found behind or in front of the image?  I only need textframes found in front of images.




      I would be grateful if someone could help.

        • 1. Re: Verify if textframe is found in front of an Image
          Jump_Over Level 5



          Just a raw idea:

          assuming both items are not inside any group - so we have just items on layers - you could use a function:

          // return true if pItem1 is over pItem2


          function firstIsUp(pItem1, pItem2) { 
                    layerIdx1 = pItem1.itemLayer.index,
                    layerIdx2 = pItem2.itemLayer.index,
                    itemID1 = pItem1.id,
                    itemID2 = pItem2.id,
                    itemLayerIdx1 = app.activeDocument.layers.item(layerIdx1).pageItems.itemByID(itemID1).index,
                    itemLayerIdx2 = app.activeDocument.layers.item(layerIdx2).pageItems.itemByID(itemID2).index;
               if (layerIdx1 < layerIdx2) return true;
               if (layerIdx1 >layerIdx2) return false;
               if (layerIdx1 == layerIdx2 && itemLayerIdx1 < itemLayerIdx2) return true;
               else return false;


          playing with groups or anchored items makes it more complex...



          • 2. Re: Verify if textframe is found in front of an Image
            Laubender Adobe Community Professional & MVP

            @Yohan828 – the "brute force" method in the UI would be using Path Finder with the "Substract" command on a selection of duplicates of the text frame and the image.


            To make that work, both objects, text frame and image container must overlap:


            1. If the text frame is in front of the image container  => "substract" => results in a text frame


            2. If the text frame is behind the image container => "substract" => results in a spline object with image inside


            Just to give you an idea…



            • 3. Re: Verify if textframe is found in front of an Image
              Laubender Adobe Community Professional & MVP

              Another idea:
              Select a duplicate of both pageItems and invoke a script menu action to add a MultiStateObject (MSO).

              The top most object will always go to state 1 of the MSO, the object in the back to state 2.


              Here a snippet for testing in the ESTK (InDesign CS5 and above):


              //An InDesign file with two objects on the same page, 
              //one text frame and one rectangle that holds an image.
              var d=app.documents[0];
              var myImageContainerDup = d.allGraphics[0].parent.duplicate();
              var myTextFrameDup = d.textFrames[0].duplicate();
              //Make a selection:
              //ID for New MultiStateObject: 83621
              //Adding a MultiStateObject (MSO) with the two selected objects:
              try{app.scriptMenuActions.itemByID(83605).invoke()}catch(e){$.writeln("Something went wrong!");exit()};
              //The object in the front goes to state 1
              //The object in the back goes to state 2
              var myMSO = d.multiStateObjects[-1];
              //State 1
              $.writeln("Front:"+"\t"+myMSO.states[0].groups[0].pageItems[0].getElements()[0].constructor.name); //=> "TextFrame" if text frame is in front
              //State 2
              $.writeln("Back:"+"\t"+myMSO.states[1].groups[0].pageItems[0].getElements()[0].constructor.name); //=> "TextFrame" if text frame is in the back
              //Some cleaning: