8 Replies Latest reply on Nov 8, 2010 6:07 AM by [Jongware]

    How to script dbl-click in center of a text frame [JS]

    PeteBaumgartner Level 1

      How can I script a double - click in center of a text frame?

       

      Is there a sample like this?

       

      Thanks.

        • 1. Re: How to script dbl-click in center of a text frame [JS]
          Haakenlid Level 3

          You don't script double clicks, or the position of the mouse cursor. But you can make a script that has a similiar outcome as double click. I assume that you want to select the word or insertionpoint that is at the center of the text frame.

          • 2. Re: How to script dbl-click in center of a text frame [JS]
            PeteBaumgartner Level 1

            Yes. So what is the approach/sample you would suggest?

            • 3. Re: How to script dbl-click in center of a text frame [JS]
              PeteBaumgartner Level 1

              I guess I should also mention there are 2 layers involved. I want to select by the center of an 'overlay layer' text box - the text in another text box 'below'. 

              • 4. Re: How to script dbl-click in center of a text frame [JS]
                Haakenlid Level 3

                I assume you first manually select a text frame.

                 

                Do you want to end up with a selected word or a insertion point?

                Is the target text frame full of text?

                Could it be half full of text?

                If it's not full of text do you want the selection to be as close as possible to the geometric center of the Text Frame, or closest to the centre of the actual text in the frame?

                Are there any other overlapping text frames or just the two?

                what are the names of the two layers?

                 

                Here's some pseudocode of how it could be done.

                 

                myScript(application.selection[0]) // first object in selection, which should be a text frame
                
                var myScript = function(myFirstTextFrame)
                myTargetTextFrame = first text Frame below myFirstTextFrame that has geometricBounds that overlap
                absoluteCentre = cooridates of myTargetTextFrames centre
                for all words in myTargetTextFrame{
                     if (word.coordinates == absoluteCentre){
                          application.select(word)
                          exit();
                     }
                }
                

                 

                Does that make any sense to you?

                • 5. Re: How to script dbl-click in center of a text frame [JS]
                  PeteBaumgartner Level 1

                  Sure, I understand. I have been thinking about the geometry - multi-page aspects of it.

                   

                  I suppose that the geoBounds of the selected Rectangle (on the top layer) must be in Spread coords - so I will need to add a y offset = the spread index times the spread height, and do the same at target time when scanning for the text line; OR perhaps use the spread coords and a spread index - to first orient to correct spread.  Thoughts?

                   

                  What is the Go To Spread n command?

                   

                  Actually I begin by scanning though the ' overlay layer' for rectangles hovering over the 'text layer' and select them 1 by 1 in script to (hopefully) find the text base layer beneath...

                  • 6. Re: How to script dbl-click in center of a text frame [JS]
                    Haakenlid Level 3

                    I suppose that the geoBounds of the selected Rectangle (on the top layer) must be in Spread coords - so I will need to add a y offset = the spread index times the spread height, and do the same at target time when scanning for the text line; OR perhaps use the spread coords and a spread index - to first orient to correct spread.  Thoughts?

                     

                    There's no document wide coordinate space. Each spread has it's own coordinate space. Different spreads are in a "third dimension" if you will.

                    What is the Go To Spread n command?

                     

                    No such function as far as I know. But this will do it, I think.

                     

                    function goToSpread(n){
                         var myDoc = app.activeDocument;
                         var mySpread = myDoc.spreads[n];
                         myDoc.layoutWindows[0].activeSpread = mySpread;
                    }
                    

                     

                    However the scripting model does not access the objects through the user interface. So you don't need to script double clicks and go to spread.

                    1 person found this helpful
                    • 7. Re: How to script dbl-click in center of a text frame [JS]
                      PeteBaumgartner Level 1

                      OK.

                       

                      To map find the text on the base layer from the rectangles on the overlay layer - I first find on the overlay layer a rectangle's .geometricBounds and .documentOffset; then on the base layer i find the text frame at that .documentOffset and run through its lines until one of the line .baseline is found to fall between the top and bottom of the rectangle y coords. Once I have my line I calculate the x-center of the rectangle and then run through the chosen line's character x-positions ( .horizontalOffset ) until I over run my character's xposition. I can select this character's word with: chosenLine.characters[c].word[0].

                       

                      Thanks!

                      • 8. Re: How to script dbl-click in center of a text frame [JS]
                        [Jongware] Most Valuable Participant

                        You got it! -- well, maybe apart from this:

                         

                        function(){return A.apply(null,[this].concat($A(arguments)))}

                        then run through the chosen line's character x-positions ( .horizontalOffset ) until I over run my character's xposition. I can select this character's word with: chosenLine.characters[c].word[0].

                         

                        Per character is not necessary. Since you want to get an entire word, you can skip by entire words. Something like (pseudo-code):

                         

                        wordindex = 0;

                        while (chosenLine.words[wordindex].horizontalOffset < yourXposition)

                          wordindex++;

                         

                        -- this will automatically stop when the first character of the next word is 'too far'. It'll also work if yourXposition is to the left of the entire text frame -- in that case, wordindex will automatically be 0. The other degenerate case, too far too the right (or a short line) is not handled here. Perhaps you could add a check for a rightmost position:

                         

                        chosenLine.insertionPoints.item(-2).horizontalOffset

                         

                        which ought to give you the position of the right side of the last character. Warning: initially I typed "item(-1)", which ought to point to the last insertionPoint of that line (just to the right of the last character). However, it seems ID "sees" the line wrapping to the next line on that position, and returns the position of the first character of the next line instead!

                         

                        (Perhaps it's safer to just use chosenLine.characters.item(-1).horizontalOffset -- that ought to work as well.)