4 Replies Latest reply on Sep 23, 2015 9:39 AM by asdfdffhdfh

    Script to move a selection relative to spread bounds?


      I have a document with a spread that is far larger than the contents contained in the document.



      I have another template file with a spread that matches the size of the bounds of the all the selected objects. I need to open that template, paste the objects, have that pasted selection of objects align to the top left of the spread (aka pasteboard or work area?), and then export as .pdf and .eps.


      Due to lots of confusion regarding various coordinate systems, my ideal solution would be to not have to move anything at all, but instead, have the upper left corner of the pasted contents align with the upper left corner of the spread as it is pasted. Neither paste nor paste-in-place puts the contents where I need them. The objects are too low.



      Even if there is a solution to paste the information where it needs to go from the outset, I'd still like to know how to move things relative to the visible work area (I believe this is correctly referred to as the spread, but more commonly called the pasteboard? I just want to make sure I'm asking my question correctly so my intentions will be understood).


      My first try with the following line of code produces the result seen in the following image; only the background .eps object is moved, and it moves down.





      My second try is illustrated below; it also seems to only reposition the background image, but something is also moved very far down and to the right. It appears that it may be the same size and shape as the spread from which I copied the objects. The selection bounds were the correct size when I first pasted the object into the new document, until the line of code to change the geometric bounds was executed.


      app.selection[0].geometricBounds = [myPage.bounds[0]+myMargins.top,myPage.bounds[1]+myMargins.left,myPage.bounds[2]-myMargin s.bottom,myPage.bounds[3]-myMargins.right];



      Also, previous attempts (before I added the image of the tiger, the text to its immediate left, and a red rectangle behind the tiger) saw all the selected objects EXCEPT the background .eps image move up and into the proper position using the app.selection[0].geometricBounds statement.


      Any help would be very greatly appreciated. Here's the code and a screenshot of the objects on the layer that are involved.


      #target indesign-11.064  // CC 2015


      myDoc = app.activeDocument;

      myPages = app.activeDocument.pages;



      // 1. Select and copy everything on this document's activePage




      // 2. Open template file and paste

          whichExportTemplate = "FCILG";

          myTemplateFolder="/Users/User/Documents/!  Resources/ Templates/  Ted Signs/ Tags";

          sFileName= myTemplateFolder + "/" + whichExportTemplate + ".indd"

          sFileName= sFileName.replace (/%20/g, " ");

          newDoc = app.open(File(sFileName));





      // 3. POSITION IT

          var myPage = myActivePage;

          var myMargins = myPage.marginPreferences;


          app.selection[0].geometricBounds = [myPage.bounds[0]+myMargins.top,myPage.bounds[1]+myMargins.left,myPage.bounds[2]-myMargin s.bottom,myPage.bounds[3]-myMargins.right];


      //    app.selection[0].move([myPage.bounds[0]+myMargins.top,myPage.bounds[1]+myMargins.left]);


      // 4. Save the new file

      // 5. Close


        • 1. Re: Script to move a selection relative to spread bounds?
          Laubender Adobe Community Professional & MVP

          Would you mind grouping all page items on the page, that you have only one object to deal with after duplicating ( I'd prefer duplicate() over app.copy() / app.pasteInPlace() ) without selecting anything? Just address all page items on your source page to add a group and handle that single group object.


          Quick and dirty:


          //TWO InDesign documents are open
          //The SOURCE document is the ACTIVE one
          var myTargetDoc = app.documents[1];
          myTargetDoc.viewPreferences.rulerOrigin = RulerOrigin.PAGE_ORIGIN;
          myTargetDoc.zeroPoint = [0,0];
          //Group everything on the active page of your SOURCE document:
          var myActivePage = app.layoutWindows[0].activePage;
          var myGroup = myActivePage.groups.add( myActivePage.pageItems.everyItem().getElements() );
          //Target the second document for the duplicate of the group:
          var myDuplicate = myGroup.duplicate(myTargetDoc.pages[0]);
          //Move the duplicate:
          //Tidy up a bit:


          Hope, that helps.


          • 2. Re: Script to move a selection relative to spread bounds?
            asdfdffhdfh Level 1

            Thanks so much, Uwe; this is great. I can do just what I need with it. This is a far easier approach to grouping everything than I had been using, and changing the viewPreferences and zeroPoint options really did the trick. Now I'll learn more about those options and try to figure out how to save the viewPreference and zeroPoint settings so I can restore them after.

            • 3. Re: Script to move a selection relative to spread bounds?
              Laubender Adobe Community Professional & MVP

              Simply store them in a variable befor changing them and then reset them:



              //Store the actual ruler origin in a variable:
              var myTargetRulerOriginBEFORE = myTargetDoc.viewPreferences.rulerOrigin;
              //Change them::
              myTargetDoc.viewPreferences.rulerOrigin = RulerOrigin.PAGE_ORIGIN;
              //Do other things…
              //Finally restore the settings:
              myTargetDoc.viewPreferences.rulerOrigin = myTargetRulerOriginBEFORE;


              Of course there are more elaborate methods of storing settings into a new object, but this would be a good start for you.



              • 4. Re: Script to move a selection relative to spread bounds?
                asdfdffhdfh Level 1

                Thanks again, storing the status in variables to later restore is exactly what I needed.