Skip to main content
July 29, 2010
Answered

Beginner: help with ungroup script

  • July 29, 2010
  • 3 replies
  • 13380 views

I just began learning Javascript for InDesign CS5 and I would appreciate some step by step help

Goal: Ungroup all grouped objects in the active document.

- Active document

- Select all grouped items

- Ungroup the items

I tried a bunch of different ways… but I always got an orange highlight in the ESTK. What would the first step be?

Correct answer Muppet_Mark-QAl63s

#target indesign function unGroup() {      if (app.documents.length == 0) {           alert("Please have an 'Indesign' document before running this script.");           return;      }      docRef = app.activeDocument;      with(docRef) {           alert('This doc has '+groups.length+' groups…')           while (groups.length != 0) {                groups.everyItem().ungroup();                alert('This doc now has '+groups.length+' groups…')           }      }      } unGroup();

The count may increase if you have nested but should work…

3 replies

July 29, 2010

I'm trying to alert the user if there are no grouped items on the document:

if (app.activeDocument.groups == 0)

            {

                alert("No grouped item.")

                break;

                 }

                {

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

                 }

It gets stuck at break;

Muppet_Mark-QAl63s
Muppet_Mark-QAl63sCorrect answer
Inspiring
July 29, 2010

#target indesign function unGroup() {      if (app.documents.length == 0) {           alert("Please have an 'Indesign' document before running this script.");           return;      }      docRef = app.activeDocument;      with(docRef) {           alert('This doc has '+groups.length+' groups…')           while (groups.length != 0) {                groups.everyItem().ungroup();                alert('This doc now has '+groups.length+' groups…')           }      }      } unGroup();

The count may increase if you have nested but should work…

July 29, 2010

Thanks MM,

What is

#target indesign?

Does that tell the ESTK to switch to "InDesign mode"?

Or perhaps as a trigger for external scripts?

Peter Kahrel
Community Expert
Community Expert
July 29, 2010

In the UI you always have to select objects before you want to do something with them. In scripting this is often not the case:

// off the top of my head:

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

Peter

Jongware
Community Expert
Community Expert
July 29, 2010

Oddly enough, that looks *just* like my solution. See screenshot proof ...

Muppet, this one-command falls under "advanced scripting", so I'd still like to see your attempts.

Muppet_Mark-QAl63s
Inspiring
July 29, 2010

Jong, were you talking to me?

while (groups.length != 0) {      groups.everyItem().ungroup(); }

Should keep ungrouping groups in groups too? May be…

Jongware
Community Expert
Community Expert
July 29, 2010

I can do it in one command

But, rather than telling you and spoil all the fun (mine at least ), can you show what you have tried so far? It might be useful to pin-point where you go wrong.

Peter Kahrel
Community Expert
Community Expert
July 29, 2010

Sorry I spoiled your fun!