Skip to main content
amberw42080715
Inspiring
May 3, 2018
Answered

How to put swatches in a swatchGroup using javascript?

  • May 3, 2018
  • 2 replies
  • 2446 views

I can't figure this out for the life of me. 

I have these functions that work great.  It puts the SpotColor swatches into the swatch panel.

var doc = app.activeDocument;

var paths = doc.pathItems;

function getColorNumbers() {

    var colorNums = []

    for (i = 0; i <= 10; i += 1) {

    colorNums.push(i / 100);

    }

   

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

    createNewSpotColor(colorNums); 

    } 

    return colorNums;

}

function createNewSpotColor(color) {

     //Define the new color value

    var newColor = new CMYKColor();

    newColor.cyan = 0;

    newColor.magenta = 0;

    newColor.yellow = 0;

    newColor.black = color;

   

    //Create the new spot

    var newSpot = app.activeDocument.spots.add();

    //Define the new SpotColor

    newSpot.name = color.toString();

    //newSpot.tint = 0;

    newSpot.typename = SpotColor;

    newSpot.color = newColor;

   // create a spotcolor object

    newSpot.colorType = ColorModel.SPOT;

    var newSpotColor = new SpotColor();

    newSpotColor.spot = newSpot;

    //newSpotColor.tint = 0;

}

getColorNumbers();

Now I would like to put these swatches into a swatchGroup.  I've tried several things.  One of my tries worked but put every individual swatch into it's own swatchGroup.  All the other times I can get the swatches in the panel and a new swatchGroup added, but can't get the swatches in the swatchGroup.

var doc = app.activeDocument; 

var colorgroup = doc.swatchGroups.add();

    colorgroup.name = 'Cad colors'; 

   

    var selSwatch = doc.swatches.getSelected(); 

    var swatch = doc.swatches; 

 

    for (a=0; a<swatch.length; a++) { 

      if (swatch.color == SpotColor) {

          swatch.selected = true;

          if(swatch.selected == selSwatch) {

            colorgroup.addSpot(selSwatch);   

            }

     }

}

What am I missing?

This topic has been closed for replies.
Correct answer pixxxelschubser

Hmmh?

amberw42080715​, why do you ignore my posts?

Here is a shorter way for creating your swatches.

var aDoc = app.activeDocument;

try {

    var swGrp = aDoc.swatchGroups.getByName("deine neue Farbgruppe");

}

catch (e) {

    var swGrp = aDoc.swatchGroups.add();

    swGrp.name = "deine neue Farbgruppe";

};

for (i = 0; i <= 10; i++) {

    colorSwatch (i/100);

}

function colorSwatch (color) {

    var newGrayColor = new GrayColor();

    newGrayColor.gray = color;

   

    var newSwatch = aDoc.swatches.add();

    newSwatch.name = color.toString();

    newSwatch.color = newGrayColor;

    swGrp.addSwatch(newSwatch);

    return;

};

2 replies

pixxxelschubser
Community Expert
pixxxelschubserCommunity ExpertCorrect answer
Community Expert
May 3, 2018

Hmmh?

amberw42080715​, why do you ignore my posts?

Here is a shorter way for creating your swatches.

var aDoc = app.activeDocument;

try {

    var swGrp = aDoc.swatchGroups.getByName("deine neue Farbgruppe");

}

catch (e) {

    var swGrp = aDoc.swatchGroups.add();

    swGrp.name = "deine neue Farbgruppe";

};

for (i = 0; i <= 10; i++) {

    colorSwatch (i/100);

}

function colorSwatch (color) {

    var newGrayColor = new GrayColor();

    newGrayColor.gray = color;

   

    var newSwatch = aDoc.swatches.add();

    newSwatch.name = color.toString();

    newSwatch.color = newGrayColor;

    swGrp.addSwatch(newSwatch);

    return;

};

amberw42080715
Inspiring
May 3, 2018

I didn't mean to ignore your post.  Sorry if I did, I'm not quit sure which post I ignored.  I'm very appreciative for your help.  This has been a great forum to come for answers. Thank you so much for helping me out.

pixxxelschubser
Community Expert
Community Expert
May 3, 2018

You're welcome.

-----------------------------------

amberw42080715  schrieb

…Sorry if I did, I'm not quit sure which post I ignored …

OT only for clarification

That was in your other thread How to output the values in a loop? post #1 and #4

I ask you:

"Do you need this array later for something else?

Do you really need the two loops?"

- but you gave no answer.

I optimized your code a bit - but also no reaction.

And here in this (new) thread you still used 'the long' way with to much loops and if clauses.

End OT

Best regards

Disposition_Dev
Legend
May 3, 2018

use the addSwatch() method instead.

change line 12 to this:

colorgroup.addSwatch(selSwatch);