Skip to main content
Participant
August 27, 2008
Question

[JS][CS3] Create new swatch

  • August 27, 2008
  • 3 replies
  • 2985 views
Hi all,

I'm writing a script that reads an xml file and then converts the contents to InDesign Objects.
Most of this I can do, but I'm having problems with the colors and swatches in InDesign.

Simple said, what I'm trying to do is the following:
1. Create a rectangle on my document
2. Create a new Color Swatch with CMYK values
3. Use the swatch for the rectangle's fillColor or strokeColor

Using Javascript I can add new colors tot my document: myDoc.colors.add()
But I can't use this color for the fillColor or strokeColor properties, it says they it needs to be a swatch, and not a color.

Unfortunately, the method myDoc.swatches.add() doesn't exist, and I can't set a colorValue property to a swatch.

Does anybody know how to do this? I'm guessing there's a simple solution for this, but I'm just not seeing it.

Thanks in advance,
Dany
This topic has been closed for replies.

3 replies

Participant
August 28, 2008
And ironically, in my first post in this topic, I did use myDoc.colors.add() :-D
Participant
August 28, 2008
Hi Kasyan,

Thanks very much for your reply.

I breaked my head trying to find what was the difference between your code and mine, but now I see:

Instead of using myDoc.colors, I used app.colors, which returns an error if you try to use that color for a rectangle, textframe, etc.

Thanks !
Dany
Kasyan Servetsky
Legend
August 27, 2008
Hi Dany,
Here is an example:

var myDoc = app.activeDocument;
var myRectangle = myDoc.rectangles.add();
var myColor = myDoc.colors.add();
myColor.colorValue = [0,0,0,60];
myColor.name = "My New Color";
myRectangle.fillColor = myColor;

Kasyan
Inspiring
January 20, 2016

I know this is an old post but I'm new at scripting and wondering if this could be accomplished with out using variables? I tried to do it but it does't work. Is a variable needed here?

app.activeDocument.colors.add().colorValue = [0,0,100,10].name = "My New Color";

Jump_Over
Legend
January 21, 2016

Hi,

New at scripting? Everyone was, is or will be...

Start from learning syntax.

app.activeDocument.colors.add({

  colorValue: [0,0,100,10],

  name: "My New Color"

  });

Jarek