Skip to main content
Anhelme
Participant
July 7, 2014
Answered

Select all content of indesign document for attaching script

  • July 7, 2014
  • 2 replies
  • 1629 views

I have a script that does some useful operation for my document. But it always need have selected part of document, like

if( app.selection.length > 0 ) ...

Could you ask me how can I select all pages from indesign document. I can have a lot of pages. And I have a lot of .indd files
I tried use app.activeDocument.pages.item(0).allPageItems; but it isn't effect even for page =(

Maybe I can use 'for' loop but maybe there is some function for selecting all document, not only one page

Thanks

This topic has been closed for replies.
Correct answer Dave_Saunders

Of course:

myPages = app.activeDocument.pages.everyItem().getElements(); // array of references to pages of document

for (var k = myPages.length - 1; k >= 0; k--) {

   myItems = myPages.allPageItems;

   for (var j = myItems.length - 1; j >= 0; j--) {

       processItem(myItems);

   }

}

This processes everything from back to front which is usually the safe approach and is potentially a little bit quicker than going the other way.

You still need to write the processItem function.

Dave

2 replies

Participant
December 3, 2014

Hi

I have a problem with this script

It does not work with element outside of the page:

ELEMENTS OUTSIDE THE PAGE:(does not get all elements..)

ELEMENTS_INDIDE_THE_PAGE(working)

Thanks

Participant
December 3, 2014

It is look like I found the solution:

myItems = app.activeDocument.pageItems.everyItem().getElements();

for (var k = myItems.length - 1; k >= 0; k--) {

    $.write(myItems);

}

Inspiring
July 7, 2014

A script does not have to make a selection to operate on objects, and indeed, you can't select beyond either the current spread or current text range. Selecting is what you have to do to use the user interface but a script can operate on any objects you can address ... as you seem to understand from the snippet of code you provided.

app.activeDocument.pages.item(0).allPageItems should have given you an array of all the page items on the first page of the active document, but because it's an array you have to loop through the items working on each one:

myItems = app.activeDocument.pages[0].allPageItems;

for (var j = myItems.length - 1; j >= 0; j--) {

   processItem(myItems);

}

where processItem is a function that does whatever you want to do to each item.

Dave

Anhelme
AnhelmeAuthor
Participant
July 7, 2014

Can I use 'for' loop for iterating all pages in document? How can I get number of pages for this operation, tell me please

Dave_SaundersCorrect answer
Inspiring
July 7, 2014

Of course:

myPages = app.activeDocument.pages.everyItem().getElements(); // array of references to pages of document

for (var k = myPages.length - 1; k >= 0; k--) {

   myItems = myPages.allPageItems;

   for (var j = myItems.length - 1; j >= 0; j--) {

       processItem(myItems);

   }

}

This processes everything from back to front which is usually the safe approach and is potentially a little bit quicker than going the other way.

You still need to write the processItem function.

Dave