🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: adobe.scripting.indesign
Article #19361

Re: To align Objects (VB)

#19361
From: Olav Kvern
Date: Tue, 07 Jan 2003 21:12
153 lines
6506 bytes
Reinhard--

You do have to calculate the position, but it's not difficult. Here's an example script (the enums, subtroutine, and function can be used in your project).



Public Enum myAlignments 
   myLeft = 1 
   myRight = 2 
   myTop = 3 
   myBottom = 4 
   myYCenter = 5 
   myXCenter = 6 
   myXYCenter = 7 
   myTopLeft = 8 
   myTopCenter = 9 
   myTopRight = 10 
   myLeftCenter = 11 
   myRightCenter = 12 
   myBottomLeft = 13 
   myBottomCenter = 14 
   myBottomRight = 15 
End Enum

Private Sub Command1_Click() 
Rem Create an example document to demonstrate object alignment. 
Dim myInDesign As InDesign.Application 
Dim myCollection As New Collection 
Set myInDesign = CreateObject("InDesign.Application.2.0") 
Set myDocument = myInDesign.Documents.Add 
myPageHeight = myDocument.DocumentPreferences.PageHeight 
myPageWidth = myDocument.DocumentPreferences.PageWidth 
Set myPage = myDocument.Pages.Item(1) 
myAlignment = myBottomRight 
Rem Add five randomly-sized rectangles to the page 
For myCounter = 1 To 5 
    Set myRectangle = myPage.Rectangles.Add 
    myY1 = myPageHeight * Rnd 
    myX1 = myPageWidth * Rnd 
    myY2 = myPageHeight * Rnd 
    myX2 = myPageWidth * Rnd 
    myRectangle.GeometricBounds = Array(myY1, myX1, myY2, myX2) 
    Rem Add the rectangle to a collection object (which we'll use later). 
    myCollection.Add myRectangle 
Next 
Rem Align the objects 
myAlignObjects myCollection, myAlignment 
End Sub

Private Function myFindBounds(myCollection) 
Rem Returns the bounds (and X and Y centers) of a set of objects. 
Select Case TypeName(myCollection) 
    Case "Collection" 
        For myCounter = 1 To myCollection.Count 
            Set myPageItem = myCollection.Item(myCounter) 
            myBounds = myPageItem.VisibleBounds 
            If myCounter = 1 Then 
                Rem If it's the first object in the collection, then use 
                Rem the bounds of the object to set the initial values. 
                myY1 = myBounds(0) 
                myX1 = myBounds(1) 
                myY2 = myBounds(2) 
                myX2 = myBounds(3) 
            Else 
                Rem If it's not the first object, then compare the bounds of the 
                Rem current object to the stored bounds; replace stored value if necessary. 
                If myBounds(0) < myY1 Then 
                    myY1 = myBounds(0) 
                End If 
                If myBounds(1) < myX1 Then 
                    myX1 = myBounds(1) 
                End If 
                If myBounds(2) > myY2 Then 
                    myY2 = myBounds(2) 
                End If 
                If myBounds(3) > myX2 Then 
                    myX2 = myBounds(3) 
                End If 
            End If 
        Next 
    Case Else 
        Rem It's a single object, so just get its bounds. 
        myBounds = myCollection.VisibleBounds 
        myY1 = myBounds(0) 
        myX1 = myBounds(1) 
        myY2 = myBounds(2) 
        myX2 = myBounds(3) 
End Select 
Rem Calculate the center points. 
myXCenterPoint = myX1 + ((myX2 - myX1) / 2) 
myYCenterPoint = myY1 + ((myY2 - myY1) / 2) 
Rem Return the bounds (and center points) of the objects in the collection. 
myFindBounds = Array(myX1, myY1, myX2, myY2, myXCenterPoint, myYCenterPoint) 
End Function

Private Sub myAlignObjects(myCollection, myAlignment) 
Rem Subroutine for moving objects to a given alignment point. 
myAlignmentArray = myFindBounds(myCollection) 
For myCounter = 1 To myCollection.Count 
    Set myObject = myCollection.Item(myCounter) 
    myObjectBounds = myFindBounds(myObject) 
    Select Case myAlignment 
        Case myLeft 
           myX = myAlignmentArray(0) 
           myY = myObjectBounds(1) 
        Case myRight 
            myX = myAlignmentArray(2) - (myObjectBounds(2) - myObjectBounds(0)) 
            myY = myObjectBounds(1) 
        Case myTop 
            myX = myObjectBounds(0) 
            myY = myAlignmentArray(1) 
        Case myBottom 
            myX = myObjectBounds(0) 
            myY = myAlignmentArray(3) 
        Case myTopLeft 
            myX = myAlignmentArray(0) 
            myY = myAlignmentArray(1) 
        Case myLeftCenter 
            myX = myAlignmentArray(0) 
            myY = myAlignmentArray(5) - ((myObjectBounds(3) - myObjectBounds(1)) / 2) 
        Case myBottomLeft 
            myX = myAlignmentArray(0) 
            myY = myAlignmentArray(3) - (myObjectBounds(3) - myObjectBounds(1)) 
        Case myTopRight 
            myX = myAlignmentArray(2) - (myObjectBounds(2) - myObjectBounds(0)) 
            myY = myAlignmentArray(1) 
        Case myRightCenter 
            myX = myAlignmentArray(2) - (myObjectBounds(2) - myObjectBounds(0)) 
            myY = myAlignmentArray(5) - ((myObjectBounds(3) - myObjectBounds(1)) / 2) 
        Case myBottomRight 
            myX = myAlignmentArray(2) - (myObjectBounds(2) - myObjectBounds(0)) 
            myY = myAlignmentArray(3) - (myObjectBounds(3) - myObjectBounds(1)) 
        Case myXCenter 
            myX = myAlignmentArray(4) - ((myObjectBounds(2) - myObjectBounds(0)) / 2) 
            myY = myObjectBounds(1) 
        Case myYCenter 
            myX = myObjectBounds(0) 
            myY = myAlignmentArray(5) - ((myObjectBounds(3) - myObjectBounds(1)) / 2) 
        Case myXYCenter 
            myX = myAlignmentArray(4) - ((myObjectBounds(2) - myObjectBounds(0)) / 2) 
            myY = myAlignmentArray(5) - ((myObjectBounds(3) - myObjectBounds(1)) / 2) 
        Case Else 
    End Select 
    myObject.Move Array(myX, myY) 
Next 
End Sub 




Thanks,

Ole
Technical Details
Message-ID: <1de74227.0@WebX.la2eafNXanI>
Path: arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!sn-xit-02!sn-xit-01!sn-xit-08!supernews.com!newsswitch.lcs.mit.edu!logbridge.uoregon.edu!news.stealth.net!news.stealth.net!newsfeed.freenet.de!eusc.inter.net!newsfeed01.sul.t-online.de!t-online.de!WebX
References: <1de74227.-1@WebX.la2eafNXanI>

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: f939a,e41d12bc2e370c81
X-Google-Attributes: gidf939a,public
X-Google-ArrivalTime: 2003-01-07 13:38:26 PST
From: "Olav Kvern" <okvern@adobe.com>
Path: arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!sn-xit-02!sn-xit-01!sn-xit-08!supernews.com!newsswitch.lcs.mit.edu!logbridge.uoregon.edu!news.stealth.net!news.stealth.net!newsfeed.freenet.de!eusc.inter.net!newsfeed01.sul.t-online.de!t-online.de!WebX
Newsgroups: adobe.scripting.indesign
Subject: Re: To align Objects (VB)
Message-ID: <1de74227.0@WebX.la2eafNXanI>
Date: Tue, 7 Jan 2003 13:12:21 -0800
References: <1de74227.-1@WebX.la2eafNXanI>
Lines: 169
MIME-Version: 1.0
Content-Type: text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding: 8bit