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

How to access layers inside a group in Scripts?

New Here ,
Jun 02, 2021 Jun 02, 2021

Copy link to clipboard

Copied

I have 3 groups in my selection. Each group has the same layers(same Name, but different content). I need to go through each one of them and make some adjustments to the position and sizes of certain layers. I've already have the adjustments coded and they are working great but just that happens only when my selection is made up directly by the layers of 1 group.

 

For better visualization, one group has the following layers: L1(textFrame), L2(Image), L3(Rectangle), etc. What I need is basically to be able to do something like this: Group1.L1.move; Group1.L2.scale; Group2.L1.move; Group2.L2.scale; etc.

TOPICS
Scripting

Views

340

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 ,
Jun 02, 2021 Jun 02, 2021

Copy link to clipboard

Copied

Actual InDesign "layers" exist independently of groups and only are one level deep: myDoc.layers. 

 

To get each page item inside a group, just iterate through your groups to get them: 

var groups = myDoc.groups; 

for (var i = 0; i < groups.length; i++) {

     groups[i].pageItems.itemByName("L1").move(...) 

   etc

}

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 ,
Jun 10, 2021 Jun 10, 2021

Copy link to clipboard

Copied

That might work, but will that go through all the groups inside the whole .indd file? or just the page I am viewing?

 

Is there a way to iterate through the groups of my selection?

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
Community Expert ,
Jun 09, 2021 Jun 09, 2021

Copy link to clipboard

Copied

Hi Alexandru,

do you mean you need access to all page items of a group?

Regardless if the group contains groups itself or other nested structures?

 

Loop the allPageItems array of the outer group.

 

Note:

If the group contains objects with states, like buttons and multistate objects, the array allPageItems does not contain items in not active states.

 

Regards,
Uwe Laubender

( ACP )

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 ,
Jun 10, 2021 Jun 10, 2021

Copy link to clipboard

Copied

I need to go through the groups and place the layers(and other smaller groups inside them) on specific positions based on their index, resize some of the layers, adjust textframe width.

 

All my big Groups are generated from a csv, so every layer has a name that I can easily use to acces that specific layer(eg. a text layer named Text_1). The big problem is that I have to iterate through the groups and make some placements based on the Group's index.

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
Community Expert ,
Jun 10, 2021 Jun 10, 2021

Copy link to clipboard

Copied

"Is there a way to iterate through the groups of my selection?"

 

Sure. First task is to get the groups:

Test for all elements selected, if there is a group selected, then assign the group to a result array.

 

If not only groups are selected:

For all other elements loop the allPageItems array to filter the groups, also write them to your result array.

See sample code below.

 

When done loop the result array that contains only groups and loop through any allPageItems array of any group.

Not directly in sample code below, but it works like the for loop with variable allItems.

 

If stacking order is important for you:

Check the stacking order of elements correlates with the order of items in the allPageItems array of that individual group. Note, that you may find exceptions when items are anchored in text.

 

// No text selected!

var mySelection = app.selection;
var mySelectionLength = mySelection.length;

var myGroupsInSelection = [];

for( var n=0; n<mySelectionLength; n++ )
{

	if( mySelection[n].constructor.name == "Group" )
	{
		myGroupsInSelection[myGroupsInSelection.length++] =
		mySelection[n];
	};

	var allItems = mySelection[n].allPageItems;
	var allItemsLength = allItems.length;
	
	for( var i=0; i<allItemsLength; i++ )
	{
		
		if( allItems[i].constructor.name == "Group" )
		{
			myGroupsInSelection[myGroupsInSelection.length++] =
			allItems[i];
		};
	};

};

alert( "Number of Groups in Selection:" +"\r"+ myGroupsInSelection.length );

 

Another note about allPageItems:

This array will NOT contain items that are stored in states of buttons, multistate objcets etc.pp. where the states are not active. So you may miss items in buttons etc.pp., generally spoken in objects that contain more than one state.

 

Regards,
Uwe Laubender

( ACP )

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
Community Expert ,
Jun 10, 2021 Jun 10, 2021

Copy link to clipboard

Copied

LATEST

"The big problem is that I have to iterate through the groups and make some placements based on the Group's index"

 

What exactly do you mean by "index"?

 

FWIW: As already mentioned, there are no layers inside a group.

Elements are stacked inside groups. All the elements in a group and the group itself is part of the same InDesign layer.

 

You should give us access to a sample InDesign document; put it on Dropbox or a similar service and share the download link.

 

Thanks,
Uwe Laubender

( ACP )

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