• 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 iterate over items within a group using JSX?

Explorer ,
Jun 25, 2021 Jun 25, 2021

Copy link to clipboard

Copied

I have around 160 groups of objects in my AI file.

I would like to perform an action ("Expand stroke, pathfinder Boolean Add and similar) on all object together within one group.
So, I'm writing a script to do just that. I'm stuck quite early in the process.

 

I can find all 'groups' within my current selection. However, I can't access the separate items within each group. The documentation (Adobe Illustrator CC 2017 Scripting Reference: JavaScript) says that a GroupItem should have GroupItems and PathItems. However, these properties do not exist (undefined). When I create a list of properties, indeed, these are not listed. All I get back from the script is:

 

clipped typename uRL note layer locked hidden selected position width height geometricBounds visibleBounds controlBounds name uuid blendingMode opacity isIsolated artworkKnockout zOrderPosition absoluteZOrderPosition editable sliced top left visibilityVariable pixelAligned wrapped wrapOffset wrapInside parent

 

So, where are the items within my group?

 

For context, a screenshot:

mhvandervelde_0-1624608945879.png

And my piece of Javascript:

    for (var i = 0; i < sel.length; i++) {
        var a = "";
        for (var key in grp) {
            a += key + "\n";
        }
        alert(a);
    }

 

(I have to use 'alert()' because Adobe's ExtendScript Toolkit is impossibly sluggish to work with)

TOPICS
Scripting

Views

2.7K

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

correct answers 1 Correct answer

Community Expert , Jun 25, 2021 Jun 25, 2021

Illustrator's DOM is pretty similar to a browser DOM if you're familiar with that. They pretty much follow the same tree shape. So, consider that there are effectively 2 types of items.. a discreet item, and a container item. Discreet items (pathItem,textFrameItem,placedItem, etc) can only go inside container items (groupItem,compoundPathItem,layer,document).

 

Each container item has a few properties that describe its child elements. so for example, a groupItem has some array properties like gr

...

Votes

Translate

Translate
Adobe
LEGEND ,
Jun 25, 2021 Jun 25, 2021

Copy link to clipboard

Copied

ExtendScript Toolkit is defunct. You should be using Microsoft VSCode.

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
Explorer ,
Jun 25, 2021 Jun 25, 2021

Copy link to clipboard

Copied

Okay, thanks.

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

Copy link to clipboard

Copied

Illustrator's DOM is pretty similar to a browser DOM if you're familiar with that. They pretty much follow the same tree shape. So, consider that there are effectively 2 types of items.. a discreet item, and a container item. Discreet items (pathItem,textFrameItem,placedItem, etc) can only go inside container items (groupItem,compoundPathItem,layer,document).

 

Each container item has a few properties that describe its child elements. so for example, a groupItem has some array properties like groupItem.pageItems (this is all children of the container, including other containers... except for layers), groupItem.compoundPathItems (this is just top level compound paths that exist in groupItem), groupItem.pathItems (this is any top level simple path items like polygons), etc.

 

So, to answer your question, to access the items of a given group, you just need to determine what KIND of items you want to identify, then use that property to access the array of child elements. So let's look at a use case example for finding and manipulating all the simple pathItems of each group in the selection:

 

 

#target Illustrator
function test()
{
	var doc = app.activeDocument;
	var sel = doc.selection;
	if(!sel.length)
	{
		alert("Please make a selection first.");
		return;
	}


	function doSomething(item)
	{
		//do something to the item here
	}

	function loopPathItems(group)
	{
		for(var p=0;p<group.pathItems.length;p++)
		{
			doSomething(group.pathItems[p]);
		}
	}


	for(var x=0;x<sel.length;x++)
	{
		if(sel[x].typename === "GroupItem")
		{
			loopPathItems(sel[x]);
		}	
	}
	
}
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
Explorer ,
Jun 25, 2021 Jun 25, 2021

Copy link to clipboard

Copied

Thank you, @Disposition_Dev Your script worked liked a charm!…

…and so did mine, when I finally discovered that although GroupItem is written with a capital 'G', pathItems is written with a lower case 'p'... (sigh).

Thanks also for the clear explanation on the hierarchy of the Document Object Model analog and the very legible script.

Still I wonder, why is 'pathItems' (and friends) not listed as a property when I iterate through the GroupItem properties? Would you happen to know?

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

Copy link to clipboard

Copied

That's a good question that i don't really know the answer to.. But suffice it to say, we're not working with ES6 JavaScript here. This is "ExtendScript" which is basically a clone of ES3 JavaScript. But as far as i know it's not an exact copy. There are things you can do in the javascript web environment that you just can't do in ExtendScript (and to be clear, i'm not talking about ExtendScript Toolkit the IDE, I'm talking about the language that illustrator scripts are built on).

 

Even if you add a breakpoint in VSCode.. You can step through the code and view the properties of any object you like, but the pageItems/groupItems/pathItems/etc arrays don't show up. you have to log them to the console or alert them if you want to see them.

 

Try out this function. This will give you a clean javascript array (rather than whatever kind of "collection" it is when you just access groupItem.pathItems or similar. in some way or other, those aren't true arrays and they can behave oddly) so that you can process the array of pageItems however you would normally process an array. You'll also be able to loop the array to give you whatever proprties you want for each item. just pass in 

 

https://github.com/wdjsdev/public_illustrator_scripts/blob/master/array_from_container.js

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
Explorer ,
Jun 26, 2021 Jun 26, 2021

Copy link to clipboard

Copied

Ah, now I see it: your profile name over here is a Spoonerism of your real name (or at least your gitHub name).

Thanks for sharing your useful collection of scripts!

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

Copy link to clipboard

Copied

I sure hope you find some of them useful. 

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
Advocate ,
Jul 13, 2023 Jul 13, 2023

Copy link to clipboard

Copied

Is there any code or reference on how to access a plugs nItem. When we use two shapes and use apt subtract from the pathfinder panel. It creates a live sort of boolean. The typename it returns is plugonItem. I would like to be able to check either the children or make sure the store b I or children items are pathItem or compundpathItem

 

I'm update Ng a script when ch calculates the area of a shape and this pluginItem returns an error

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 ,
Jul 24, 2023 Jul 24, 2023

Copy link to clipboard

Copied

im not aware of any scripts for that (which doesnt mean they dont exist). But my inital reaction is to make a copy of the plugin item, expand it to turn it into a regular shape (or group of regular shapes) then analyze the resuting object to get the area, then delete the copy you made.

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
Advocate ,
Jul 25, 2023 Jul 25, 2023

Copy link to clipboard

Copied

Yes that is my solution as well. Im now copy/pasting it then group it and apply pathfinder trim action. Then i call the script again and delete the duplicate. I do give the user a warning dialog stating what is going to happen. They can do a manual re-check if they doubt the script works and to make sure it is the correct outcome

 

This method also works like a charm for compundPathItems which are intersecting.

I used this on the script originally created by Bryan Buchanan

Ive expanded it with more functionality and also the settings are saved and read back in when rerunning the script

preview of the dialogpreview of the dialog

 

I just did notice i need to make a better check. If i run the script with multiple objects and one is a pluginItem, it doesn't calculate properly. Need to a bit more work on it i guess

 

EDIT 2

okay that was rather a quick one, already got it fixed within couple minutes

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 ,
Jul 25, 2023 Jul 25, 2023

Copy link to clipboard

Copied

You could do the copy/pathfinder/analyze/delete without pausing the script, I believe.

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
Advocate ,
Jul 26, 2023 Jul 26, 2023

Copy link to clipboard

Copied

Yes, perhaps I should. I've test d many shapes. Wacht time double checking it by doing manual pathfinder trim function and then run the script. Outcome was the same.

I tried also looking online for area calculator to do extra check. But haven't found one yet

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 ,
Jul 26, 2023 Jul 26, 2023

Copy link to clipboard

Copied

LATEST

yea it can be difficult to find readymade illustrator scripts to fulfill specific requirements like this. its a relatively small community by software standards.

 

if youre willing to share your code and a sample file, i could help finish up the area function to do the logic i laid out above. send me a DM or an email to illustrator.dev.pro@gmail.com

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
Guide ,
Jun 25, 2021 Jun 25, 2021

Copy link to clipboard

Copied

Just to add to what's already been said, items within container items are not enumerable with a "for ... in" loop.  

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

Copy link to clipboard

Copied

good point Femke.

 

container objects ARE objects... they're just not javascript objects. the Ilustrator API needs to be used in order to access these items.

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