13 Replies Latest reply on Aug 10, 2016 11:41 AM by Mnietek1

    how to duplicate frame content to another frame

    Mnietek1 Level 1

      I'd like to duplicate content of one frame to another frame. Content is not a text. It could be a graphic or group of objects.

      Is this possible?

        • 1. Re: how to duplicate frame content to another frame
          Laubender Adobe Community Professional & MVP

          If this is not for InDesign Server, you could use the ExtendScript methods select(), copy(), pasteInto() and move().
          Just as you do this in the UI.

           

          What is your version of InDesign?

           

          contentPlace() would be another option, also available for InDesign Server.

           

          InDesign and InDesign Server CS5.5 and above:

          If you want to cheat a bit, you could add() a new text frame to the target graphic frame, duplicate() the contents of the source frame, move() the duplicate to the right place above the target and anchor it by insertAnchoredObject() to the added text frame.

           

          What did you try so far with code?

           

          Regards,
          Uwe

          1 person found this helpful
          • 2. Re: how to duplicate frame content to another frame
            Peter Kahrel Adobe Community Professional & MVP

            Assuming that the two frames were named on the Layers panel, you can do this:

             

            from = app.documents[0].textFrames.item('from');
            to = app.documents[0].textFrames.item('to');
            from.parentStory.duplicate (LocationOptions.AFTER, to.insertionPoints[0]);
            

             

            Peter

            • 3. Re: how to duplicate frame content to another frame
              Laubender Adobe Community Professional & MVP

              Mnietek1 wrote:

              I'd like to duplicate content of one frame to another frame. …

              What kind of frames are you talking about?

               

              Source frame:

              Graphic frame with nested objects?

              Text frame with anchored objects?


              Target frame:

              Graphic frame with/without no nested objects?
              Text frame with/without anchored objects?

               

              Something else?

               

              Regards,
              Uwe

              • 4. Re: how to duplicate frame content to another frame
                Mnietek1 Level 1

                Thx for answers.

                I mean graphic frames - not text frames.

                Source graphic frame can contains just an image or group of objects.

                I'd like to copy whole content of one frame to another.

                 

                I thought there is faster method than use of copy() and pasteInto()

                 

                ___

                sorry for my English

                • 5. Re: how to duplicate frame content to another frame
                  Laubender Adobe Community Professional & MVP

                  … I thought there is faster method than use of copy() and pasteInto()

                  Without adding something, that is not inside the source graphic frame?
                  Like a group object or a text frame one can anchor something to?
                  Unfortunately no.


                  Otherwise you have to sample all possible properties with their respected values of the target object, do a duplicate of the source and apply the stored values to the duplicate. Plus remove the original target.

                   

                  But that would remove assumed unique properties like the id number of the object from all the id numbers of all objects in the document, would remove "private" data set by insertLabel() and 3rd party plugIns that is hard or impossible to gather…

                   

                  Regards,
                  Uwe

                  • 6. Re: how to duplicate frame content to another frame
                    Peter Kahrel Adobe Community Professional & MVP

                    Frames are rectangles or text frames. If you placed a group of images (any group, in fact) in a frame, then that frame is a text frame and you can duplicate the content using the code I posted above. An image is in a rectangle, and can be duplicated by re-placing it in the target rectangle:

                     

                    from = app.documents[0].rectangles.item('from'); 
                    to = app.documents[0].rectangles.item('to');
                    to.place (File(from.graphics[0].itemLink.filePath));
                    

                     

                    You might have to 'duplicate' scale and crop too.

                     

                    Anyway, I don't think there's any need to copy and paste.

                     

                    Peter

                    • 7. Re: how to duplicate frame content to another frame
                      Mnietek1 Level 1

                      pkahrel napisał(-a):

                       

                      Assuming that the two frames were named on the Layers panel, you can do this:

                       

                      1. from=app.documents[0].textFrames.item('from');
                      2. to=app.documents[0].textFrames.item('to');
                      3. from.parentStory.duplicate(LocationOptions.AFTER,to.insertionPoints[0]);

                       

                      Peter

                      I can't get this to work.

                      "If you placed a group of images (any group, in fact) in a frame, then that frame is a text frame"

                      Are you sure? I get error "Object is invalid" when I try to access parentStory property of frame which contains group of objects...

                       

                      script.jpg

                      • 8. Re: how to duplicate frame content to another frame
                        Laubender Adobe Community Professional & MVP

                        Here an example with two selected objects.

                        The blue one is the source and contains a rectangle, the target is the yellow circle.

                         

                        That's for InDesign CS5.5 or above since I am working with the order of selection with the code below.
                        First I selected the source, then I added the target to the selection by holding the SHIFT key and selected the target.

                        The script will use the mentioned app methods that work on selections.

                         

                        Before:

                         

                        Before-Source-Target-Selected.png

                         

                        After running the script:

                         

                        After-Source-Target-Selected.png

                         

                        Script:

                        You can undo the actions in one go.

                        Read the code, read the comments.

                         

                        Note:

                        If the target contains anything it will be replaced by the duplicated contents of the source.
                        Without warning!

                         

                        function main()
                        {
                            // Minimum check, there could be way more:
                            // Do nothing, if not two objects are selected:
                            if(app.selection.length != 2){return};
                           
                            // The first selected object is the source:
                            var source = app.selection[0];
                           
                            // Do nothing, if the source contains no object(s):
                            if(source.pageItems.length == 0){return};
                           
                            // The second selected object,
                            // that is added to the first selected one
                            // is the target:
                            var target = app.selection[1];
                           
                            // Duplicate the object inside the source
                            // That would free it from the source
                            var contents = source.pageItems[0];
                            var dupContents = contents.duplicate();
                        
                            app.select(dupContents);
                            app.cut();
                            app.select(target);
                            app.pasteInto();
                            app.select([source,target]);
                           
                            // If you want to remove the object inside the source
                            // do it now:
                            // contents.remove();
                        };
                        
                        app.scriptPreferences.userInteractionLevel = UserInteractionLevels.interactWithAll;
                        app.doScript
                            (
                           
                            main,
                            ScriptLanguage.JAVASCRIPT,
                            [],
                            UndoModes.ENTIRE_SCRIPT,
                            "Paste Into Target | SCRIPT"
                           
                            );
                        

                         

                        Regards,
                        Uwe

                        • 9. Re: how to duplicate frame content to another frame
                          Peter Kahrel Adobe Community Professional & MVP

                          Interesting different schools of thought: Uwe prefers copying and pasting, I always try to avoid that as long as possible.

                           

                          Mnietek -- Your screenshot shows that the 'to' frame is a rectangle, not a text frame.

                           

                          P.

                          • 10. Re: how to duplicate frame content to another frame
                            Laubender Adobe Community Professional & MVP

                            Hi Peter,

                            just an example, no school of thought. I'm really far from beeing docmatic here. ;-)

                             

                            Here an example by using a helper text frame that is added to the target.

                            I nested a rectangle into the target frame to make a point.

                             

                            Note: This time contents that possibly is nested in the target graphic frame will not be removed.

                            This example can be rewritten so that no selection is necessary. Would work with InDesign Server then.

                             

                            Before:

                            The Layers Panel is showing the structure of the selected graphic frames

                             

                            Before-UsingHelperTextFrame.png

                             

                            After:

                            A text frame was added to the target, the duplicated contents of the blue rectangle is anchored to that helper text frame.

                            And the original contents of the yellow target was not removed.

                            After-UsingHelperTextFrame.png

                             

                            Code:

                             

                            function main()
                            {
                                // Minimum check, there could be way more:
                                // Do nothing, if not two objects are selected: 
                                if(app.selection.length != 2){return};
                                
                                // The first selected object is the source:
                                var source = app.selection[0];
                                
                                // Do nothing, if the source contains no object(s):
                                if(source.pageItems.length == 0){return};
                                
                                // The second selected object, 
                                // that is added to the first selected one 
                                // is the target:
                                var target = app.selection[1];
                                
                                // Duplicate the object inside the source
                                // That would free it from the source
                                var contents = source.pageItems[0];
                                var dupContents = contents.duplicate();
                                
                                // Optional:
                                dupContents.name = "MagentaContentsDuplicated";
                                
                                // Add helper text frame to the target graphic frame:
                                var helperTextFrame = target.textFrames.add
                                (
                                    {
                                        name : "HelperTextFrame" , /* Optional */
                                        geometricBounds : target.geometricBounds ,
                                        strokeWeight : 0 ,
                                        fillColor : "None" ,
                                        strokeColor : "None"
                                    }
                                );
                                
                                // Anchor duplicate contents to the helper text frame:
                                dupContents.anchoredObjectSettings.insertAnchoredObject( helperTextFrame.insertionPoints[0] , AnchorPosition.ANCHORED );
                            
                                // Optional:
                                app.select([source,target]);
                                
                                // If you want to remove the object inside the source
                                // do it now:
                                // contents.remove();
                            };
                            
                            app.scriptPreferences.userInteractionLevel = UserInteractionLevels.interactWithAll;
                            app.doScript
                                (
                                
                                main, 
                                ScriptLanguage.JAVASCRIPT, 
                                [], 
                                UndoModes.ENTIRE_SCRIPT, 
                                "Anchor Dup Contents Into Target | SCRIPT"
                                
                                );
                            

                             

                             

                            Important note for users of InDesign CC 2015:

                             

                            Do not try this with a graphic frame, that is the contents of a graphic cell in a table.
                            Basically a rewritten version of the code above would work, but InDesign will crash, if you e.g. try to duplicate the page where the table is.

                            Or InDesign will crash, if you try to copy/paste the frame where the table is.

                             

                            Reason: The developers did not foresee that one can paste a text frame as part of a group frame into a graphic cell.
                            InDesign is prone to crash then. It's a time-bomb.

                             

                            Regards,
                            Uwe

                            • 11. Re: how to duplicate frame content to another frame
                              Peter Kahrel Adobe Community Professional & MVP

                              I don't think I suggested anywhere that you were dogmatic, Uwe:)

                              Not sure I follow you on the helper frame. I mean, I can follow the code, but I'm mystified why you use it.

                               

                              P.

                              • 12. Re: how to duplicate frame content to another frame
                                Laubender Adobe Community Professional & MVP

                                Hi Peter,
                                just an example.

                                 

                                Without a helper text frame it's hard to impossible to transfer a duplicate of the contents of a source (graphic frame) to the target (also a graphic frame).

                                 

                                What's left, at least as far I can tell, is the method contentPlace() one can try one's luck.

                                I did some tests with that some years ago. As far as I can remember I was only able to transfer a group of objects, not a single object without grouping it.

                                 

                                Will try that later.

                                But if you have a better method doing a transfer without using a helper object or doing the boring pasteInto(), I would be interested to hear from it.

                                 

                                Thanks,
                                Uwe

                                • 13. Re: how to duplicate frame content to another frame
                                  Mnietek1 Level 1

                                  Thanks for suggestions.

                                  I thought it's rather simple task but i was wrong