6 Replies Latest reply on May 24, 2011 3:30 AM by John Hawkinson

    move all the items of a layer to new page?

    zyberkid Level 1

      hi,

       

      is it possible to move all the items in the layer to move to the new page with the same coordinates in the page 1

       

      eg:

      i have two layer named "layer_page1"and one more layer named "layer_page2"

       

      i have 2 text frame objects , 1 rectangle objects in the "

      layer_page1" and 3 text frame objects, 3 rectangle objects in the "layer_page2"

       

      all the items are in the page.items(0).

       

      now i will add a new page to the document

      and i need to move all the items in the layer named "layer_page2" to the new page, i.e page.item(1). is it possible?

       

      if it is possible it will solve lot my efforts typing.. and verifying.

       

       

      your comments are highly appreciated.

        • 1. Re: move all the items of a layer to new page?
          John Hawkinson Level 5

          Yes, but.

           

          It sounds like you're trying to use scripting to to solve something you would rather do interactively:

          if it is possible it will solve lot my efforts typing.. and verifying.

          Why not hide the layer_page1, select all, cut, and then paste it onto the new page?

           

          Anyhow:

           

          and i need to move all the items in the layer named "layer_page2" to the new page, i.e page.item(1). is it possible?

           

          Instead of using the notation page.item(1), just use page[1]. It is shorter and more compact, yet equally clear, and it does not invite confusion with ...pageItems[1].

           

          Anyhow, something like (untested):

          var
            d=app.activeDocument,
            src=d.pages[0], dst=d.pages[1],
            layer=d.layers.itemByName("layer_page2"),
            i;
          for (i=0; i<src.pageItems.length; i++) {
            if (src.pageItems[i].itemLayer === layer) {
              src.pageItems[i].move(dst);
            }
          }
          
          1 person found this helpful
          • 2. Re: move all the items of a layer to new page?
            zyberkid Level 1

            works like charm,

             

            but overlapped items were not moved..

             

            looking into it whats strucking there.

            • 3. Re: move all the items of a layer to new page?
              John Hawkinson Level 5

              looking into it whats strucking there.

              Sorry, I don't understand this sentence (or what you mean by "strucking").

               

              Overlapping items are not moved? Interesting. Is it attempting to move() them? Perhaps you should try allPageItems instead of pageItems.

               

              Otherwise, please $.writeln() each item as you attempt to move it and perhaps check and see if it is even attempting to move them...

              • 4. Re: move all the items of a layer to new page?
                zyberkid Level 1

                some of the items are unable to move,

                when i ran the script again, some were moved.

                and then i ran the same script for the third time, then only all the contents in layer were moved to the new page.

                 

                i was just seeing this script need that much iterations , i am thinking this is influenced by the z-index(CSS style )

                 

                particulartly if contents are overlapped completly

                • 5. Re: move all the items of a layer to new page?
                  Laubender Adobe Community Professional & MVP

                  John,

                  hm, I don't think your untested script will work in a predictable way. Technically it will work a bit. Just tested it. But:

                   

                  1. if you loop from index 0 to the end, even if you loop from the end to index 0 you will mess the index of the pageItems when moving them to a different page

                  => I recommend grouping all pageItems on the specified layer, so that we have to move only one pageItem, the new group

                   

                  2. problem with move() to another page is, that you cannot retain the original page coordinates of the moved object, it will get 0,0 coordinates automatically

                  => we have to store the coordinates of the group first, then move, then apply the old coordinates

                   

                  3. in a real-world scenario we should deal with the possibility of locked pageItems

                   

                  4. Furthermore we have to make sure that the layer is not locked

                   

                  The following example will unlock all pageItems on that layer and will lock them after they are moved:

                   

                  //Move all objects on a layer of a page to a different page

                  var
                    d=app.activeDocument,
                    src=d.pages[0], dst=d.pages[1],
                    layer=d.layers.itemByName("Layer 2"),
                    i;

                  var myItemsToMove = new Array();
                  var myItemsLocked = new Array();

                  for (i=0; i<src.pageItems.length; i++) {
                      if (src.pageItems[i].itemLayer === layer) {
                          if(src.pageItems[i].locked){
                              myItemsLocked.push(src.pageItems[i].id);
                              src.pageItems[i].locked=false;
                            }
                          myItemsToMove.push(src.pageItems[i]);
                    }
                  }
                  //Check, if layer is locked:

                  if(layer.locked){
                      var layL = true;
                      layer.locked=false;
                      };

                  // create a group on the source page:
                  var newGroup = src.groups.add(myItemsToMove);
                  var gB = newGroup.geometricBounds;

                  //We have to move only ONE object, the new group
                  //To the destination page
                  newGroup.move(dst);
                  //To the old coordinates
                  newGroup.move(undefined,[gB[1],gB[0]]);

                  //Restore the state of the pageItems
                  //Ungroup them once:
                  newGroup.ungroup();

                  //Relock the former locked objects:
                  for(n=0;n<myItemsLocked.length;n++){
                      d.pageItems.itemByID(myItemsLocked[n]).locked=true;
                      };

                  //Relock layer if it was locked:
                  if (layL){layer.locked=true};

                   

                   

                   

                  Uwe

                  1 person found this helpful
                  • 6. Re: move all the items of a layer to new page?
                    John Hawkinson Level 5

                    Thanks, Uwe! You can see I got a bit sloppy.

                    This did get complex pretty quickly.

                    I don't use it very much, but this seems like a case where it would be a good idea to use try / finally (without catch); while I'm here, I really think it's a bad idea to declare vars inside block scope...it becomes just waaaaay too confusing that they get effectively hoisted to the enclosing function scope. Hopefully it doesn't make the changes too hard to see if I adjust:

                     

                    E.g.:

                    var layL, newGroup, gB;
                    if(layer.locked
                        layL = true;
                        layer.locked=false;
                    };
                    try {
                      // create a group on the source page:
                      newGroup = src.groups.add(myItemsToMove);
                      gB = newGroup.geometricBounds;

                      //We have to move only ONE object, the new group
                      //To the destination page
                      newGroup.move(dst);
                      //To the old coordinates
                      newGroup.move(undefined,[gB[1],gB[0]]);

                      //Restore the state of the pageItems
                      //Ungroup them once:
                      newGroup.ungroup();
                      //Relock the former locked objects:
                      for(n=0;n<myItemsLocked.length;n++){
                          d.pageItems.itemByID(myItemsLocked[n]).locked=true;
                      };
                    } finally {
                      //Relock layer if it was locked:
                      if (layL){layer.locked=true};
                    }

                     

                    Maybe that's too paranoid. Maybe it's not paranoid enough.