Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Loop push array not working in if statement

Participant ,
Jul 22, 2021 Jul 22, 2021

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.")
}

 

TOPICS
Scripting
420
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Guide , Jul 22, 2021 Jul 22, 2021

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;

 

Translate
Adobe
Guide ,
Jul 22, 2021 Jul 22, 2021

Why is "usedColourTotal" an array and not a primitive number?  You're pushing arrays into an array and comparing arrays.  

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Jul 22, 2021 Jul 22, 2021

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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
Jul 22, 2021 Jul 22, 2021

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;

 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Jul 22, 2021 Jul 22, 2021
LATEST

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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines