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

Explorer ,
Mar 02, 2021 Mar 02, 2021

Copy link to clipboard

Copied

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();

 

TOPICS
Scripting

Views

105

Likes

translate

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 2 Correct Answers

Adobe Community Professional , 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...

Likes

translate

Translate

Translate
Adobe Community Professional , 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.

Likes

translate

Translate

Translate
Adobe Community Professional ,
Mar 02, 2021 Mar 02, 2021

Copy link to clipboard

Copied

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(",");

Likes

translate

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 ,
Mar 02, 2021 Mar 02, 2021

Copy link to clipboard

Copied

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

Likes

translate

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 ,
Mar 02, 2021 Mar 02, 2021

Copy link to clipboard

Copied

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

Likes

translate

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
Enthusiast ,
Mar 02, 2021 Mar 02, 2021

Copy link to clipboard

Copied

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();

Likes

translate

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
Adobe Community Professional ,
Mar 02, 2021 Mar 02, 2021

Copy link to clipboard

Copied

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.

Likes

translate

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 ,
Mar 03, 2021 Mar 03, 2021

Copy link to clipboard

Copied

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

Likes

translate

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