0

I have a very long list of names that I need to alternate the CMYK color value every other line.

I found this script online that changes the tint every 3rd line:

var i, p;
for (i=0; i<app.selection[0].paragraphs.length; i++) {
  p = app.selection[0].lines[i];
  if (i%3 === 2) { p.fillTint = 50; }

}

I tried changing the script to change the CMYK value doing this:

var i, p;
for (i=0; i<app.selection[0].paragraphs.length; i++) {
  p = app.selection[0].lines[i];
  if (i%2 === 1) { p.colorValue=[0,100,100,0]; }

}

But using "colorValue" is not supported. How can I change the script to make every other line in a text box change the color value?

2
  • Should it change every line into a new color or change every second line into a different color? If you need every line in another color you will run into a pretty full swatch panel. InDesign can't apply colors from scripting without creating a new swatch. – fabianmoronzirfas Oct 27 '15 at 4:30
  • @fabiantheblind In my opinion it doesn't matter if the script does every even line or every odd line because you can change the starting fill color to what you want an adjust the script. In my answer below I have a script written by Kasyan Servetsky that changes the CMYK fill color every even line number. – AndrewH Oct 27 '15 at 12:59
1

Credit to Kasyan Servetsky for creating the script.

var i, p, color;  
for (i=0; i<app.selection[0].paragraphs.length; i++) {  
    p = app.selection[0].lines[i];  
    color = makeColor("C=0 M=100 Y=100 K=0", ColorSpace.CMYK, ColorModel.process, [0, 100, 100, 0]);  
    if (i%2 === 1) {   
        p.fillColor = color;  
    }  
}  

function makeColor(colorName, colorSpace, colorModel, colorValue) {  
    var doc = app.activeDocument;  
    var color = doc.colors.item(colorName);  
    if (!color.isValid) {  
        color = doc.colors.add({name: colorName, space: colorSpace, model: colorModel, colorValue: colorValue});  
    }  
    return color;  
}
0
0

How about:

for ( var i=0; i < app.selection[0].paragraphs.length; i++ ) {

  var p = app.selection[0].lines[i],
      even = i % 2 === 1;

  if ( even ) {

    p.fillColor = "My Swatch Name";

    // Or one of these two:
    // p.applyParagraphStyle( app.activeDocument.paragraphStyles.itemByName("My Paragraph Style"), true );
    // p.applyCharacterStyle( app.activeDocument.characterStyles.itemByName("My Character Style"), false );

  }

}
0

I suggest this version Kudos to indisnip.wordpress.com

//  found on http://bit.ly/h5EobK indisnip.wordpress.com ->
//  how to apply:
//
//  add CMYK color
// myColorAdd(app.activeDocument, "My Custom Color", ColorModel.PROCESS, [80,50,30,10]);
//
//  add RGB color
// myColorAdd(app.activeDocument, "My Custom Color", ColorModel.PROCESS, [33,66,99]);
//
//  add HEX color
// myColorAdd(app.activeDocument, "My Custom Color", ColorModel.PROCESS, "ABCDEF");
//
// add color directly
// add CMYK col or to document
// and assign it  to selected object
//app.selection [0].fillColor = myColorAdd(app.activeDocument, "My Custom Color", ColorModel.PROCESS,  [80,50,30,10]);
color_maker = function(myDocument, myColorName, myColorModel, myColorValue) {
  if (myColorValue instanceof Array === false) {
    myColorValue = [(parseInt(myColorValue, 16) >> 16) & 0xff, (parseInt(myColorValue, 16) >> 8) & 0xff, parseInt(myColorValue, 16) & 0xff];
    myColorSpace = ColorSpace.RGB;
  } else {
    if (myColorValue.length == 3)
      myColorSpace = ColorSpace.RGB;
    else
      myColorSpace = ColorSpace.CMYK;
  }
  try {
    myColor = myDocument.colors.item(myColorName);
    myName = myColor.name;
  } catch (myError) {
    myColor = myDocument.colors.add();
    myColor.properties = {
      name: myColorName,
      model: myColorModel,
      space: myColorSpace,
      colorValue: myColorValue
    };
  }
  return myColor;
};
var main = function() {
  if (app.documents.length === 0) return;
  var doc = app.activeDocument;
  if (doc.selection.length === 0) return;
  var sel = app.activeDocument.selection[0];
  for (var i = 0; i < sel.lines.length; i++) {
    if (i % 2 === 0) {
      var col = color_maker(doc, 'my color ' + i, ColorModel.PROCESS, [0, 0, 0, Math.random() * 100]);
      sel.lines[i].fillColor = col;
    }
  }
};
main();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.