Skip to main content
Inspiring
June 15, 2016
Answered

How to define my color?

  • June 15, 2016
  • 5 replies
  • 1444 views

Hi experts,

I have a script:

app.findObjectPreferences = null;

app.findObjectPreferences.fillColor = "MARKUP";

app.changeObjectPreferences.fillColor = "Black";

app.changeObject();

app.findObjectPreferences = null;

app.findObjectPreferences.strokeColor = "MARKUP";

app.changeObjectPreferences.strokeColor = "Black";

app.changeObject();

for find and change object color, but if the docs which has no such as color name in the swatch, the script will run into error, so, how can I define if no such as color, script continue? how can make the script as below working?

var myDoc = app.activeDocument;

myColor = myDoc.colors.name("MARKUP");

if (!cmyColor) continue;

app.findObjectPreferences = null;

app.findObjectPreferences.fillColor = myColor;

app.changeObjectPreferences.fillColor = "Black;

app.changeObject();

app.findObjectPreferences = null;

app.findObjectPreferences.strokeColor = myColor;

app.changeObjectPreferences.strokeColor = "Black";

app.changeObject();

thanks

Regard

John

This topic has been closed for replies.
Correct answer Kai Rübsamen

Here is an idea, that includes also the previous question "MARKUP, markup, MARK-UP, MARK_UP":

app.doScript(main, ScriptLanguage.JAVASCRIPT , [], UndoModes.ENTIRE_SCRIPT, "Replace Colors");

function main() {

var curDoc = app.activeDocument;

var allSwatches = curDoc.swatches;

var allPItems = curDoc.allPageItems;

var colorsToRemove = [];

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

  var curSwatch = allSwatches;

  var curSwatchName = curSwatch.name;

  var colorToFind = curSwatchName.search(/Mark[-_]?UP/i);

  if (colorToFind != -1) {

colorsToRemove.push(curSwatch.name);

  }

}

alert("Found color names:\r" + colorsToRemove.join("\r"));

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

  var curItem = allPItems;

  var fColorName = curItem.fillColor.name;

  var sColorName = curItem.strokeColor.name;

  for (var j = 0; j < colorsToRemove.length; j++) {

var curColorName = colorsToRemove;
if (fColorName == curColorName) {
  curItem.fillColor = "Black";
}
if (sColorName == curColorName) {
  curItem.strokeColor = "Black";

  }

}

}

btw: the syntax highlighting destroyed the code

Kai

5 replies

m1b
Community Expert
Community Expert
March 10, 2023

Hi all, I'm still seeing this bug in ID 18.2. I couldn't find the bug in uservoice, so I created one.

Please vote for it here.

- Mark

JohnwhiteAuthor
Inspiring
June 17, 2016

great!

thanks Kai

thank so much.

regard

John

JohnwhiteAuthor
Inspiring
June 17, 2016

yes! not work.

John

Kai Rübsamen
Participating Frequently
June 17, 2016

John, what’s the goal? Only "MARKUP" or, "Mark-up", "Mark_UP" … which kind of objects? Example!

If FC does’t work, loop through all objects and check, if your color = your Color.

Kai

Kai Rübsamen
Kai RübsamenCorrect answer
Participating Frequently
June 17, 2016

Here is an idea, that includes also the previous question "MARKUP, markup, MARK-UP, MARK_UP":

app.doScript(main, ScriptLanguage.JAVASCRIPT , [], UndoModes.ENTIRE_SCRIPT, "Replace Colors");

function main() {

var curDoc = app.activeDocument;

var allSwatches = curDoc.swatches;

var allPItems = curDoc.allPageItems;

var colorsToRemove = [];

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

  var curSwatch = allSwatches;

  var curSwatchName = curSwatch.name;

  var colorToFind = curSwatchName.search(/Mark[-_]?UP/i);

  if (colorToFind != -1) {

colorsToRemove.push(curSwatch.name);

  }

}

alert("Found color names:\r" + colorsToRemove.join("\r"));

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

  var curItem = allPItems;

  var fColorName = curItem.fillColor.name;

  var sColorName = curItem.strokeColor.name;

  for (var j = 0; j < colorsToRemove.length; j++) {

var curColorName = colorsToRemove;
if (fColorName == curColorName) {
  curItem.fillColor = "Black";
}
if (sColorName == curColorName) {
  curItem.strokeColor = "Black";

  }

}

}

btw: the syntax highlighting destroyed the code

Kai

JohnwhiteAuthor
Inspiring
June 15, 2016

Thank you Ravindra

I have tried, but not work.

thank you so much.

regard

John

Kai Rübsamen
Participating Frequently
June 15, 2016

Does anybody get here a result?

app.findObjectPreferences.strokeColor = "Markup";

I tried it with swatch (not just the name) or color and get always an error!

It doesn’t also work, if I use the swatch by its index, instead of the name.

There were some posts in the forum about some bugs in CS 4/5, but it seems, that is broken in CC too?

Kai

milligramme
Inspiring
June 15, 2016

Hi

you can fix some part of this script.

var myDoc = app.activeDocument; 

var myColor = myDoc.swatches.item("MARKUP");

if (!myColor.isValid){

  alert("NO SWATCH");

  exit();

}

now you can't exit if the document don't have 'MARKUP' swatch.

but this code wont work in my InDesign CC, I found same issue

[JS CS4] findObjectPreferences.fillColor error |Adobe Community https://forums.adobe.com/thread/583184

I very much regret to say I don't have any idea to solve this problem.

thank you

mg

JohnwhiteAuthor
Inspiring
June 15, 2016

Thank you mg

so I changed it into like this:

var myDoc = app.activeDocument;

var myColor = myDoc.swatches.item("MARKUP");

if (!myColor.isValid){

alert("NO SWATCH");

exit();

}

app.findObjectPreferences = null;

app.findObjectPreferences.fillColor = myColor;

app.changeObjectPreferences.fillColor = "Black";

app.changeObject();

app.findObjectPreferences = null;

app.findObjectPreferences.strokeColor = myColor;

app.changeObjectPreferences.strokeColor = "Red";

app.changeObject();

but I got error.

How can I fix?

thanks

regard

John

Ravindra_KCS
Inspiring
June 15, 2016

Hi John,

You can use try catch for this as follows..

try{

    app.findObjectPreferences = null; 

    app.findObjectPreferences.fillColor = "Markup1"; 

    app.changeObjectPreferences.fillColor = "Test"; 

    app.changeObject(); 

    }

   catch(err)

   {

        app.findObjectPreferences = null; 

        app.findObjectPreferences.strokeColor = "Markup"; 

        app.changeObjectPreferences.strokeColor = "Black"; 

        app.changeObject(); 

    }

Thanks and Regards

Ravindra