23 Replies Latest reply on Feb 21, 2013 11:59 AM by MrTIFF

    Beginner: help with ungroup script

    Rand0mUser Level 1

      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?

        • 1. Re: Beginner: help with ungroup script
          [Jongware] Most Valuable Participant

          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.

          1 person found this helpful
          • 2. Re: Beginner: help with ungroup script
            Peter Kahrel Adobe Community Professional & MVP

            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

            1 person found this helpful
            • 3. Re: Beginner: help with ungroup script
              Peter Kahrel Adobe Community Professional & MVP

              Sorry I spoiled your fun!

              • 4. Re: Beginner: help with ungroup script
                [Jongware] Most Valuable Participant

                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.

                 

                groups.png

                • 5. Re: Beginner: help with ungroup script
                  Muppet Mark-QAl63s Level 4

                  Jong, were you talking to me?

                   

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

                   

                  Should keep ungrouping groups in groups too? May be…

                  • 6. Re: Beginner: help with ungroup script
                    Rand0mUser Level 1

                    I was doing variations of the code you guys sent before.

                     

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

                     

                    Doesn't work.

                     

                    Edit: DAMMIT! AGAIN! AARGH!

                     

                    I left the top left pop-up menu on ESTK CS5 instead of Adobe InDesign CS5. Now it works…

                     

                    Jong, what's your for for?

                     

                    My guess at that line of code:

                    s (variable for group selection?) starts at the beginning of the active document, it stops at the end of the active document, increments one step at a time (i.e., every instance of a group)

                     

                    ?

                    • 7. Re: Beginner: help with ungroup script
                      Rand0mUser Level 1

                      I did just about this and variations thereof before you posted this one:

                       

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

                       

                      To test it, i grouped two text frames together in InDesign.

                      • 8. Re: Beginner: help with ungroup script
                        [Jongware] Most Valuable Participant

                        sorry

                        no, mixed up posts.
                        • 9. Re: Beginner: help with ungroup script
                          Rand0mUser Level 1

                          Oops, I sent replied to one message via email and I thought it wasn't posted (because of the delay).

                           

                          Is that what you mean by "no mixed up posts"?

                          • 10. Re: Beginner: help with ungroup script
                            Laubender Adobe Community Professional & MVP

                            Muppet Mark is right: to catch all nested groups one would need a while loop:

                             

                            var G = app.activeDocument.groups;
                            
                            while (G.length != 0) {
                                 G.everyItem().ungroup();
                                 };
                            

                             

                            Uwe

                            • 11. Re: Beginner: help with ungroup script
                              Peter Kahrel Adobe Community Professional & MVP

                              > Should keep ungrouping groups in groups too? May be…

                               

                              That's right.

                              • 12. Re: Beginner: help with ungroup script
                                Muppet Mark-QAl63s Level 4

                                Jong inadvertently called you a 'muppet' when that title belongs to me…

                                 

                                mark.hasOwnProperty('muppet');
                                

                                • 13. Re: Beginner: help with ungroup script
                                  Rand0mUser Level 1

                                  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;

                                  • 14. Re: Beginner: help with ungroup script
                                    Muppet Mark-QAl63s Level 4

                                    #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…

                                    • 15. Re: Beginner: help with ungroup script
                                      Rand0mUser Level 1

                                      Thanks MM,

                                       

                                      What is

                                      #target indesign?

                                       

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

                                      Or perhaps as a trigger for external scripts?

                                      • 16. Re: Beginner: help with ungroup script
                                        Muppet Mark-QAl63s Level 4

                                        It does in mine but I think that may be 'all old hat' now? I can also run the script by double clicking in GUI… (If ESTK is not open)

                                        • 17. Re: Beginner: help with ungroup script
                                          Peter Kahrel Adobe Community Professional & MVP

                                          That #target directive is needed when you run scripts from the ESTK. You can also target PhotoShop, Illustrator, Bridge, etc. etc., including the ESTK itself. But when you run scripts from the Scripts panel there's no need for the #target directive.

                                           

                                          Peter

                                          • 18. Re: Beginner: help with ungroup script
                                            Abigailrstahlman Level 1

                                            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!

                                            • 19. Re: Beginner: help with ungroup script
                                              MrTIFF Level 3

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

                                              • 20. Re: Beginner: help with ungroup script
                                                Abigailrstahlman Level 1

                                                WOW, thank you this is working perfectly.

                                                 

                                                I am also trying to combine your script with another script I have been editing (I'm sorry I don't remember where I found it, but I can not take any credit).This other script moves all text boxes to a new layer.

                                                 

                                                I am trying to move all text boxes except those with effects applied. Essentially, I need to create separate layers for text with and without effects.

                                                 

                                                I realize that the if statement needs to be altered, I am also wondering if the "grp", needs to be changed to "Item"?

                                                 

                                                I also realize that combining these scripts probably makes no sense, but I am trying my best to copy paste and figure things out as I go, I have no experience with javascript, and I infinitely appreciate all of the help this forum has offered me.

                                                 

                                                 

                                                Original move text script:

                                                 

                                                var newLayer = app.activeDocument.layers.add();

                                                var tfs =

                                                app

                                                .activeDocument

                                                .spreads.everyItem().textFrames.everyItem().getElements();

                                                while(tf = tfs.pop()){

                                                 

                                                   tf.move(newLayer);

                                                 

                                                }

                                                 

                                                Copy/Paste failed script:

                                                 

                                                var newLayer = app.activeDocument.layers.add();

                                                     var tfs =

                                                     app

                                                     .activeDocument

                                                     .spreads.everyItem().textFrames.everyItem().getElements()

                                                       

                                                unction 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 (hasAnEffect(item)) {

                                                 

                                                                               continue;

                                                 

                                                                     } else {

                                                 

                                                                            tf.move(newLayer);

                                                 

                                                                     }

                                                 

                                                           }

                                                 

                                                }

                                                 

                                                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;

                                                           }

                                                }

                                                     while(tf = tfs.pop()){

                                                         (tf = tfs.pop())

                                                         tf.move(newLayer);

                                                         }

                                                 

                                                Thanks for the 3rd time now, everyone on these forums is amazing.

                                                • 21. Re: Beginner: help with ungroup script
                                                  MrTIFF Level 3

                                                  You just need to test for effects from your textFrame-and-layer code, using exactly the same functions as we used for testing for Group effects.

                                                  Reusable functions -- you gotta love it! :-)

                                                   

                                                  Like this:

                                                   

                                                  #target "indesign"

                                                   

                                                  var doc = app.activeDocument;

                                                  var newLayer = doc.layers.itemByName("MyNewTextFrameLayer");

                                                  if (!newLayer.isValid)

                                                            newLayer = doc.layers.add({name:"MyNewTextFrameLayer"});

                                                  var tfs = doc.spreads.everyItem().textFrames.everyItem().getElements();

                                                  while(tf = tfs.pop())

                                                  {

                                                            if(hasAnEffect(tf) == false) {

                                                                           tf.move(newLayer);

                                                    }

                                                  }

                                                   

                                                  function hasAnEffect(grp) {

                                                            [same as before]

                                                  }

                                                   

                                                  function settingHasAnEffect(settingX) {

                                                            [same as before]

                                                  }

                                                  • 22. Re: Beginner: help with ungroup script
                                                    Abigailrstahlman Level 1

                                                    Amazing, This works like a charm, thank you so much. You have really been an increadable help to me.

                                                     

                                                    I was trying to add a test to your settingHasAnEffect(settingX) function, but I can not get it work. I want also exclude text boxes with any fill color.

                                                     

                                                    I am guessing that the problem is either that, ".fillColor" is the wrong vocabulary (by the way where in ExtendScript can I find these terms), or that a fill color is actually a preference, and not a setting, and can not be applied, or tested along side the applied effects.

                                                     

                                                    I am getting closer in at least diagnosing my mistakes?

                                                     

                                                    ...

                                                     

                                                          var test11 = settingX.fillColor.applied;

                                                     

                                                     

                                                     

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

                                                     

                                                                    test8 || test9 || test10 || test11) {

                                                     

                                                     

                                                    ...

                                                     

                                                    Thank You! Thank You!

                                                    • 23. Re: Beginner: help with ungroup script
                                                      MrTIFF Level 3

                                                      fillColor is actually a property of the pageItem, not one of the transparency (effect) Setting objects, so you address it just by pageItem.fillColor. (both a textFrame and a Group are PageItems.)

                                                       

                                                      So we can just add a hasFillColor function and add that test to the hasAnEffect test that we have already implemented. hasFillColor() is a little bit more complicated than you would expect, since, as the documentation states, fillColor can either be a Swatch object or a Color object. So we have to test for both cases.

                                                       

                                                      (By the way, I don't "just know" all these things -- I have a horrible memory, actually -- I usually have to look them up in the object reference documentation.)

                                                       

                                                      So you get something like this:

                                                       

                                                      #target "indesign"

                                                       

                                                       

                                                      var doc = app.activeDocument;

                                                      var newLayer = doc.layers.itemByName("MyNewTextFrameLayer");

                                                      if (!newLayer.isValid)

                                                                newLayer = doc.layers.add({name:"MyNewTextFrameLayer"});

                                                      var tfs = doc.spreads.everyItem().textFrames.everyItem().getElements();

                                                      while(tf = tfs.pop())

                                                      {

                                                                if(hasFillColor(tf) || hasAnEffect(tf)) {

                                                                          ; // do nothing

                                                                } else {

                                                                          tf.move(newLayer);

                                                                }

                                                      }

                                                       

                                                       

                                                      function hasFillColor(pageItem) {

                                                                var swatch = pageItem.fillColor;

                                                                if (swatch.constructor && swatch.constructor.name == "Swatch") {

                                                                          return (swatch.index == 0) ? false : true; // [None]

                                                                          // the reason I'm testing swatch.index == 0 instead of swatch.name == "None"

                                                                          // is that I can't remember if the swatch names get localized or not,

                                                                          // and we don't want our script to break in a non-English-language country.

                                                                } else {

                                                                          return true;          // not a Swatch -- so a non-swatch (unnamed) Color was applied

                                                                }

                                                      }

                                                       

                                                       

                                                      function hasAnEffect(pageItem) {

                                                                var contentX = pageItem.contentTransparencySettings;

                                                                var fillX = pageItem.fillTransparencySettings;

                                                                var strokeX = pageItem.strokeTransparencySettings;

                                                                var tX = pageItem.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;

                                                                }

                                                      }