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

InDesign Script: How to select all items simulating CTRL+A

Explorer ,
Nov 24, 2019 Nov 24, 2019

I have this....it is working where it copies all objects on PAGE 1 only and pastes in the target.

I have a loop to go through all the pages (in this case 2 pages); but it is not going to Page 2 for some reason.

But I'm getting closer......

 

//HERE IS NOW THE CODE WORKING THAT SELECTS ALL OBJECTS ON PAGE 1 AND THEN COPIES AND PASTES THEM IN THE TARGET DOCUMENT
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 
var allPages = curDoc.pages;

//this is supposed to loop through the pages but only doing page 1
for (var i = 0; i < allPages.length; i++) {
var currentPage =curDoc.pages[i];// Find the active page.

ItemsOnPage = currentPage.pageItems.length; // Get the number of items on the page.

//var myPageItems = sourceLayer.pageItems;
//select items on curren page
for (var i=0; i<ItemsOnPage; i++) // Loop through every item.
{
currentPage.pageItems.everyItem().select();
}
}//now loop to next page....



app.copy(); // copies whatever is selected
inDoc.close(); //close the source document so i don't have to deal with trying to active the other document

myDoc = app.activeDocument; //target document now active
myDoc.activeLayer = myDoc.layers[0]; //active layer is now the newest.. layer 0
// paste in place for all unlocked layers, excepting source layer
app.pasteInPlace();

 

//and then it loop through opening up another document with 2 pages....

TOPICS
Scripting
4.2K
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 as a whole....as the duplicat and the geometric position didn't work with the Duplicate (and maybe because I'm going between documents).  Not sure. But this is what I did get to work.

  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];
...
Translate
Community Expert ,
Nov 24, 2019 Nov 24, 2019

The main problem is that you use the same variable in two loops:

 

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

 

for (var i=0; i<ItemsOnPage; i++) // Loop through every item.

Use a different letter for the second loop, though that might still not fix your problem, not sure.

Another thing is that you shouldn't really copy and paste when you can avoid it. Much better to use .duplicate(): it's quicker and more robust. Its use is illustrated in this script:

targetPages = app.documents[0].pages;
sourcePages = app.documents[1].pages;

for (i = 0; i < sourcePages.length; i++) {
  itemsOnPage = sourcePages[i].pageItems;
  for (j = 0; j < itemsOnPage.length; j++) {
    itemsOnPage[j].duplicate (targetPages[i]);
  }
}

Finally, when you show code, please use the code widget, it makes code easier to read.

P.

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 24, 2019 Nov 24, 2019

I'm having a big problem with the duplicate as that is what I originally used.

see one of my posted here:

https://community.adobe.com/t5/indesign/indesign-script-how-do-i-reference-a-page-layer-item-within-...

there are imsges there to show the problem.

 

when duplicated to the target document it reorders the page items.  I even tried copying one page item at a time but it still doesn't maintain the order.

 

so the only thing that works is if I do a manual Select All then Copy then Paste in Place. So I am writing code to simulate it and it works and keeps the order.

 

thx!!!!

heather 😄

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 24, 2019 Nov 24, 2019

Ah yes, duplicating to a page places the at the top-left corner. So record where the item is on the source page, then move it to the required position:

targetPages = app.documents[0].pages;
sourcePages = app.documents[1].pages;

for (i = 0; i < sourcePages.length; i++) {
  itemsOnPage = sourcePages[i].pageItems;
  for (j = 0; j < itemsOnPage.length; j++) {
    gb = itemsOnPage[j].geometricBounds;
    pos = [gb[1], gb[0]];
    duped = itemsOnPage[j].duplicate (targetPages[i]);
    duped.move (pos);
  }
}

 

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 as a whole....as the duplicat and the geometric position didn't work with the Duplicate (and maybe because I'm going between documents).  Not sure. But this is what I did get to work.

  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