Skip to main content
Known Participant
December 9, 2016
Answered

group all items on page in a document

  • December 9, 2016
  • 4 replies
  • 3055 views

Hi,

I'm very new at this -

I have a script for

//group all on active spread

var doc = app.activeDocument;

var myItems = app.activeWindow.activeSpread.pageItems;

app.activeWindow.activeSpread.groups.add(myItems);

app.select(myItems);

app.select(app.activeWindow.activePage.allPageItems);

but I can't get it to work for the whole document.

I tried things like

var doc = app.activeDocument;

var myItems = app.activeDocument.pageItems;

app.activeDocument.groups.add(myItems);

but I can't figure it out

Thanks for any help

Vanessa

This topic has been closed for replies.
Correct answer Peter Kahrel

What exactly are you trying to achieve? When you say "but I can't get it to work for the whole document", you should realise that the items you want to group must be on the same spread. So if you want to work on the document, you probably want to group items per spread. Correct? If yes, use this:

spreads = app.documents[0].spreads.everyItem().getElements();

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

  if (spreads.pageItems.length > 1) {

    spreads.pageItems.everyItem().locked = false;

    spreads.groups.add (spreads.pageItems);

  }

}

Peter

4 replies

OutlineGAuthor
Known Participant
December 12, 2016

I see why the group cannot be set up the same as the ungroup script.

Thanks for all your help!! I still got a LOT to learn

OutlineGAuthor
Known Participant
December 12, 2016

var doc = app.activeDocument;

app.activeDocument.groups.everyItem().ungroup();

does ungroup all spreads in a doc but the opposite does not work?

Community Expert
December 12, 2016

Hi,

the "why" is the same as working within the UI.

everyItem().ungroup() is not the opposite of everyItem().group().


With document.groups.everyItem().ungroup() every single group in the document that is not nested is addressed and will be ungrouped. You are addressing not a single group.

Whereas document.groups.add() is always trying to add one single group.

Further:

You are doing: document.groups.add()

So you are working with the document.

You do not tell InDesign on what spread or page the group should be added. So InDesign tries to add the group on the first spread. And that will fail, if not all page items of the document do not exist on the first spread.

It will work if all page items you want to group are on the first spread.

Regards,
Uwe

Peter Kahrel
Community Expert
Peter KahrelCommunity ExpertCorrect answer
Community Expert
December 9, 2016

What exactly are you trying to achieve? When you say "but I can't get it to work for the whole document", you should realise that the items you want to group must be on the same spread. So if you want to work on the document, you probably want to group items per spread. Correct? If yes, use this:

spreads = app.documents[0].spreads.everyItem().getElements();

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

  if (spreads.pageItems.length > 1) {

    spreads.pageItems.everyItem().locked = false;

    spreads.groups.add (spreads.pageItems);

  }

}

Peter

tpk1982
Legend
December 9, 2016

use app.activeDocument.pageItems instead of app.activeWindow.activeSpread.pageItems

OutlineGAuthor
Known Participant
December 9, 2016

//group all in document

var doc = app.activeDocument;

var myItems = app.activeDocument.pageItems;

app.activeDocument.pageItems.groups.add(myItems);

app.select(myItems);

that gives an error...?

the groups.add is wrong I think, but I don't know what the correct code is. I've been trying to open the chm file from JongWare (think that has all indd coding names?) but haven't succeeded yet...

tpk1982
Legend
December 9, 2016

try this

var doc = app.documents[0]; 

var myItems = app.activeWindow.activeSpread.pageItems.everyItem(); 

myItems.locked = false; 

var myGroup = doc.groups.add(myItems); 

app.select(myGroup);