Quitter
  • Communauté internationale
    • Langue:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티

How to script all document swatches (from the swatches panel) into a drop down list UI

Participant ,
Mar 02, 2021 Mar 02, 2021

Hi,

 

I am trying to loop through the document swatches from the swatches panel to pull into a drop down list in a simple ui dialog panel.

 

I have managed to get a loop to alert of all the swatches but only one at a time and am struggling to get the full list pulled into the drop down menu. I have also managed to get the last Pantone color to pull into the list (see screenshot and code) but I presume that this is because of the loop only picking the last item up.

 

How would I create a loop to add an array for the drop down list to see all the swatches in the swatches panel?

 

Thanks in advance.

Screenshot 2021-03-02 at 15.39.04.png

var dialog = new Window("dialog");
    dialog.text = "Dialog";
    dialog.orientation = "column";
    dialog.alignChildren = ["center","top"];
    dialog.spacing = 10;
    dialog.margins = 16;
    var spotNames = app.activeDocument.swatches.length;
    for (i=0; i < app.activeDocument.swatches.length; i++){
    spotNames = app.activeDocument.swatches[i].name
}
var dropdown1_array = [spotNames];
var dropdown1 = dialog.add("dropdownlist", undefined, undefined, {name: "dropdown1", items: dropdown1_array});
    dropdown1.selection = 0;

dialog.show();

 

SUJETS
Scripting
797
Traduire
Signaler
Directives de la communauté
Restez bienveillant et courtois, ne vous attribuez pas la paternité des créations d’autrui et assurez-vous de l’absence de doublons avant de poster du contenu. En savoir plus
community guidelines

correct answers 2 bonnes réponses

Héros valeureux , Mar 02, 2021 Mar 02, 2021

You have got to add new items to an array like this:

var myArray = [];

var thisItem;

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

  thisItem = allTheItems[i];

  myArray.push(thisItem);

}

 

Alternatively, which isn't really recommended but can be a good teaching tool or technique in other situations, you can build a string by adding to it with += and also some delimiter, and then using String.split() to turn that string into a proper array to use in places where an array is needed.

 

var myStri

...
Traduire
Héros valeureux , Mar 02, 2021 Mar 02, 2021

It should just be 

var dropdown1_array = myArray;

No backets because it's already an array. You would make the brackets when creating the array: var myArray = []; 

As you may notice, you can just use myArray to put into the dropdown list argument at this point rather than assigning to a different variable - unless you like the renaming for some reason.

Traduire
Adobe
Héros valeureux ,
Mar 02, 2021 Mar 02, 2021

You have got to add new items to an array like this:

var myArray = [];

var thisItem;

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

  thisItem = allTheItems[i];

  myArray.push(thisItem);

}

 

Alternatively, which isn't really recommended but can be a good teaching tool or technique in other situations, you can build a string by adding to it with += and also some delimiter, and then using String.split() to turn that string into a proper array to use in places where an array is needed.

 

var myString = "";

var thisItem;

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

  thisItem = allTheItems[i];

  myString += thisItem;

  // don't put the last delimiter

  if (i < allTheItems.length - 1) {

      myString += ",";

  }

}

 

var myArray = myString.split(",");

Traduire
Signaler
Directives de la communauté
Restez bienveillant et courtois, ne vous attribuez pas la paternité des créations d’autrui et assurez-vous de l’absence de doublons avant de poster du contenu. En savoir plus
community guidelines
Participant ,
Mar 02, 2021 Mar 02, 2021

I will take a look at this and see if I can make it work and let you know I get on. thanks.

Traduire
Signaler
Directives de la communauté
Restez bienveillant et courtois, ne vous attribuez pas la paternité des créations d’autrui et assurez-vous de l’absence de doublons avant de poster du contenu. En savoir plus
community guidelines
Participant ,
Mar 02, 2021 Mar 02, 2021

I have had a look at this and I had already tried to push into an array but I missed putting the item in the brakets to push back in, so I was on the right lines at one point, just moved away from there.

 

I have tried what you have suggested and it is gathering everything together into the array but how would I then break this down for individual colours for the drop down as it is adding everything into the first option (see screenshot)? I have done it as per the code below:

var dropdown1_array = [myArray];

Screenshot 2021-03-02 at 17.25.44.png

Traduire
Signaler
Directives de la communauté
Restez bienveillant et courtois, ne vous attribuez pas la paternité des créations d’autrui et assurez-vous de l’absence de doublons avant de poster du contenu. En savoir plus
community guidelines
Guide ,
Mar 02, 2021 Mar 02, 2021
var dialog = new Window("dialog");
dialog.text = "Dialog";
dialog.orientation = "column";
dialog.alignChildren = ["center","top"];
dialog.spacing = 10;
dialog.margins = 16;
var myArray = [];
for (i=0; i < app.activeDocument.swatches.length; i++){
    myArray.push(app.activeDocument.swatches[i].name);
}
var dropdown1 = dialog.add("dropdownlist", undefined, undefined, {name: "dropdown1", items: myArray});
dropdown1.selection = 0;
dialog.show();
Traduire
Signaler
Directives de la communauté
Restez bienveillant et courtois, ne vous attribuez pas la paternité des créations d’autrui et assurez-vous de l’absence de doublons avant de poster du contenu. En savoir plus
community guidelines
Héros valeureux ,
Mar 02, 2021 Mar 02, 2021

It should just be 

var dropdown1_array = myArray;

No backets because it's already an array. You would make the brackets when creating the array: var myArray = []; 

As you may notice, you can just use myArray to put into the dropdown list argument at this point rather than assigning to a different variable - unless you like the renaming for some reason.

Traduire
Signaler
Directives de la communauté
Restez bienveillant et courtois, ne vous attribuez pas la paternité des créations d’autrui et assurez-vous de l’absence de doublons avant de poster du contenu. En savoir plus
community guidelines
Participant ,
Mar 03, 2021 Mar 03, 2021
LA PLUS RÉCENTE

Thanks so much for all your help on this. I have got it working now.

Traduire
Signaler
Directives de la communauté
Restez bienveillant et courtois, ne vous attribuez pas la paternité des créations d’autrui et assurez-vous de l’absence de doublons avant de poster du contenu. En savoir plus
community guidelines