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

group all items on page in a document

Participant ,
Dec 09, 2016 Dec 09, 2016

Copy link to clipboard

Copied

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

TOPICS
Scripting

Views

2.3K

Translate

Translate

Report

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

Community Expert , Dec 09, 2016 Dec 09, 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

...

Votes

Translate

Translate
Guide ,
Dec 09, 2016 Dec 09, 2016

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

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
Participant ,
Dec 09, 2016 Dec 09, 2016

Copy link to clipboard

Copied

//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...

Votes

Translate

Translate

Report

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
Guide ,
Dec 09, 2016 Dec 09, 2016

Copy link to clipboard

Copied

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);

Votes

Translate

Translate

Report

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 ,
Dec 11, 2016 Dec 11, 2016

Copy link to clipboard

Copied

OutlineG wrote:

…I've been trying to open the chm file from JongWare (think that has all indd coding names?) but haven't succeeded yet...

Hi Vanessa,

when on Mac OSX—don't know if you are—you can open Jongware's chm files with the free app iChm for Mac.

I'm not sure what is required for Windows, but it seems that there is an add-on for Firefox browser, that can read chm files.

Depending on your version of InDesign you could also browse online for InDesign's DOM documentation.

Compiled by Jongware:

Adobe InDesign CS5 (7.0) Object Model JS: Table of Contents

Adobe InDesign CS5.5 (7.5) Object Model JS: Table of Contents

Adobe InDesign CS6 (8.0) Object Model JS: Table of Contents

Compiled by Gregor Fellenz:

InDesign ExtendScript API (8.0)

InDesign ExtendScript API (11.0)

InDesign ExtendScript API (12.0)

Regards,
Uwe

Votes

Translate

Translate

Report

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 ,
Dec 09, 2016 Dec 09, 2016

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

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
Participant ,
Dec 12, 2016 Dec 12, 2016

Copy link to clipboard

Copied

var doc = app.activeDocument;

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

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

Votes

Translate

Translate

Report

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 ,
Dec 12, 2016 Dec 12, 2016

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

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 ,
Dec 12, 2016 Dec 12, 2016

Copy link to clipboard

Copied

A document is organized in master spreads and spreads.
With document.spreads you are addressing all spreads that are not masters.

If you want to loop through only a subset of the spreads you have to start with the appropriate index number and the approriate length of the loop.

Example where you start with the third spread and you end with the fourth spread, that means doing a loop through two spreads:

var spreads = app.documents[0].spreads;

for(var n=2;n<4;n++)

{

    // Doing something with the third and the fourth spread:

    // Where n can be 2 or 3. The loop will stop if 3 is reached.

    // n is used as value for the spreads index number.

    var currentSpreadLooped = spreads;

    // Alternatively this could be written as:

    var currentSpreadLooped = spreads.item(n);

    // because variable spreads is holding a collection of spreads.

    // If variable spreads would hold an Array type of items with:

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

    // you have to use the first alternative for denoting a single spread in the loop:

    // spreads and not spreads.item(n) .

    // ** WARNING **

    // The code above inside this loop will also work with documents with only a SINGLE spread!

    // InDesign is not looking, if the a denoted spread exists before you do not like to do something with the spread.

    // We can say: In this case the current spread stored in variable currentSpreadLooped "is not resolved"!

   

    // Doing something with a spread that is not existing in the document will throw an error!

    // For example: alert(spreads.pages.length) will throw:

    // "Object is invalid"

   

    // You could "resolve" the denoted spread by using:

    var currentSpreadLooped = spreads.getElements()[0];

    // or:

    var currentSpreadLooped = spreads.item(n).getElements()[0];

   

    // OR YOU DECIDE TO WORK WITH AN ARRAY TYPE FROM THE START AND RESOLVE THE OBJECTS THAT SHOULD BE STORED:

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

   

    $.writeln(n);

};

Regards,
Uwe

Votes

Translate

Translate

Report

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
Participant ,
Dec 12, 2016 Dec 12, 2016

Copy link to clipboard

Copied

LATEST

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

Votes

Translate

Translate

Report

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