7 Replies Latest reply on Jul 22, 2008 10:56 PM by Marco Lugli

    Aligning page item objects

    Marco Lugli Level 1
      Hi,
      I hope someone might suggest me a way to do the following: I want to align two page items (1 oval and 1 text frame) to their center coordinates. Here is my starting code:

      //Aligning page items to their center coordinates

      myDoc = app.documents.add();
      myFont = app.fonts.item("Arial");

      //Create Oval
      myOval = myDoc.ovals.add();
      with(myOval){
      geometricBounds = [0,0,5,5];
      fillColor = myDoc.swatches.item("Black");
      }

      //Create Text Frame
      myTxtFrame = myDoc.textFrames.add({contents:"1"});
      myParagraph = myTxtFrame.paragraphs.item(0);
      with(myParagraph){
      appliedFont = myFont;
      justification = Justification.centerAlign;
      fillColor = myDoc.swatches.item("Paper");
      pointSize = 15;
      }
      //Code for aligning the two page items

      Thanks
        • 1. Re: Aligning page item objects
          Peter Kahrel Adobe Community Professional & MVP
          1. Determine the horizontal and vertical centres of both objects. If "gb" are the beometricBounds of one object, then that object's vertical position is at gb[2]-gb[0], its horizontal position, at gb[3]-gb[1].

          2. Move one object to the other, using the .move() method. The amount to move vertically is the difference between the vertical position of the target object minus the vertical position of the object to be moved. The amount to move horizontally is determined similarly.

          You may have to compensate positive and negative movents (left-right and up-down) depending on the position of each object. Note that the .move() method works differently in CS2 and CS3.

          Peter
          • 2. Re: Aligning page item objects
            Level 1
            Those are height and width, in your point 1, Peter. The center coords are: [(gb[3] + gb[1])/2, (gb[2] + gb[0])/2].

            Dave
            • 3. Re: Aligning page item objects
              Marco Lugli Level 1
              Isn't there an Align method instead of the Move one?
              • 4. Re: Aligning page item objects
                Level 1
                No, you have to do it yourself.

                Dave
                • 5. Re: Aligning page item objects
                  Level 1
                  Hi Marco,

                  You might take a look at the AlignToPage.jsx example script that comes with CS3--it covers the basics of aligning objects.

                  But...in your example, you don't even need to do that. You're creating both objects, so you know the size and location of the objects. Just do something like this:

                  //Create Oval
                  
                  myOval = myDoc.ovals.add({geometricBounds:[0,0,5,5], fillColor:myDoc.swatches.item("Black")});

                  //Create Text Frame
                  myTxtFrame = myDoc.textFrames.add({contents:"1", geometricBounds:[0,0,5,5]});
                  myParagraph = myTxtFrame.paragraphs.item(0);
                  with(myParagraph){
                      appliedFont = myFont;
                      justification = Justification.centerAlign;
                      fillColor = myDoc.swatches.item("Paper");
                      pointSize = 15;
                  }


                  Two notes:

                  * Don't just tell the document to create the objects. Tell a specific page or spread. This will make your code more portable.

                  * If you're just centering the text in the oval, why not put the text in an oval text frame, rather than putting a text in a separate frame?

                  Thanks,

                  Ole
                  • 6. Re: Aligning page item objects
                    Peter Kahrel Adobe Community Professional & MVP
                    > Those are height and width, in your point 1

                    True, true -- so much for rattling down things from memory!

                    Thanks,

                    Peter
                    • 7. Re: Aligning page item objects
                      Marco Lugli Level 1
                      Thanks Olav and all of you guys - I'll do some tests and I'll give you my feedback.