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 :
#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.
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;
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.
Copy link to clipboard
Copied
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();