Saturday, April 29, 2006

 

Add Object to Group

Here's a first cut at a method to add an object to a group. I find myself wanting to do this kind of thing when I'm using InDesign as an illustration tool. There's a lot more could be done by the method (for example: add at front or back, or even add at a designated point -- in front of/behind a member of the group; even in place of), but this first cut demonstrates feasibility:
//DESCRIPTION: Test of method that adds an item to an existing group

Group.prototype.addObject = function(theObj) {
  var myArray = this.pageItems.everyItem().id;
  myArray.push(theObj.id);
  var myParent = this.parent;
  try { this.ungroup() } catch (e) { throw "Unable to ungroup" }
  var gpArray = new Array();
  for (var j = 0; myArray.length > j; j++) {
  gpArray.push(myParent.pageItems.itemByID(myArray[j]));
  }
  try { myParent.groups.add(gpArray) } catch (e) { throw "Unable to regroup" }
}

myGroup = app.activeDocument.pages[0].groups[0];
myTF = app.activeDocument.pages[0].textFrames[0];
myGroup.addObject(myTF);
The two error conditions could be caused by:

1. Trying to ungroup a group that is inside another group or is an anchored object.
2. Trying to add a text object (other than a text frame) to the group.

Probably, in the case of 2, I should first reassemble the original group before throwing the error. But for the moment I'm just playing with an idea.

Comments: Post a Comment

<< Home

This page is powered by Blogger. Isn't yours?