Skip to main content
July 29, 2010
Answered

Beginner: help with ungroup script

  • July 29, 2010
  • 3 replies
  • 13215 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…

MrTIFF
Participating Frequently
February 19, 2013

Hi,

I am wondering if this script could be modified to ignore groups with effect applied. Example (When working manually):

Lets say I am working on a spread, with multiple groups in the layout, and one of these groups has a drop shadow applied to it. If I SELECT ALL then UNGROUP, I will get the error, "The group has one or more effects applied. Ungrouping will remove those effects and may cause the appearance of items in the group to change..." I need to ignore these groups, and avoid changing the appearance of the document. Is there a way to incorporate this into the Ungroup script above?

I am very new to scripting, and I am trying my best, learning as I go along, and not having much success, so I am reaching out for help.

I think that I need to define effects, and tell the script to ignore somehow. I can't figure out what the vocabulary for applied effects should be var effects = ????, and I also can't figure out where in the script to add if(effects.length == 0). I recognize that this might not be possible, since this script seems to be reading the entire document, as opposed to page items, I hope it can be easily adapted, but I realize this might be a much more complicated issue.

Thanks so much for any help, I wish I was further along in JavaScript, and could be asking smarter and more specific questions. Maybe someday I will be able to return all the scripting favors I have received.

Oh also I am working in CS6 on OSX 10.8.

THANKS!


Well, this seems to work, though it's a bit lengthy. The hard part was figuring out whether or not a Group (or any other PageItem) has an effect applied to it. I had to check through a lot of Preferences to do it. If there's an easier way, I hope that someone will point it out!

#target indesign

/**

* for all Groups in the activeDocument, check to make sure no Effects are applied to the Group;

* if no effects, then ungroup it.

*

* 19 Feb 2013 - Stephen Carlsen.

*/

function unGroupAll() {

          if (app.documents.length == 0) {

                    alert("Please have an Indesign document open before running this script.");

                    return;

          }

          var doc = app.activeDocument;

          var items = doc.allPageItems;

          for (var ix = 0; ix < items.length; ix++) {

                    var item = items[ix];

                    var itemType = item.constructor.name;

                    if (itemType != "Group") {

                              continue;

                    }

                    if (hasAnEffect(item)) {

                              continue;

                    } else {

                              item.ungroup();

                    }

          }

}

function hasAnEffect(grp) {

          var contentX = grp.contentTransparencySettings;

          var fillX = grp.fillTransparencySettings;

          var strokeX = grp.strokeTransparencySettings;

          var tX = grp.transparencySettings;

          if (settingHasAnEffect(contentX)

                    || settingHasAnEffect(fillX)

                    || settingHasAnEffect(strokeX)

                    || settingHasAnEffect(tX)) {

                    return true;

          } else {

                    return false;

          }

}

function settingHasAnEffect(settingX) {

          var test1 = settingX.bevelAndEmbossSettings.applied;

          var test2 = (settingX.blendingSettings.blendMode != BlendMode.NORMAL);

          var test3 = settingX.directionalFeatherSettings.applied;

          var test4 = (settingX.dropShadowSettings.mode == ShadowMode.DROP);

          var test5 = (settingX.featherSettings.mode != FeatherMode.NONE);

          var test6 = settingX.gradientFeatherSettings.applied;

          var test7 = settingX.innerGlowSettings.applied;

          var test8 = settingX.innerShadowSettings.applied;

          var test9 = settingX.outerGlowSettings.applied;

          var test10 = settingX.satinSettings.applied;

 

          if (test1 || test2 || test3 || test4 || test5 || test6 || test7 ||

                    test8 || test9 || test10) {

                    return true;

          } else {

                    return false;

          }

}

unGroupAll();

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!