Skip to main content
Inspiring
December 7, 2022
Question

Apply background to different products based on the Layers

  • December 7, 2022
  • 2 replies
  • 586 views

Hi,

In the InDesign document, I created two layers. With the help of the two layers, I need to apply the background colors for the different products in the InDesign document. Here is the code I used to apply the background color to the products. Could you please guide me on how to apply background colors to the document using layers?  

 

//The active document:
var myDoc = app.activeDocument;

//The layers collection of the active document:
var myLayers = myDoc.layers;

//Loop through the layers collection of the active document:
for(var i = 0; i < myLayers.length; i++)
{
alert( myLayers[i].name );
app.documents[0].layers[0].layerColor = UIColors.PURPLE;
};

This topic has been closed for replies.

2 replies

Community Expert
December 12, 2022

@MonishaRajendran said:

"With the help of the two layers, I need to apply the background colors for the different products in the InDesign document. "

 

I'm not sure what you like to do.

Please share two InDesign pages with sample frames.

One with the original state, one where all is done what you like to do.

 

FWIW: With "background colors" I could imagine that you need a frame stacked all below in the stack of frames that has the area of a page where a distinct fill color should be applied to. Depending on the layer all other contents of the page is positioned on. Of course I could be wrong…

 

Regards,
Uwe Laubender
( Adobe Community Expert )

Loic.Aigon
Legend
December 7, 2022

Not sure I get your right but if you want to apply a specific color depending on the layer name, you can do something like :

 

//The active document:
var myDoc = app.activeDocument;

//The layers collection of the active document:
var myLayers = myDoc.layers;

var myColors = {
    red:UIColors.RED,
    green:UIColors.GREEN   
}

//Loop through the layers collection of the active document:
var layerName = "";
var nLayer;
for(var i = 0; i < myLayers.length; i++)
{
    nLayer = myLayers[i];
    layerName =  nLayer.name;
    var color = myColors[layerName];
    if(color){
        nLayer.layerColor = color;
    }
};
Inspiring
December 8, 2022

Hi @Loic.Aigon 

For the whole InDesign document, I need to apply the layer colors for the textframe in the background color.
 
-Monisha
Legend
December 10, 2022

hello @MonishaRajendran,

 

Give this script a try...

Note: You'll need to set the UIColors for layers 1 & 2 to the colors that you want.

The function getUIcolorValue with the hard coded UIcolor values was found in this post: https://community.adobe.com/t5/indesign-discussions/getting-the-id-and-the-rgb-array-for-the-tag-color-of-a-xml-tag/td-p/8475136

I recommend that you read through the post to see how the UIcolor values were obtained using the Digital Color Meter.app and make sure that the values match your desired output. If not, you can see how to use the Digital Color Meter.app in this post: https://support.apple.com/lt-lt/guide/digital-color-meter/welcome/mac

so you can update the values to a different color space.

 

 

function main() {
  
  function getUIcolorValue(r){if(r.constructor===Array)return pColour;switch(String(r)){case"BLACK":return[0,0,0];case"BLUE":return[0,0,255];case"BRICK_RED":return[153,0,0];case"BROWN":return[153,51,0];case"BURGUNDY":return[153,0,51];case"CHARCOAL":return[171,163,181];case"CUTE_TEAL":return[130,207,194];case"CYAN":return[0,255,255];case"DARK_BLUE":return[0,0,135];case"DARK_GREEN":return[0,84,0];case"FIESTA":return[247,89,107];case"GOLD":return[255,153,0];case"GRASS_GREEN":return[153,204,0];case"GRAY":return[128,128,128];case"GREEN":return[79,255,79];case"GRID_BLUE":return[122,186,217];case"GRID_GREEN":return[156,222,156];case"GRID_ORANGE":return[255,181,107];case"LAVENDER":return[153,153,255];case"LIGHT_BLUE":return[79,153,255];case"LIGHT_GRAY":return[186,186,186];case"LIGHT_OLIVE":return[140,166,107];case"LIPSTICK":return[207,130,181];case"MAGENTA":return[255,79,25];case"OCHRE":return[153,102,0];case"OLIVE_GREEN":return[102,102,0];case"ORANGE":return[255,102,0];case"PEACH":return[255,153,153];case"PINK":return[255,153,204];case"PURPLE":return[102,0,102];case"RED":return[255,0,0];case"SULPHUR":return[207,207,130];case"TAN":return[204,153,102];case"TEAL":return[0,153,153];case"VIOLET":return[153,51,255];case"WHITE":return[255,255,255];case"YELLOW":return[255,255,79];default:return alert("Could not find UI colour called "+String(pColor)),[186,186,186]}}

  var doc = app.activeDocument;

    doc.layers[0].layerColor = UIColors.PURPLE;
    doc.layers[1].layerColor = UIColors.ORANGE;

   for(var i = 0; i < doc.layers.length; i++) {
     var myNewColorSwatch = doc.layers[i].layerColor.toString();
       var myLayerColorValues = getUIcolorValue(doc.layers[i].layerColor);

       var colorValues = [];
       for (var c = 0; c < myLayerColorValues.length; c++) 
         colorValues.push(parseInt(myLayerColorValues[c]));
  
         var myColorToCheck = doc.colors.itemByName(myNewColorSwatch);

         if(!myColorToCheck.isValid){
          var myColor = doc.colors.add();
          myColor.model = ColorModel.PROCESS;
          myColor.space = ColorSpace.RGB      //RGB UIColors: DO NOT remove or comment this line out
          myColor.colorValue = colorValues; 
          myColor.name = myNewColorSwatch;
          myColor.space = ColorSpace.CMYK;    //RGB UIColors to CMYK: comment out to keep colors RGB
        }
    }

    for(var p = 0; p < doc.pages.length; p++) {
       var myPageItems = doc.pages[p].pageItems.everyItem().getElements();
       for(var i=0;i<myPageItems.length;i++){
           if (myPageItems[i] instanceof TextFrame){
              myLayerColor = myPageItems[i].itemLayer.layerColor.toString();
                myPageItems[i].fillColor = doc.swatches.itemByName(myLayerColor);
            }
        }
    }
}

app.doScript(main, ScriptLanguage.JAVASCRIPT, undefined, UndoModes.ENTIRE_SCRIPT, 'Do Script');

 

 

Regards,

Mike