Skip to main content
JADarnell
Inspiring
January 29, 2014
Answered

Creating a color in InDesign Javascript (CC)

  • January 29, 2014
  • 2 replies
  • 2784 views

Hello everyone:

I am using this code to try to create a color that can be used to fill a button:

function MakeButtonColor()

{

   var TheColor;

   var TheDocument = app.activeDocument;

   var myError;

  

   try

   {

      TheColor = TheDocument.colors.item("DPSButtonColor");

      var ColorName = TheColor.name;

   }

   catch(myError)

   {

       TheColor = TheDocument.colors.add({name: "DPSButtonColor", colorSpace:ColorSpace.RGB, model:ColorModel.process,colorValue:[0,137,208]});

   }

   var ButtonColor1 = TheColor;

   alert("ButtonColor is " + ButtonColor1.name);

     

}

I am taking this directly from the Scripting Guide with only a very few modifications.

But I never ever see the last two statements executed.   I've tried all sorts of things to make this work and it just doesn't want to work.

Anyone have any ideas why it won't work and what I can do to fix it?

This topic has been closed for replies.
Correct answer csm_phil

Hi JADarnell,

I just modified your code as per the Trevor mentioned lines. Try this!

MakeButtonColor()

function MakeButtonColor(){

   var TheColor;

   var TheDocument = app.activeDocument;

   var myError;

   try{

       TheColor = TheDocument.colors.itemByName("DPSButtonColor");

       var ColorName = TheColor.name;

       }

   catch(myError){

       TheColor = TheDocument.colors.add({name: "DPSButtonColor", space:ColorSpace.RGB, model:ColorModel.process,colorValue:[0,137,208]});

       }

   var ButtonColor1 = TheColor;

   alert("ButtonColor is " + ButtonColor1.name);

   }

thx,

csm_phil

2 replies

csm_phil
csm_philCorrect answer
Legend
January 30, 2014

Hi JADarnell,

I just modified your code as per the Trevor mentioned lines. Try this!

MakeButtonColor()

function MakeButtonColor(){

   var TheColor;

   var TheDocument = app.activeDocument;

   var myError;

   try{

       TheColor = TheDocument.colors.itemByName("DPSButtonColor");

       var ColorName = TheColor.name;

       }

   catch(myError){

       TheColor = TheDocument.colors.add({name: "DPSButtonColor", space:ColorSpace.RGB, model:ColorModel.process,colorValue:[0,137,208]});

       }

   var ButtonColor1 = TheColor;

   alert("ButtonColor is " + ButtonColor1.name);

   }

thx,

csm_phil

JADarnell
JADarnellAuthor
Inspiring
January 30, 2014

Thanks Trevor, thanks Phil.  After wrestling with what can only be described as a self-inflicted wound, I used your code and it worked as advertised.

I considered starting a flame war WRT the proper positioning of brackets in code, but decided I was too grateful for such unpleasant behavior (grin).

My only other, closing remark is that I was following a number of examples which used "colorSpace" instead of "space."  They, however, were obviously wrong...success proved that.

Take care,

John

Trevor:
Legend
January 30, 2014

ColorSpace is then name of the enum i.e. Colorspace.RGB.

Trevor:
Legend
January 30, 2014

Hi JADarnell,

change colorSpace to space and make sure to call the function by adding MakeButtonColor() to the beggining or end of the script