7 Replies Latest reply on Jun 12, 2013 2:41 PM by Laubender

    [CS5.5][JS] How to get anchored object by text search result?

    JohnDevlon Level 1

      Hi,

       

      I need to get the anchored object by text search result. This can be an image, textframe, grouped objects...

       

      This is my code...

       

       

                  app.findTextPreferences = NothingEnum.nothing;

                  app.findTextPreferences.findWhat = "<FFFC>";

       

       

                  var myResults = app.activeDocument.findText();

                 

                  var currentParagrapfNum = -1;

                 

                  for (var n = 0; n < myResults.length; n++){

                     

                              var currentResult = myResults[n];

                             

                              currentParagrapfNum  = currentResult.paragraphs[0].index;

       

                              var currentAnchoredObject = ??? ;

       

                              currentAnchoredObject.anchoredObjectSettings.horizontalReferencePoint = AnchoredRelativeTo.TEXT_FRAME;

                              currentAnchoredObject.anchoredObjectSettings.verticalReferencePoint = VerticallyRelativeTo.LINE_BASELINE

       

                  }

       

      Can someone please help me?

       

      Thanx

        • 1. Re: [CS5.5][JS] How to get anchored object by text search result?
          Peter Kahrel Adobe Community Professional & MVP

          Look for ^a (which stands for anchored object). Something along the lines of

           

          app.findTextPreferences.findWhat = "^a";
          myResults = app.activeDocument.findText();
          for (var n = 0; n < myResults.length; n++){
              myResults[n].anchoredObjectSettings.horizontalReferencePoint = AnchoredRelativeTo.TEXT_FRAME;
              myResults[n].anchoredObjectSettings.verticalReferencePoint = VerticallyRelativeTo.LINE_BASELINE
          }
          

           

          It's not clear what you want with currentParagrapfNum, by the way.

           

          Peter

          • 2. Re: [CS5.5][JS] How to get anchored object by text search result?
            JohnDevlon Level 1

            Hi Peter,

             

            Thanks for the "^a" tip.

             

            Unfortunately the anchoredObjectSettings property doesn't exists in the search result item.

            The item is a character object. Any suggestions?

             

            Thanks

            • 3. Re: [CS5.5][JS] How to get anchored object by text search result?
              Peter Kahrel Adobe Community Professional & MVP

              Yes, true, ^a finds anchors but they're type character. What you need is

               

              myResults[n].pageItems[0].anchoredObjectSettings.horizontalReferencePoint = AnchoredRelativeTo.TEXT_FRAME;

               

              Peter

              • 4. Re: [CS5.5][JS] How to get anchored object by text search result?
                Laubender Adobe Community Professional & MVP

                @John – yes, it's a character, but the character has a pageItems.length.
                Or a specific textFrames.length if the anchored object happens to be a textFrame. Or a rectangles.length, if it happens to be such an object, etc.pp.

                 

                So myFoundCharacter.pageItems[0] is the anchored object.

                 

                From that it's easy to get the anchoredObjectSettings:

                 

                myFoundCharacter.pageItems[0].anchoredObjectSettings.properties.toSource();
                

                 

                 

                Uwe

                • 5. Re: [CS5.5][JS] How to get anchored object by text search result?
                  JohnDevlon Level 1

                  Hi Peter,

                   

                  Thanks for your feedback. It works great.

                   

                  John

                  • 6. Re: [CS5.5][JS] How to get anchored object by text search result?
                    JohnDevlon Level 1

                    Hi Laubender;

                     

                    Thanks for your reply.

                     

                    John

                    • 7. Re: [CS5.5][JS] How to get anchored object by text search result?
                      Laubender Adobe Community Professional & MVP

                      @John – it's a bit more tricky to find out what type of anchored object you have found.


                      E.g.:

                       

                       

                      myResults[n].pageItems[0].constructor.name
                      

                       

                      will always return [object PageItem] and not the specific: [object TextFrame] or [object Rectangle] or [object Group]…

                      I think you get the idea.

                       

                      The problem with that is:
                      You get an error (even if you know the anchored object is a text frame!), if you ask for:

                       

                      myResults[n].pageItems[0].insertionPoints.length;

                       

                      PageItems have no property insertionPoints, TextFrames (among other objects) have!

                      One workaround would be to test for a specific string with the properties of pageItems[0] put toSource():

                       

                      var myFoundCharacter = myResults[n];
                      var myAnchoredObject = myFoundCharacter.pageItems[0];
                      
                      //Test for TextFrame:
                      
                      var regExpression = /\/\/text-frame\[@id=/;
                      
                      if(regExpression.test(myAnchoredObject.properties.toSource())){
                      
                          //the variable "myAnchoredObject" previously points to a pageItem, now it points to a textFrame:
                          myAnchoredObject = myFoundCharacter.textFrames[0];
                          var insPointsLength = myAnchoredObject.insertionPoints.length;
                          
                          alert("Anchored Object is: TextFrame"+"\t"+"Number of insertion points: "+insPointsLength);
                      
                          };
                      
                      else{alert("Found anchored object is not a text frame.")};
                      

                       

                      I came up with that spontaniously.
                      There might be better methods.

                      I think we have already discussed that here in this forum.
                      (But I'm too lazy to search for it right now ;-)
                      Time for me to go to bed here in Germany…)

                       

                      Uwe

                      1 person found this helpful