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

Create swatch group from selection

Participant ,
Jul 19, 2022 Jul 19, 2022

Copy link to clipboard

Copied

Hi,

I managed to create a swatch group from javascript, now I am trying to get the selection colors to add to that swatch. I know I can add a swatch, take that swatch and put it to the swatch group using :

var swGrp = docRef.swatchGroups.add();
swGrp.name = "ColorsToList";
var newSwatch = docRef.swatches.add();
newSwatch.name = color.toString();
swGrp.addSwatch(newSwatch);
 
what I am having issue with is to make a for loop to loop through the colors of my selected group/object.
 Here's what I tried so far:

 

#target illustrator
app.preferences.setBooleanPreference("ShowExternalJSXWarning", false);
var docRef = app.activeDocument;

function main(){
    SwatchGroup();
    function SwatchGroup(){
        var swGrp = docRef.swatchGroups.add();
        swGrp.name = "ColorsToList";
        for(i = 0; i <= docRef.pathItems[i].length; i++){
                var itemColor = docRef.pathItems[i].fillColor;
                var newSwatch = docRef.swatches.add();
                newSwatch.color = itemColor;
                newSwatch.name = "test" + i;
                swGrp.addSwatch(newSwatch);
            return;
        }
    };
}
main();

 

it simply create the group and a white color with "test1" as the name - my selected item has 5colors.

 

Any idea how I could make it?

thanks.

TOPICS
Scripting

Views

127

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 Expert ,
Jul 19, 2022 Jul 19, 2022

Copy link to clipboard

Copied

try:

var itemColor = docRef.pathItems[i].fillColor.color;

 

or.... if it's a spot color,

var itemColor = docRef.pateItems[i].fillColor.color.spot.color;

Votes

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
Participant ,
Jul 19, 2022 Jul 19, 2022

Copy link to clipboard

Copied

thank you for the fast answer. Really appreciated.
I now have this code here:

#target illustrator
app.preferences.setBooleanPreference("ShowExternalJSXWarning", false);
var docRef = app.activeDocument;

function main(){
    SwatchGroup();
    function SwatchGroup(){
        var swGrp = docRef.swatchGroups.add();
        swGrp.name = "ColorsToList";
        for(i = 0; i <= docRef.pathItems[i].length; i++){
                var itemColor = docRef.pathItems[i].fillColor;
                var newSwatch = docRef.swatches.add();
                if(itemColor.typename.toString() == "SpotColor"){
                    var itemColorName = docRef.pathItems[i].fillColor.spot.name;
                    newSwatch.color = itemColor;
                    newSwatch.name = itemColorName;
                    alert(itemColor.spot.name);
                }
                else{
                    newSwatch.color = itemColor;
                    newSwatch.name = "CMYK" + i;
                }
                swGrp.addSwatch(newSwatch);
                i++;
        }
    };
}
main();

very early code atm... want to switch it to name the CMYK color by the cymk values at a later stage.

Right now it skips a color and add some spot as cmyk... It also adds a bunch of duplicates.

The main use for that script will be list all colors in a design (cmyk and spot) then next step will be to list them on the page in elipse with names next to. There are few spots we need to modify the names depending on client so that is something I will deal with too. Not that I am asking for help for that part... just to give an idea of what I am currently doing so it might help understand the issue.

 

Thanks again.

Votes

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
Participant ,
Jul 19, 2022 Jul 19, 2022

Copy link to clipboard

Copied

LATEST

I just found out that it actually crash when it find another type of... object. That might be why it doesn't provide all the colors from a selection. I've been able to avoid it using a try/catch. But within a group if I had a rectangle or a mask or something else it just don't list it in the colors. Any idea how I could get every colors from every selected items?

#target illustrator
app.preferences.setBooleanPreference("ShowExternalJSXWarning", false);
var docRef = app.activeDocument;

function main(){
    SwatchGroup();
    function SwatchGroup(){
        var swGrp = docRef.swatchGroups.add();
        swGrp.name = "ColorsToList";
        for(i = 0; i <= docRef.pathItems[i].length; i++){
            if(docRef.pathItems[i].selected == true){
                try{
                    var itemColor = docRef.pathItems[i].fillColor;
                    var newSwatch = docRef.swatches.add();
                    if(itemColor.typename.toString() == "SpotColor"){
                        var itemColorName = docRef.pathItems[i].fillColor.spot.name;
                        newSwatch.color = itemColor.spot.color;
                        newSwatch.name = itemColorName;
                        alert(itemColor.spot.name);
                    }
                    else{
                        newSwatch.color = itemColor;
                        newSwatch.name = "CMYK" + i;
                    }
                    swGrp.addSwatch(newSwatch);
                    i++;
                    }
                    catch(err) {
                    alert(err);
                    }
            }
        }
    };
}
main();

Votes

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