Copy link to clipboard
Copied
Hi,
I am trying to push an array from a loop to return CMYK total values for x2 selected Illustrator object. This then alerts the total CMYK values for both object, then individually alerts the total values for each object and then alerts if the top object is lighter, darker or the same total values.
The alerts it gives are the correct values but for some reason in the if and if else statements of seeing if the value is greater than, less than or equal to fails and does not give the required results, if I manually enter the same values into the array the if and if else statements return the required results. What am I doing wrong here?
var doc = app.activeDocument;
if (doc.selection.length == 2) {
var usedColourTotal;
var cmykTotalArray = [];
for (var i = 0; i < doc.selection.length; i++) {
var usedColourC = doc.selection[i].fillColor.cyan;
var usedColourM = doc.selection[i].fillColor.magenta;
var usedColourY = doc.selection[i].fillColor.yellow;
var usedColourK = doc.selection[i].fillColor.black;
var usedColourTotal = [usedColourC + usedColourM + usedColourY + usedColourK];
cmykTotalArray.push(usedColourTotal);
}
var value1 = cmykTotalArray[0];
var value2 = cmykTotalArray[1];
alert(cmykTotalArray)
alert(value1);
alert(value2);
if (value1 > value2) {
alert("Top object = Darker\nvalue1 = " + value1 + "\nvalue2 = " + value2);
} else if (value1 < value2) {
alert("Top object = Lighter\nvalue1 = " + value1 + "\nvalue2 = " + value2);
} else if (value1 == value2) {
alert("Both objects = The same colour\nvalue1 = " + value1 + "\nvalue2 = " + value2)
}
} else {
alert("Please make sure only 2 page items are selected.")
}
1 Correct answer
I can't try running the script presently (so I can't replicate your problem), I'm just going by reading it. What happens if you change line 10 to:
var usedColourTotal = usedColourC + usedColourM + usedColourY + usedColourK;
Explore related tutorials & articles
Copy link to clipboard
Copied
Why is "usedColourTotal" an array and not a primitive number? You're pushing arrays into an array and comparing arrays.
Copy link to clipboard
Copied
Ok, sounds like I have gone about this in an incorrect way. This seemed the right way to do this (still new to this).
I was trying to capture the total values from both objects in the loop and push them in the array to compare. Is there a better way to do this?
Many thanks in advance.
Copy link to clipboard
Copied
I can't try running the script presently (so I can't replicate your problem), I'm just going by reading it. What happens if you change line 10 to:
var usedColourTotal = usedColourC + usedColourM + usedColourY + usedColourK;
Copy link to clipboard
Copied
Ahhh, just realised my error, I had put line 10 into an array instead of just pushing the number (making clear your firsst message).
That has worked. thanks for your help on this, silly error but could not see the wood for the trees.

