12 Replies Latest reply on Apr 27, 2009 6:54 AM by Kasyan Servetsky

    Document.Distribute example

    Coully

      Hello

       

      Does anyone have an example of the document.distribute method.? Specifically the construct of the Objects parameter?

       

      What I am trying to do is actually execute this via C# and COM Interop. Everything else is going fine with some casting but I cant seem to build the 'Objects' part. I know this is a scripting thread and that is what I am asking, how do you populate the Objects object using the add method or with a direct casting? This is C# but you get the idea of what I am trying to achieve, the foreach is my poor attemt to populate the Objects group.

       

          InDesign.Application app = (InDesign.Application)COMCreateObject("Indesign.Application");

                      app.Open(@"c:\group.indd", true);

                      InDesign.Document doc = app.ActiveDocument;

                      InDesign.Page page = (InDesign.Page)doc.Pages[1];

                      if (page.Groups.Count > 0)

                      {

                          Group G = (Group)page.Groups[1];

                         

                          object AlignDistBounds = new object();

                          object UseDistMeasurement = new object();

                          object AbsoluteDistMeasurement = new object();

       

                          Objects objs = null;

       

                          int iu = 1;

                        

                         foreach( TextFrame t in (TextFrames)G.TextFrames)

                          {

                             

                              object o = new object();

                              o = (object)t;

                              objs.Add(o, iu);

                              iu++;

                          }

                        

       

                          doc.Distribute(objs, idDistributeOptions.idVerticalSpace, AlignDistBounds, UseDistMeasurement, AbsoluteDistMeasurement);

       

       

      Any help greatly appreciated

       

      Mark

        • 1. Re: Document.Distribute example
          Kasyan Servetsky Level 5

          myDocument.distribute(myObjects, DistributeOptions.VERTICAL_SPACE, AlignDistributeBounds.SPREAD_BOUNDS, false);

          • 2. Re: Document.Distribute example
            Coully Level 1

            Thanks for your quick response Kasyan,

             

            You've posted the script version of the distribute method, but could you tell me how to make up the myobjects part? In the API this in an 'Objects' object but I need to know how you would add for example; All the textframes in a group?

             

            Thanks

             

            Mark

            • 3. Re: Document.Distribute example
              Kasyan Servetsky Level 5

              This groups all text frames on the active spread:

              var mySpread = app.activeWindow.activeSpread;
              var myTextFrames = mySpread.textFrames;
              var myGroup = mySpread.groups.add(myTextFrames);

              • 4. Re: Document.Distribute example
                Kasyan Servetsky Level 5

                I must misunderstood your question.

                For example, let's distribute all text frame on the active spread vertically:


                var myDoc = app.activeDocument;
                var mySpread = app.activeWindow.activeSpread;
                var myTextFrames = mySpread.textFrames;
                myDoc.distribute(myTextFrames, DistributeOptions.VERTICAL_SPACE, AlignDistributeBounds.SPREAD_BOUNDS, false);

                 

                Te 1st parameter is alignDistributeItems: The page items to align or distribute (PageItem)  – in this script the collection of text frames.

                • 5. Re: Document.Distribute example
                  Coully Level 1

                  Thanks Kasyan, that's great. That answered that one mate and I'm nearly there!

                   

                  I am trying to automate the alignment of all the items within a group, is this possible in the same way. What I mean is -  if my group contains a mixture of rectangles and textframes do I have to add them individually. I would like to be able to add to the collection manually.. Excuse my code, but something like this?


                  var myDoc = app.activeDocument;
                  var myPage = myDoc.Pages[1];

                  var myPageItems = myPage.Groups[1].TextFrames

                  var myRectangles = myPage.Groups[1].Rectangles

                  for(int i = 1; i <= myPage.Groups[1].Rectangles.Count; i++)

                  {

                       myPageItems.Add(myRectangles[i], index??...not sure);

                  }


                  myDoc.distribute(myPageItems, DistributeOptions.VERTICAL_SPACE, AlignDistributeBounds.SPREAD_BOUNDS, false);

                   

                  Thanks for any help.

                   

                  Mark

                  • 6. Re: Document.Distribute example
                    Kasyan Servetsky Level 5

                    I don't think you need to create a group. Instead create an array and add all page items you want to it:

                    var myDoc = app.activeDocument;
                    var myPage = myDoc.pages[0];
                    var myTextFrames = myPage.textFrames;
                    var myRectangles = myPage.rectangles;
                    var myItems = [];

                     

                    for (i = 0; i < myTextFrames.length; i++){
                        myItems.push(myTextFrames[i]);
                    }

                     

                    for (j = 0; j < myRectangles.length; j++){
                        myItems.push(myRectangles[j]);
                    }

                     

                    myDoc.distribute(myItems, DistributeOptions.VERTICAL_SPACE, AlignDistributeBounds.SPREAD_BOUNDS, false);


                     

                    1 person found this helpful
                    • 7. Re: Document.Distribute example
                      Coully Level 1

                      Kasyan - thanks mate that's superb.

                       

                      The group thing was part of my automation. What I was trying to achieve was; any items grouped by whoever created the template and that were script labeled,  could have a set of rules applied to it. One of which being the doc.distribute.

                       

                      So one of my user/designers creates a indesign template, groups a number of rects or text frames for alignment, my service populates template with data and then deals with any overflow or alignment that is required.

                       

                      I'm doing this from c# and I am very close to achieving this apart from the fact that I can't populate the myItems object, In C#, the distribute method expects myItems be of type Indesign.Objects and with everything else I have been able to cast / multi cast to achieve this but am struggling with this one. I would post on the Indesign Server part of the forum but I'm not sure this could be answered?

                       

                      var myDoc = app.activeDocument;

                      var myPage = myDoc.pages[0];

                      var myTextFrames = myPage.textFrames;

                      var myRectangles = myPage.rectangles;

                      var myItems = [];

                       

                      for (i = 0; i < myTextFrames.length; i++){

                          myItems.push(myTextFrames[i]);

                      }

                       

                      for (j = 0; j < myRectangles.length; j++){

                          myItems.push(myRectangles[j]);

                      }

                       

                      myDoc.distribute(myItems, DistributeOptions.VERTICAL_SPACE, AlignDistributeBounds.SPREAD_BOUNDS, false);

                      • 8. Re: Document.Distribute example
                        Kasyan Servetsky Level 5

                        So, you have one group with text frames and another one with rectangles on the page. And you want to ungroup both of them and distribute all text frames and rectangles. Right?

                        • 9. Re: Document.Distribute example
                          Coully Level 1

                          Sorry mate, no, I have one group with a combination of text frames and rectangles in.

                           

                          My problem is now c# and casting related, your script was a great help but I know need to do exactly the same in c#.

                           

                          myDoc.distribute(myItems, DistributeOptions.VERTICAL_SPACE, AlignDistributeBounds.SPREAD_BOUNDS, false);

                           

                           

                          I've attached a screenshot which should be clearer. Sorry if this is on the wrong thread now.

                           

                          Thanks

                           

                          Mark

                          • 10. Re: Document.Distribute example
                            Kasyan Servetsky Level 5

                            The screenshot is not accessible. Could you e-mail it to me?

                            • 11. Re: Document.Distribute example
                              Coully Level 1

                              Ok - so this takes a group and vertically aligns all the textframes or rectangles within it.

                               

                              var myFile = new File("/c/group.indd");


                              var myDocument = app.open(myFile);

                              var myPage = myDocument.pages.item(0);

                              var myGroup = myPage.groups[0];


                              var myTextFrames = myGroup.textFrames;

                              var myRectangles = myGroup.rectangles;

                              var myItems = [];


                              for (i = 0; i < myTextFrames.length; i++){

                                  myItems.push(myTextFrames[i]);

                              }


                              for (j = 0; j < myRectangles.length; j++){

                                  myItems.push(myRectangles[j]);

                              }


                              myDocument.distribute(myItems, DistributeOptions.VERTICAL_SPACE, AlignDistributeBounds.ITEM_BOUNDS, true);


                              result = myItems.length;


                              result;

                               

                              Kasyan / all - if you know how to achieve this from C# it would be greatly appreciated. .NET 4.0 contains 'dynamics' allowing more interaction with loosely typed stuff, but until it is released I'd like to crack this. I know Ive been going on about this sorry!

                               

                              C#

                              InDesignServer.Objects objs;

                              objs = ??? How do you build the 'Objects' object?

                              doc.Distribute(objs, idDistributeOptions.idVerticalSpace, AlignDistBounds, UseDistMeasurement, AbsoluteDistMeasurement)

                               

                              Thanks Mark

                              • 12. Re: Document.Distribute example
                                Kasyan Servetsky Level 5

                                 

                                I don’t know C# at all, but from what I read about it in this forum, it seems that it does not work quite well with InDesign.