• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

How do I put rectangles in a group (JavaScript)?

New Here ,
Apr 03, 2008 Apr 03, 2008

Copy link to clipboard

Copied

Hello,

I have a JavaScript script that draws several graphics rectangles on a page. I would like to put them in a group, the same as I would manually with a selection and ctrl-G.

I have tried many, many approaches but am unable to do it. I would be grateful if someone could either post a code fragment or sketch an outline of what I should do.

Thanks much,
Bill
TOPICS
Scripting

Views

825

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 03, 2008 Apr 03, 2008

Copy link to clipboard

Copied

This groups all text frames on the first page of a document:

doc = app.activeDocument;
doc.pages[0].groups.add (doc.pages[0].textFrames);

Peter

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Apr 04, 2008 Apr 04, 2008

Copy link to clipboard

Copied

Peter,

Thanks for your suggestion. I was able to get that far along in my work. However, I don't want to group ALL the rectangles on the page, only the ones that I create with my script. I obviously have the rectangle returned by rectangles.add() but I can't figure out how to go from that to a group.

Thanks,
Bill

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Apr 04, 2008 Apr 04, 2008

Copy link to clipboard

Copied

Hi Bill,

If you're labeling the rectangles as you create them, then this should work:

myGroup = myPage.groups.add(myPage.rectangles.item("GroupMe"));

Dave

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Apr 04, 2008 Apr 04, 2008

Copy link to clipboard

Copied

You can also use an array. For example, say you had a mix of items that met various criteria, you could do something like this:
myPage = app.documents[0].pages[0];

groupies = new Array();
myPIs = myPage.pageItems;
for (var j = myPIs.length - 1; j >= 0; j--) {
if (myPIs.label == "GroupMe") {
groupies.push(myPIs);
}
}
myGroup = myPage.groups.add(groupies);
I've just used one criterion here, but you could have as many tests as you want within the loop.

Dave

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Apr 04, 2008 Apr 04, 2008

Copy link to clipboard

Copied

Hi Dave,

Thanks, I'll give it a try.
Bill
> A new message was posted by Dave Saunders in
>
> *InDesign Scripting* --
> How do I put rectangles in a group (JavaScript)?
>
> Hi Bill,
>
> If you're labeling the rectangles as you create them, then this should
> work:
>
> myGroup = myPage.groups.add(myPage.rectangles.item("GroupMe"));
>
> Dave

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Apr 04, 2008 Apr 04, 2008

Copy link to clipboard

Copied

Hi Dave,

Thanks much. Just one more thought... Is there some way to identify the index in pageItems or the specific pageItem of an newly created rectangle, that is, from the rectangle returned by rectangles.new()? I could then push that specific PI onto an array and after I make all the rectangles I could then make the group from the array of PIs as you did above.

Bill

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Apr 04, 2008 Apr 04, 2008

Copy link to clipboard

Copied

Sure:

myRect = myPage.rectangles.add();
myGroupies.push(myRect);

Dave

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Apr 04, 2008 Apr 04, 2008

Copy link to clipboard

Copied

That was the first thing that I tried and I'm quite sure that it didn't work. myPage.rectangles.add() returns a rectangle, not a pageItem. groups.add() expects an array of pageItems and was displeased when I called it with a list of rectangles. I'll try it again in the event that I had something screwed up.

Bill

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Apr 04, 2008 Apr 04, 2008

Copy link to clipboard

Copied

Rectangles are page items.

Page item is a generic kind of object; rectangles are specific kinds.

It is, though, possible to create an array of rectangles (or any page items) that can't be grouped. For example, if oone (or more) of the rectangles is already in a group or is anchored to text. Or if the objects are on different spreads.

The syntactic correctness of a parameter doesn't assure success.

Dave

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Apr 04, 2008 Apr 04, 2008

Copy link to clipboard

Copied

Aha! Now that makes a lot of things clearer. Thanks! I'll give the array of my newly created rectangles another try. There was probably something else at play during my first test.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Apr 04, 2008 Apr 04, 2008

Copy link to clipboard

Copied

LATEST
Dave & others,

After an afternoon of some frustration, mostly due to noobie issues:

1) The distinction between "Selection" vs. "Direct Selection" pointers.
2) That a group cannot contain just one item

I was finally able to accomplish exactly what I wanted. I push each rectangle onto an array as I make it. I then page.groups.add() that array of rectangles and then select the group. Works perfect.

My sincere thanks for your help. I really appreciate it. Lots to learn.

Bill

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines