Home > Enterprise >  Illustrator Script- Java script- If conditions not working
Illustrator Script- Java script- If conditions not working

Time:03-16

I'm trying to make a script that will find the width of an item and check if the selected item width is the same and then show an alert.

For some reason my if condition is not working even when all the conditions are met.

Here is my code:

var doc = app.activeDocument;

function mm(n) {return n / 2.83464566929134;}
function pt(n) {return n * 2.83464566929134;}

//=====================
//   Calculate Prisa
//=====================
var prisa = doc.pathItems.getByName("prisa");
var prisawidth = mm(prisa.geometricBounds[2]-prisa.geometricBounds[0]);

//=========================
//   Calculate Selection
//=========================
var sel = doc.selection[0];
var selwidth = mm(sel.geometricBounds[2]-sel.geometricBounds[0]);

if (selwidth == prisawidth) {alert("Same Width");}

CodePudding user response:

You can try this:

if (Math.abs(selwidth - prisawidth) < .01) alert("About the same width");

I think the problem has to do with rounding of the numbers. Say, you can see '2 mm' and '2 mm', but actually there are '2.00001 mm' and '2.00002 mm' or something like this. So it's need either to round the fugures before compairing or check if the diffference is less than some minimal value.

  • Related