Copy link to clipboard
Copied
I am trying to create a script that iterates thru the selected pathItems on the artboard and adds their name to an array but at the start of each group of pathItems that have the same name I would like a header child.
Artboard pathItems:
3 - pathItem.name = lemon
2 - pathItem.name = orange
3 - pathItem.name = peach
Header child:
pathObj.addChild("These are lemons:");
pathObj.addChild("These are oranges:");
pathObj.addChild("These are peaches:");
Resulting array:
[These are lemons:, lemon, lemon, lemon, These are oranges:, orange, orange, These are peaches:, peach, peach, peach]
Any help would be greatly appreciated.
1 Correct answer
here's a sample
// map pathItems to json 12/5/24
// https://community.adobe.com/t5/illustrator-discussions/how-to-add-a-header-child-to-array/m-p/15022564#M429214
function main() {
var idoc = app.activeDocument;
var pItems = idoc.pathItems;
var fruits = {};
var pname;
for (var b=0; b<pItems.length; b++) {
pname = pItems[b].name;
// if key does not exist, create one and initialize it with an empty array
if (!fruits[pname]
...
Explore related tutorials & articles
Copy link to clipboard
Copied
it might be possible to make an array but the way you described it, an Object might be easier to consume after it has been made instead of an Array
fruits = {
lemon: [lemon, lemon, lemon],
orange: [orange, orange],
peach: [peach, peach, peach]
}
Copy link to clipboard
Copied
here's a sample
// map pathItems to json 12/5/24
// https://community.adobe.com/t5/illustrator-discussions/how-to-add-a-header-child-to-array/m-p/15022564#M429214
function main() {
var idoc = app.activeDocument;
var pItems = idoc.pathItems;
var fruits = {};
var pname;
for (var b=0; b<pItems.length; b++) {
pname = pItems[b].name;
// if key does not exist, create one and initialize it with an empty array
if (!fruits[pname]) {
fruits[pname] = [];
}
fruits[pname].push(pname);
}
//alert(fruits.toSource());
//alert(fruits["orange"].toSource());
// then to consume the data
alert("There are: " + fruits["orange"].length + " Oranges");
}
main();
Copy link to clipboard
Copied
now if it is imperative to have an Array I'll give it another try.
Copy link to clipboard
Copied
@CarlosCanto You can use named members of arrays in ES3. If you just change to
var fruits = [];
I think .your script will work the same. (However like you I would use an object here.)
But I'm actually wondering if OP had something entirely different in mind but I'm not sure what. Maybe a visual object to label the oranges etc?
- Mark
Copy link to clipboard
Copied
Hi Mark, for real? I did not know that 😕
I did what you mentioned and the script works the same, but fruits.toSource() came up empty, but the rest worked as expected
Copy link to clipboard
Copied
Yeah it's weird isn't it? I think Arrays are objects with special handling of numerical indices. But all the normal object stuff works. Maybe.

