12 Replies Latest reply on Mar 18, 2014 3:41 AM by Laubender

    [ID-CS5][JS] Pasting into a rectangle at same position

    johnD

      Hi,

       

      I am trying to make a script and have been successful to a point...

       

      I have content on a layer labeled "copy-layer"

      Also, rectangles and polygons marking areas of copy-layer on another layer called "export-layer"

      I want to copy all of the content from copy-layer inot each individual shape from  export-layer

      This will help me export selection(i.e. individual shape) to JPEG.

       

      I struggling to pasteinto() theses shapes and locate copied content at same position as it was on copy-layer.

       

      Any help will be appreciated.

       

      Thank You

       

       

      johnD

        • 1. Re: [ID-CS5][JS] Pasting into a rectangle at same position
          Jump_Over Level 5

          Hi,

           

           

          johnD wrote:

           

          ...

          I struggling to pasteinto() theses shapes and locate copied content at same position as it was on copy-layer.

          ...

           

          If your goal is to paste object in same position use object.duplicate() method, i.e.:

          mRectangle.duplicate(app.activeDocument.layers.item("export-layer"));
          

           

          Jarek

          • 2. Re: [ID-CS5][JS] Pasting into a rectangle at same position
            Laubender Adobe Community Professional & MVP

            @Jarek – yes.

             

            A "Paste Into" is only possible with the app methods:

             

            select()

            cut() OR copy()

            pasteInto()

             

            If you do that in the same document, there is a fair chance it will work.
            If done from document to document, sometimes InDesign is crashing (did some experiments recently with InDesign CS5.5 in this regard).

             

            Also, if it happens, that source and target are not on the same spread, the pasted into object would be not at the same relative page position as before.

             

            Just a simple test:

            A double sided document with 3 pages starting on a right page.

            Two simple objects on page 1, a rectangle and an oval, nothing else.

             

            See screen grabs:

             

            RectangleAndOvalOnSameSpread.png

             

            After Paste In Place oval to rectangle:

             

            AfterThePasteInPlace_SameSpread.png

             

             

             

            //Working example:
            //One document with one rectangle and one oval on the SAME spread!
            
            var myDoc = app.documents[0];
            
            app.select(null);
            app.select(myDoc.ovals[0]);
            app.cut(); //could also be app.copy()
            app.select(null);
            app.select(myDoc.rectangles[0]);
            app.pasteInto();
            app.select(null);
            

             

            The result is as expected.

             

            Now move the rectangle to page 3 with the same position relative to the page.

            Do the test again.

             

            RectangleAndOvalOnDifferentSpreads.png

             

            The result is quite different:

             

            AfterThePasteInPlace_DifferentSpread.png

             

            We now have to move the oval to its former relative position.

            Or we had to move the rectangle to the spread of the oval and after pasteInto move it back to its own spread…

             

            And you could change the coordinate system of the rulers from by page to by spread or to by spine. No difference.

            If the two objects are on different spreads you'll get something like in the last screen grab.

             

            Uwe

            • 3. Re: [ID-CS5][JS] Pasting into a rectangle at same position
              Laubender Adobe Community Professional & MVP

              And on: even if both objects are on the same page, in the moment  one of the objects do not overlap* the other, with Paste Inside, the position of the pasted object will change to center of the container.

               

              *It remains to be seen, what that does exactly mean.

               

              So in any case it's best to write the position of the object that should be pasted inside into a variable and move the object after the paste to that position…

               

               

              Uwe

              • 4. Re: [ID-CS5][JS] Pasting into a rectangle at same position
                Marc Autret Level 4

                Thanks Uwe. Good to know.

                 

                @+

                Marc

                • 5. Re: [ID-CS5][JS] Pasting into a rectangle at same position
                  johnD Level 1

                  All of you who who posted Thank for opting to help.

                   

                  Sorry was busy at work for the last two weeks

                   

                  Let me post my script and explain again what I want to do and what really happens,

                  @Laubender has placed a finger on the problem,

                  but am not able to resolve my issue

                   

                  I have a page layout

                  Layer1.jpg

                   

                  The sky blue content is on a master layer

                  The orange polygons are the ones which mark the content which i want to export to jpeg (they are on "Layer 1"). They also have a style applied "exportStyle"

                  Layer 1 concists of images shapes and text.

                   

                   

                   

                  What I want to finally arrive at is

                   

                  exportLayer.jpg

                  Each orange bordered polygon moved to export-layer.

                  Then all items from Layer 1 pasteInto() the orange bordered polygons (sort of like appling a mask in photoshop).

                  But the contents position should be same as it was on Layer 1.

                  This would enable me to select each of the orange bordered polygons individually and export them to JPEG.

                  Then delete the copy & export layers and save document.

                   

                  What really happens is

                  pasteInto.jpg

                   

                  The Layer 1 items are not in same position.

                  They all align to the polygon co-ordinates.

                   

                  I did try to move the pasted contents to the geometricBounds I acquired before pasteInto(),

                  but some elements disappeared.

                   

                  Below is my script

                   

                  ===================================================================================

                  var myFile = app.activeDocument;

                   

                  //-----if layer does not exist creates it -------------------------------

                  function GetLayer(myFile,name){

                    var layer = myFile.layers.item(name);

                    if(!layer.isValid){

                      layer = myFile.layers.add({name:name});

                    }

                    return layer;

                  }

                   

                  //------------------------------------------------------------------------------

                  var shapes = myFile.pages[0].allPageItems;                   

                  var exportLayer = GetLayer(myFile,"E-Shape-Layer");

                  var copyLayer = GetLayer(myFile,"Copy-Page-Layer");

                   

                   

                  //------ filters out all items who do not have the style 'exportStyle' applied to it -------

                  for (var i=0; i<shapes.length; i++)

                  {

                      if (shapes[i].appliedObjectStyle.name === "exportStyle")

                      {

                  shapes[i].itemLayer=exportLayer;

                      }

                  }

                   

                   

                   

                  //---------------------------------------------------------------------------------------- --------

                   

                   

                  var appliedMastr=myFile.pages[0].appliedMaster.name; //gets the master applied to the first page

                  var mastrPageContent = myFile.masterSpreads.itemByName(appliedMastr).pageItems;   //get items of the applied master

                  var mainWorkedLayerItems = myFile.layers.itemByName("Layer 1").pageItems;              //get  items of the main layer

                   

                  app.select(NothingEnum.nothing);

                   

                  mastrLayerItemsGroup = myFile.groups.add(mastrPageContent.everyItem());                  //add the applied master items into a group

                  workdLayerItemsGroup= myFile.groups.add(mainWorkedLayerItems.everyItem());           //add the main layer items into a group

                   

                   

                  mastrLayerItemsGroup.duplicate(copyLayer);                                                                   //duplicate the master items group to copy layer

                  workdLayerItemsGroup.duplicate(copyLayer);                                                                  //duplicate the main layer group to copy layer

                   

                   

                  copyLayerItems=myFile.layers.itemByName("Copy-Page-Layer").pageItems;                  //get  items of the copy layer

                  copyLIgroup=myFile.groups.add(copyLayerItems.everyItem());                                        //add the copy layer items into a group

                   

                   

                  shapes2=myFile.layers.itemByName("E-Shape-Layer").allPageItems;                              //get all items on export layer (that will be all the polygons )

                   

                   

                  //------- cycles through all shapes on export layer and  pasteInto() the grouped copy layer items in them---------

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

                  app.select(NothingEnum.nothing);

                      shapes2[i].select();

                      app.pasteInto();

                  }

                   

                  ===================================================================================

                   

                  Any and all help is appreciated

                   

                  Thanks!

                   

                   

                  johnD

                  • 7. Re: [ID-CS5][JS] Pasting into a rectangle at same position
                    Laubender Adobe Community Professional & MVP

                    @John – ah, you changed your comment while I was writing my comment…

                     

                    Uwe

                    • 8. Re: [ID-CS5][JS] Pasting into a rectangle at same position
                      johnD Level 1

                      @Uwe

                       

                      That is exactly what I want to do (with reference to your question in post 6).

                       

                      Thank You.

                       

                      johnD

                      • 9. Re: [ID-CS5][JS] Pasting into a rectangle at same position
                        Laubender Adobe Community Professional & MVP

                        @John – would the scenario you lined out with your script work, if you do it without the script, just working with UI commands?

                         

                        If you suspect that the position of the pasted groups will change, you'll have to get the x/y position of the original group still unpasted and move() the group pasted inside to that position.

                         

                        That could be done in your last for loop.

                         

                        Not tested:

                         

                        for(var i=0; i<shapes2.length; i++){
                        
                        app.select(null);
                        
                            shapes2[i].select();
                            app.pasteInto();
                            //Move group inside shapes2[i]:
                            shapes2[i].groups[0].move([copyLIgroup.geometricBounds[1],[copyLIgroup.geometricBounds[0]]);
                            //Maybe a good idea:
                            app.select(null);
                        
                        }
                        

                         

                        Another comment on your output file format: I hope you are aware that JPEG does not support transparant pixels. So exporting an object to JPEG that is not a regular rectangle will get you some white pixels around its shape.

                         

                        Uwe

                        • 10. Re: [ID-CS5][JS] Pasting into a rectangle at same position
                          johnD Level 1

                          @Uwe

                           

                          Thanks

                           

                          @John – would the scenario you lined out with your script work, if you do it without the script, just working with UI commands?

                           

                          Yes I am able to do all this in the UI.

                           

                          Getting the geometricBounds of Original Group Did the trick.

                           

                          Shortly I will paste my entire script.

                           

                          Thanks

                           

                          johnD

                          • 11. Re: [ID-CS5][JS] Pasting into a rectangle at same position
                            johnD Level 1

                            Hi

                             

                            Thanks All for your time help, especially Uwe

                             

                            Here is what i have worked out.

                            Basically broken the operations to 2 scripts.

                            First to select shapes and run script. This will push selected shapes to the export layer.

                            Second, when ready to do the actual exporting

                             

                            // ===========SCRIPT 1: MOVING SELECTED SHAPES TO E-Shape-Layer =====

                            var myFile = app.activeDocument;

                            var myDocument = app.documents.item(0);

                             

                            function GetLayer(myFile,name){

                              var layer = myFile.layers.item(name);

                            if(!layer.isValid){   layer = myFile.layers.add({name:name});   }

                                else{

                            myFile.layers.itemByName("E-Shape-Layer").remove();

                                        layer = myFile.layers.add({name:name});

                                        }

                              return layer;

                            }

                             

                            function GetStyle(myFile,name){

                              var style = myFile.objectStyles.item(name);

                                if(!style.isValid){

                                    style = myFile.objectStyles.add({name:name});

                                 }

                             

                              return style;

                            }

                            //---------------------------------------------------------------------------------------- -----------------------------------------

                                exportLayer = GetLayer(myFile,"E-Shape-Layer");

                                app.cut();

                             

                            myDocument.activeLayer = "E-Shape-Layer";

                            app.pasteInPlace();

                             

                                myStyle = GetStyle(myFile,"exportStyle");

                            myStyle.strokeColor=myFile.swatches.itemByName("PT_Gold");

                            myStyle.strokeWeight=1;

                             

                                var shapes = myFile.layers.itemByName("E-Shape-Layer").allPageItems;

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

                            shapes[i].applyObjectStyle(myStyle);

                                }

                            myFile.layers[exportLayer.index].visible=true;

                            myFile.layers[exportLayer.index].locked=true;

                            myFile.layers[exportLayer.index].printable=false;

                             

                            myDocument.activeLayer = "Layer 1";

                             

                            // ===========SCRIPT 2: EXECUTING EXPORT===========================

                            //---------------------------------------------------------------------------------------- -------------

                            var myFile = app.activeDocument;

                            var fileNm = myFile.name.replace(/.[^.]+$/,'');

                            var fileExt= myFile.name.replace(/^.*\./,'');

                             

                            //-----if layer does not exist creates it -------------------------------

                             

                            function GetLayer(myFile,name){

                              var layer = myFile.layers.item(name);

                              if(!layer.isValid){

                                layer = myFile.layers.add({name:name});

                              }

                              return layer;

                            }

                             

                            function javascript_abort()

                            {

                               alert('You did not enter Page Number! \n Process Terminated!!!');

                               exit(0);

                            }

                            var FolderNm = Folder("/d/000000/JPEG");*/

                            //---------------------------------------------------------------------------------------- -------------

                            var pgName=""; pgName=prompt("Please Enter PAGE NAME to export",fileNm);

                            var pgNum=""; pgNum=prompt("Please Enter Page Number to export",);

                             

                             

                            if(isNaN(pgNum)){ javascript_abort(); }

                             

                            var exportLayer = myFile.layers.itemByName("E-Shape-Layer");

                            if(exportLayer.isValid){

                             

                                    var bkupLayer = GetLayer(myFile,"Bkup-Export-Layer");

                                    var copyLayer = GetLayer(myFile,"Copy-Page-Layer");

                                    var shapes = myFile.layers.itemByName("E-Shape-Layer").allPageItems;   

                                   

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

                            shapes[i].duplicate(bkupLayer);

                                    }

                             

                                    var appliedMastr=myFile.pages[pgNum-1].appliedMaster.name;

                                    var mastrPageContent = myFile.masterSpreads.itemByName(appliedMastr).pageItems;

                                    var mainWorkedLayerItems = myFile.layers.itemByName("Layer 1").pageItems;

                             

                            app.select(NothingEnum.nothing);

                             

                                    mastrLayerItemsGroup = myFile.groups.add(mastrPageContent.everyItem());

                            workdLayerItemsGroup = myFile.groups.add(mainWorkedLayerItems.everyItem());

                            mastrLayerItemsGroup.duplicate(copyLayer);

                            workdLayerItemsGroup.duplicate(copyLayer);

                             

                            copyLayerItems=myFile.layers.itemByName("Copy-Page-Layer").pageItems;

                            copyLIgroup=myFile.groups.add(copyLayerItems.everyItem());

                            copyLIgroup.name = "copyGroupNm";

                                    var gBounds=myFile.groups.itemByName("copyGroupNm").geometricBounds;

                             

                                    app.select(myFile.groups.itemByName("copyGroupNm"));

                                    app.copy();

                                   

                            myFile.layers[exportLayer.index].locked=false;

                             

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

                            app.select(null);

                             

                            shapes[i].select();

                            app.pasteInto();

                            shapes[i].groups[0].move([gBounds[1],gBounds[0]]);

                             

                                        var imgno="";

                                            if(i<10){ imgno=".0"+i;}

                                            else{ imgno="."+i;}

                            shapes[i].exportFile(ExportFormat.JPG, File(FolderFB + "/" + fileNm + imgno + i + ".jpg"), false);

                             

                            app.select(null);

                                    }

                               

                            myFile.layers.itemByName("Copy-Page-Layer").remove();

                            myFile.layers.itemByName("E-Shape-Layer").remove();

                            myFile.layers[bkupLayer.index].visible=true;

                            myFile.layers[bkupLayer.index].locked=false;

                            myFile.layers[bkupLayer.index].printable=false;

                             

                            myFile.layers[bkupLayer.index].name = "E-Shape-Layer";

                            //workdLayerItemsGroup.ungroup();

                             

                                    for(var i=0; i<myFile.groups.length; i++){

                            myFile.groups[i].ungroup();

                                    }

                             

                            alert("DONE!!");

                             

                            }

                            else{

                            alert("No export areas Marked and Applied!");   

                            }

                             

                            @Uwe

                            ran into a small problem when I was testing these scripts.

                            If I have two pages in document, it will give me an error.

                            Error No 30477, error string: invalid parameter.

                            The line causing the error : workdLayerItemsGroup= myFile.groups.add(mainWorkedLayerItems.everyItem());

                            Any guidance to proceed and resolve scenario is appreciated.

                             

                            Thanks!

                             

                            johnD

                            • 12. Re: [ID-CS5][JS] Pasting into a rectangle at same position
                              Laubender Adobe Community Professional & MVP

                              @John – without looking deep into ypur code, I think the error is due to the fact that you cannot group objects beyond spreads.

                               

                              A layer is not restricted to a page or a spread. It is document wide*. So you need to build the group from all pageItems on a spread and using that.

                               

                              * The masterSpread is an exception. If you ask for myLayer.pageItems.everyItem() you'll get all first level pageItems on every spread except the ones on masterSpreads.

                               

                              Uwe