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

How to add a header child to array

Explorer ,
Dec 05, 2024 Dec 05, 2024

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.

 

TOPICS
How-to , Scripting
300
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

Community Expert , Dec 05, 2024 Dec 05, 2024

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]
...
Translate
Adobe
Community Expert ,
Dec 05, 2024 Dec 05, 2024

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]

}

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
Community Expert ,
Dec 05, 2024 Dec 05, 2024

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();
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
Community Expert ,
Dec 05, 2024 Dec 05, 2024

now if it is imperative to have an Array I'll give it another try.

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
Community Expert ,
Dec 05, 2024 Dec 05, 2024

@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

 

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
Community Expert ,
Dec 05, 2024 Dec 05, 2024

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

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
Community Expert ,
Dec 05, 2024 Dec 05, 2024
LATEST

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. 

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