3 Replies Latest reply on Feb 16, 2011 2:29 PM by John Hawkinson

    How to position a library asset on the document?

    Rums Level 1

      Hi,

      I have a library asset with an imagebox and a few textframes. I want multiple copies of them moved onto the document inside rectangles or grids. I ran a placeAsset call to place the library asset on the document. I also got feeding data into the imageboxes and text frames working. I am wondering how to move them as a unit to different rectangles/grids inside the document? Also: is there a way to get the dimension of the entire library asset so that I can determine the number of grids or pages that need to be created in the document. Thanks a lot in advance for any help on this front.

       

      thanks,

        • 1. Re: How to position a library asset on the document?
          John Hawkinson Level 5

          placeAsset() behaves in kind of a funny way if there is an active selection. So you need to make sure there isn't one when you place the asset. Then you can just move the asset into place with .move(). You should be able to check its geometricBounds as well (or visibleBounds). Note that in your case it's an array so you may need to do multiple moves.

           

          Here's an example script that assumes the user has selected a bunch of articles. It then goes and places an asset ("T") from the library relative

          to each article. Note that it clears the selection first to avoid the above problem.

           

          function main() {
             var
                  selectionArray=[],
                  i;
          
          // Asset.placeAsset() requires there be no active selection, otherwise
          // it gives goofy results. So preserve the selection.
              for (i=0; i<app.selection.length; i++) {
                      selectionArray.push(app.selection[i]);
              }
              app.select(null); // Deselect everything
              // Loop and apply placeT() to all selections
              for (i=0; i<selectionArray.length; i++) {
                  if(selectionArray[i].constructor.name==="TextFrame"){
                      placeT(selectionArray[i]);
                  }
              }
              app.select(selectionArray);
          }
          
          function placeT(article){
              var
                  leading, p, t, x, y,
                  articleBounds=fromBounds(article.visibleBounds);
          ...
              p = article.paragraphs[2];
          ...
              // X pos is article's left edge plus its left inset (if any)
              x = articleBounds.x1+article.textFramePreferences.insetSpacing[0];
          ...
              leading = p.pointSize * p.autoLeading / 100; // xxx not sufficient
              // Y pos is the baseline of the correct graf plus two line heights
              y = p.baseline+(leading*2)/12;
          ...
              // Place the T
              t=app.libraries.item("Library.indl").
                  assets.item("T").placeAsset(app.activeDocument);
              // Move it into place.
              t[0].move([x, y]);
          ...
          }
          
          function fromBounds(gB) {
              return { y1: gB[0], x1: gB[1], y2: gB[2], x2: gB[3],
                  height: gB[2]-gB[0], width: gB[3]-gB[1]};
          }
          
          • 2. Re: How to position a library asset on the document?
            Rums Level 1

            thanks for the quick response John.

            My library has a complicated set of imageboxes and text frames layed out in a specific fashion. I was wondering if there is a way to group the placed items array (array we get after called placeAsset) as a group and move them as one unit within the bounds of a rectangle that I can draw on the document. Calculating the bounds on each placed items and moving them individually seems to defeat the purpose of having a library asset to me.  If this is possible, I can get the bounds on the rectangle, get bounds of the group and position the group into the rectangle.

             

             

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

            var myRectangle = myPage.rectangles.add();

            var placedLibraryAssets = libraryAsset.placeAsset (myDoc);
            // make a group of these placedLibraryAssets
            // get bounds of rectangle
            //move the group into the rectangle.

             

             

            I tried

            myPage.groups.add (placedLibraryAssets);

            var myGroup = myPage.groups.item(0);

            myGroup.move([x,y]);

             

            But it does not seem to work.

            • 3. Re: How to position a library asset on the document?
              John Hawkinson Level 5

              How does it fail? That's a critical component of any question, and one you haven't included. Since we're all volunteers here, please do make it easier for us to answer your questions!

               

              (I assume you do realize that you can group items before you place them in the Library, though of course it is much more robust for your script to handle ungrouped items).

               

              Anyhow, this code just worked for me:

               

              t=app.libraries.item("Library.indl").
                      assets.item("E").placeAsset(app.activeDocument);

              g=app.activeDocument.pages[0].groups.add(t);

              g.move([0,0]);

               

              I'm not sure why you are using myPage.groups.item(0). You should certainly use the group that is returned by the add() method. I would expect your way to work, but it's sloppy. (And apparently it does not work?)