Skip to main content
Inspiring
September 14, 2010
Answered

JS Grouping

  • September 14, 2010
  • 1 reply
  • 1503 views

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?

myDoc.groups.add([myTFa,myTFb]);

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.

This topic has been closed for replies.
Correct answer Marc Autret

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.


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

@+

Marc

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.

1 reply

Harbs.
Legend
September 14, 2010

How are you referencing the frames?

Harbs

Inspiring
September 14, 2010

myDoc.textFrames.item("13")

myDoc.textFrames.item("14")

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

Harbs.
Legend
September 14, 2010

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

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

Harbs