12 Replies Latest reply on Sep 19, 2010 7:56 PM by Fred Goldman

    JS Grouping

    Fred Goldman Level 3

      I have two text frames both on the same page, same layer unlocked. If I group them with the UI it works. When I try to group them with this script it doesn't. It gives an error invalid parameters. Can anyone figure out what the problem might be?





      Accoding to the Extendscript DataBrowser both myTFa and myTFb point to valid text frames. If I try this same syntax on a different doc it works, but if I paste these same two text frames into my problem doc it doesn't.


      I am at a loss as to what is wrong. I know I am not giving a lot to go on, but I can't see what the problem can be.

        • 1. Re: JS Grouping
          Harbs. Level 6

          How are you referencing the frames?



          • 2. Re: JS Grouping
            Fred Goldman Level 3



            This is in CS4 so that should still be kosher. And it worked in another doc, finally, the databrowser shows it is pointing to valid text frames. (Should I have put that into a try/catch/finally statement .


            Message was edited by: Fred Goldman

            • 3. Re: JS Grouping
              Harbs. Level 6

              That can't be right. myDoc.item should be undefined...


              Maybe you have more than one object with the same label?



              • 4. Re: JS Grouping
                Fred Goldman Level 3

                Yeah, sorry, I edited the message it should be myDoc.textFrames.item("13"). I also tried myDoc.pageItems.item("14").


                There is only one with that label.

                • 5. Re: JS Grouping
                  Fred Goldman Level 3

                  If I select them both and run this it also doesn't work:




                  But if I copy them to a new doc and run it it does.

                  • 6. Re: JS Grouping
                    Marijan Tompa [tomaxxi] Level 4

                    Have you declared document variable?


                    var myDoc = app.activeDocument;





                    • 7. Re: JS Grouping
                      Harbs. Level 6

                      What happens if you give an alert that shows each item.parent.name


                      What do you get?



                      • 8. Re: JS Grouping
                        Harbs. Level 6

                        Do you possibly have two docs with the same name?



                        • 9. Re: JS Grouping
                          Fred Goldman Level 3

                          myDoc is defined.


                          They are both unsaved documents


                          item.parent.name returns 16 for both.


                          And just to add another twist CS3 it throws the same error and in CS5 it works using itemByName.

                          • 10. Re: JS Grouping
                            Marc Autret Level 4

                            Hi Fred,


                            The problem sounds to come from the fact that InDesign does not allow us to group objects from anywhere.


                            As ID users, we know that the objects must share the same spread. At the scripting level, the statement myDoc.groups.add(arrayOfItems) is potentially defective because there is no guarantee that the objects passed as arguments belong to the same spread. I don't know how the Scripting DOM internally deal with this risk, but I think that the safe way to group objects should be to always invoke .groups.add(...) from the right Spread or Page object.


                            You indicate that “item.parent.name returns 16 for both [objects],” from which I conclude that your text frames probably belong to Page 16 of your document. When you test your script on a new document that contains a copy of the text frames, it suddenly works because (I suppose) the items now belong to Page 1. And I guess that myDoc.groups.add(...) has a default behavior which consists on delegating the method to the first spread of myDoc. It may be implicitly equivalent to: myDoc.spreads[0].groups.add(...).


                            If my hypothesis is correct, this explains why myDoc.groups.add( [tf1, tf2] ) fails when the items belong to another spread.

                            So, give a try to this statement:

                            tf1.parent.groups.add( [tf1,tf2] );





                            EDIT: In CS5, myDoc.groups.add(...) seems to be more tolerant in that it probably finds itself the right parent receiver, and validates the action if grouping is possible.

                            • 11. Re: JS Grouping
                              Harbs. Level 6

                              Nice theory.



                              • 12. Re: JS Grouping
                                Fred Goldman Level 3

                                You hit it right on!


                                Thanks Marc!