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

Select and change fill color of multiple onject with the same name

Participant ,
Jul 05, 2023 Jul 05, 2023

Hello there,

i'm trying to achieve this. I have a script that opens a dialog window in which i can select the color from the swatches and apply it at the corresponding object.

The problem is it works only with the objects in the first layer. I have multiple objecst with the same name in all the different layers of my doc.

 

Here's the script

 

#target illustrator

// Create a reference to the active document
var doc = app.activeDocument;

// Create an array of paths with their respective names
var tracciati = [
  { nome: "A", colore: "" },
  { nome: "B", colore: "" },
  { nome: "C", colore: "" }
];

// Create an array with the names of color samples present in the document
var campioni = [];
for (var i = 0; i < doc.swatches.length; i++) {
  var swatch = doc.swatches[i];
  campioni.push(swatch.name);
}

// Create a dialog window
var dialog = new Window("dialog", "Seleziona tracciati e colore di riempimento");
dialog.orientation = "column";

// Add a panel for path selection
var tracciatiPanel = dialog.add("panel", undefined, "Seleziona tracciati");
tracciatiPanel.alignChildren = "left";
tracciatiPanel.orientation = "column";

var dropdownsColori = [];

// Create dropdown menus for color selection for each path
for (var i = 0; i < tracciati.length; i++) {
  var tracciato = tracciati[i];

  var tracciatoGroup = tracciatiPanel.add("group");
  tracciatoGroup.orientation = "row";
  
// Add path label
var tracciatoLabel = tracciatoGroup.add("statictext", undefined, tracciato.nome);
  
// Add dropdown menu for color selection
var dropdownColori = tracciatoGroup.add("dropdownlist", undefined, campioni);
  dropdownColori.selection = 0;
  dropdownsColori.push(dropdownColori);
}

// Add an "OK" button to the dialog window
var okButton = dialog.add("button", undefined, "OK");
okButton.onClick = function() {
// Iterate through the color dropdown menus
for (var i = 0; i < tracciati.length; i++) {
    var tracciato = tracciati[i];
    var dropdownColori = dropdownsColori[i];
    
    // Get the index of the selected color
    var selectedColorIndex = dropdownColori.selection.index;
    
    // Get the corresponding color sample
    var selectedColor = doc.swatches[selectedColorIndex].color;
    
    // Select the corresponding path in the illustration
    var tracciatoSelezionato = doc.pathItems.getByName(tracciato.nome);
    tracciatoSelezionato.selected = true;
    
    // Apply the fill color to the selected path
    tracciatoSelezionato.fillColor = selectedColor;
  }
  
  // Close the dialog window
  dialog.close();
};

// Show the dialog window
dialog.show();


The layers before the script:

1.JPG

 

The layers after the script, as you can see not all objects A, B and C are filled.


2.JPG

 

 

Any help? Thanks!

TOPICS
Experiment , Scripting , Tools
324
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 , Jul 05, 2023 Jul 05, 2023

Hi @Delresto, I think the problem is that your script is using

doc.pathItems.getByName(tracciato.nome);

but I think the "getByName" method returns a single pathItem. You need to get multiple items. I think you will have to loop over the pathItems and check each name, rather than this line.

- Mark

Translate
Adobe
Community Expert ,
Jul 05, 2023 Jul 05, 2023

Hi @Delresto, I think the problem is that your script is using

doc.pathItems.getByName(tracciato.nome);

but I think the "getByName" method returns a single pathItem. You need to get multiple items. I think you will have to loop over the pathItems and check each name, rather than this line.

- 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
Participant ,
Jul 06, 2023 Jul 06, 2023
LATEST

Thank you m1b, that helped!

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