Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

InDesign Script: Issue - copy layer items (using Duplicate), in target file layer items reordering

Explorer ,
Nov 22, 2019 Nov 22, 2019

When using this Script

//get the layer from the source file
var sourceLayer=inDoc.layers[0];
//make sure we have the correct larget layer
targetLayer=myStagingDoc.layers.itemByName(adorName);
//Copy layer from source to target
sourceLayer.pageItems.everyItem().duplicate(targetLayer);


When it pastes the items into the target document/layer, it is re-ordering the layer items.

I have tried "Remember Paste Layers", Locking Layers, Locking individual layer items.  

 

See the attached images.

The source file shows the correct order.

When pasted into the Target, you'll then see they are re-ordered.

If I do it manually:

Remember Paste Layers is turned on.

In source document, go to layer, CTRL+A to select all, CTRL+C to copy.

Go to target document, CTRL+V to paste and it works.

TOPICS
Scripting
2.3K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Explorer , Nov 26, 2019 Nov 26, 2019

This is what I ended up using.....and it copies every item, on every page, and pastes it in place, and keeps the order of all items (that the duplicate() did not do). 
😄

 

  var sourceLayer=inDoc.layers[0];
                 var targetLayer=myStagingDoc.layers.itemByName(adorName);

               //set active document.
               var myDoc = app.activeDocument;
               //probably the same thing....
               var curDoc = app.documents[0];
               //get pages in current doc
 
...
Translate
Community Expert ,
Nov 23, 2019 Nov 23, 2019

Hi Heather,

I think you have to loop through all spreads of the document. You cannot do the dups in one go and get this right.

Not if you do sourceDoc.sourceSpread.everyItem().duplicate( targetDoc.targetSpread ).

 

This should work:

 

Since InDesign CS5 page items are essentially organized in spreads.

For every spread in your source document loop the allPageItems array.

 

Two conditions must be met for an item you want to duplicate:

1. parent of the item must be the spread you are currently looping

( means, the item is not part of a nested structure like a group or anchored or … )

2. itemLayer of the item must be the layer you like to target

 

Duplicate the items in reverse stacking order.

 

What I do not know: Do you want to duplicate items from sourceDoc.spread N to targetDoc.spread N ?

Or do you want to duplicate all items to one spread of the target document. Then stacking order is a problem in itself.

 

Regards,
Uwe Laubender

( ACP )

 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Nov 23, 2019 Nov 23, 2019

Still having an issue with this....

 

Here is the code for looping.

And you  can see in the screen shots, they are still not copying into the target in the expected order.

It's kinda crazy how it reorders the page items.....

 

The one with RED layers is the target document (in the wrong order).

The BLUE layers are the source in the expected order.

 

Code.pngSourceCopy.pngTargetCopy.png

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Nov 25, 2019 Nov 25, 2019

Hi Heather,

you do not use the allPageItems array of a spread and pick the right items from that.

You are simply using pageItems of a layer. And that is the wrong approach.

 

Sometimes you have to go the long way and checking all page items…

 

Regards,
Uwe Laubender

( ACP )

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Nov 25, 2019 Nov 25, 2019

Alternative:

If the stacking order is exactly in reverse compared to the original after duplicating the layers' page items with layer.pageItems.everyItem().duplicate( targetLayer ), then you could use a duplicated InDesign document of your source, remove all items in the duplicated source, then first duplicate all items of a distinct layer from doc Source to doc DupSource and then again on to doc Target to reverse the reversed order.

 

Regards,
Uwe Laubender

( ACP )

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Nov 25, 2019 Nov 25, 2019

Hi Heather,

your task is now spreading to several threads here in the forum.

You tried several approaches to solve the problem duplicating items from layer A to layer B in a different document.

 

Thank you for posting the one here in this thread using everyItem() this way:

sourceLayer.pageItems.everyItem().duplicate( targetLayer )

this is very elegant and powerful!

 

Its strength:

If the target document contains as much spreads as the source document all page items land on the right spread and on the right layer.

 

However there is one flaw:

The items are duplicated in reverse stacking order.

 

We can fix this easily if we provide a third document.

Why to provide a third document?

 

A to B ( order is reversed )

B to C ( order is reversed )

So A to C over B ( order is not reversed )

 

See into this code where I am using a duplicate of the target document as document B in between:

 

 

 

var sourceDoc = app.documents.itemByID( app.documents[0].id );
var targetDoc = app.documents.itemByID( app.documents[1].id );

var sourceLayerName = "SourceLayer" ;
var targetLayerName = "TargetLayer" ;

var sourceLayer = sourceDoc.layers.itemByName( sourceLayerName );
var targetLayer = targetDoc.layers.itemByName( targetLayerName );

var tempDoc = app.open
(
	targetDoc.fullName , true , OpenOptions.OPEN_COPY
);

var tempLayer = tempDoc.layers.itemByName( targetLayerName );

sourceLayer.pageItems.everyItem().duplicate( tempLayer );
tempLayer.pageItems.everyItem().duplicate( targetLayer );

tempDoc.close( SaveOptions.NO );

 

 

 

If your target document's layer already contains any elements then you have to remove them before you populate the layer in the temp document.

 

Regards,
Uwe Laubender

( ACP )

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Nov 26, 2019 Nov 26, 2019
LATEST

This is what I ended up using.....and it copies every item, on every page, and pastes it in place, and keeps the order of all items (that the duplicate() did not do). 
😄

 

  var sourceLayer=inDoc.layers[0];
                 var targetLayer=myStagingDoc.layers.itemByName(adorName);

               //set active document.
               var myDoc = app.activeDocument;
               //probably the same thing....
               var curDoc = app.documents[0];
               //get pages in current doc
               var allPages = curDoc.pages;
             
                //loop through the pages
                for (var i = 0; i < allPages.length;) { 
                         $.write("i = " + i+ "\n");
                        //var currentPage =curDoc.pages[i];// Find the active page.
                     
                         app.activeDocument.layoutWindows[0].activePage = app.activeDocument.pages[i];
                         var currentPage = app.activeDocument.pages[i];
                         //curDoc.pages[i];// Find the active page.
                         ItemsOnPage = currentPage.pageItems.length; // Get the number of items on the page.
                         $.write("ItemsOnPage= " + ItemsOnPage + "\n");
                         //var myPageItems = sourceLayer.pageItems;
                         //select items on curren page
                         for (var j=0; j<ItemsOnPage; j++) // Loop through every item.
                         {
                                currentPage.pageItems.everyItem().select();
                         }
                        
                        //copy
                        app.copy(); // copies whatever is selected

                        //go to Staging doc -- make active document
                        myDoc = app.documents[1];
                        app.activeDocument = myDoc;
                        
                        //go to current page
                        app.activeDocument.layoutWindows[0].activePage = app.activeDocument.pages[i];

                        //paste in place for all unlocked layers, excepting source layer
                        app.pasteInPlace();

                        //set focus back to other document....
                        curDoc = app.documents[1];
                        app.activeDocument = curDoc;
                        i++;
                
                }//now loop to next page....

                 //close the current file/layer
                 inDoc.close(); 
                //**************************************************************************************

         }//now continue loop to next file...

 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines