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

How to access elements in a group with JavaScript? (Beginner Question)

Engaged ,
Jul 19, 2021 Jul 19, 2021

Hello all,
I am trying to learn javascript for illustrator. Unfortunately I found many different PDF files, but no real beginner's guide for the basics.

Currently I want to access the elements of a group in a selection to do something with the elements in order. My structure looks like this, * = selected with the mouse.

 

Layer 1
- * Group 1
- * Rectangle 1
- * Rectangle 2
- Rectangle 3

 

(A) When I start my script, I get for
app.activeDocument.selection.length is 1, but there are 3 elements selected.

 

(B) How can I visit the elements of "Group 1"? I have only found a solution in which I visit ALL element and check if they are selected and no group. That should be easier, right?


Here is my script:

// Visit all elements of a selection
//
// If there is 1 group with 2 child elements
// than I get vSelected = 1 and vCounter = 2

var docRef = app.activeDocument;
var vCounter = 0;
var vSelected = docRef.selection.length; //

for ( var i = 0; i < docRef.pageItems.length; i++ ) {
	var vElement = docRef.pageItems[i];
	if ( vElement.selected === true ) {
		if (vElement.typename != "GroupItem") {
		vCounter++; // Do something with vElement
		}
	}
}

alert (vCounter + " Elements in the selection" + );

// end of file.

 

Greetings, Jens

TOPICS
Scripting
1.2K
Translate
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

correct answers 1 Correct answer

Guide , Jul 19, 2021 Jul 19, 2021

You reference the elements of a group as follows:

 

var parent = docRef.groupItems[0];
var child1 = docRef.groupItems[0].pathItems[0];
var child2 = docRef.groupItems[0].pathItems[1];

 

The "selection" collection contains parent items only. So it contains groupItems[0] only (i.e. length is 1). Children items may have the "selected" property assigned true, but are not part of the "selection" collection.  

 

The pageItems collection contains parent and children items. So it contains groupItems[0], groupI

...
Translate
Adobe
Guide ,
Jul 19, 2021 Jul 19, 2021

You reference the elements of a group as follows:

 

var parent = docRef.groupItems[0];
var child1 = docRef.groupItems[0].pathItems[0];
var child2 = docRef.groupItems[0].pathItems[1];

 

The "selection" collection contains parent items only. So it contains groupItems[0] only (i.e. length is 1). Children items may have the "selected" property assigned true, but are not part of the "selection" collection.  

 

The pageItems collection contains parent and children items. So it contains groupItems[0], groupItems[0].pathItems[0] and groupItems[0].pathItems[1] (i.e. length is 3).

 

Translate
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
Engaged ,
Jul 19, 2021 Jul 19, 2021

ok, thanks. That's what I was looking for. I will try it just now.

Translate
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
Engaged ,
Jul 19, 2021 Jul 19, 2021
LATEST

So, I finally figured out how to visit ONLY the items in a group in a selection. Thanks. here is my script.

 

 

// Visit all elements of a selection

var docRef = app.activeDocument;
var vSelected = docRef.selection;
var vCounter = 0;

// if the first object in the selection is a group:
var vParent = vSelected[0]; // get the elements of the group
var vNumbers = vParent.pageItems.length; // numbers of elements
// pageItems is important, because it could be also a textframe and so on

for ( var i = 0; i < vNumbers; i++ ) {
	var vChild = vParent.pageItems[i];
	// do something with vChild
	vCounter++;
}

alert (vNumbers + " / " + vCounter + " Elements in the selection");

// end of file.

 

Case closed. Jens.

 

Translate
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