Copy link to clipboard
Copied
Hi, I have this script where I:
var doc = app.activeDocument;
var item = doc.layers['Stans'];
var listOfRed = [];
var listOfBlue = [];
var listOfBlack = [];
for (var i = 0; i < item.pageItems.length; i++) {
if (sameCMYK(item.pageItems[i].strokeColor, cmykColor (0, 100, 100, 0)))
listOfRed.push(item.pageItems[i]);
if (sameCMYK(item.pageItems[i].strokeColor, cmykColor (100, 0, 0, 0)))
listOfBlue.push(item.pageItems[i]);
if (sameCMYK(item.pageItems[i].strokeColor, cmykColor (0, 0, 0, 100)))
listOfBlack.push(item.pageItems[i]);
}
function cmykColor(c, m, y, k) {
var ret = new CMYKColor();
ret.cyan = c;
ret.magenta = m;
ret.yellow = y;
ret.black = k;
return ret;
}
function sameCMYK(color1, color2) {
if (color1.cyan == color2.cyan && color1.magenta == color2.magenta && color1.yellow == color2.yellow && color1.black == color2.black)
return true
else
return false
}
alert("number of lines in the Stans layer: " + item.pageItems.length);
alert("number of red lines: " + listOfRed.length + " number of blue lines: " + listOfBlue.length + " number of black lines: " + listOfBlack.length);
alert("rectangle position X: " + listOfRed[0].left + " Y: " + listOfRed[0].top);
But I need to compare position of chosen objects. Which one is more to the left? which one is below the other? How would I do that? What is also bugging me is that position coordinates in the script are different from the coordinates in illustrator when I select the object. How can the script write the right coordinates illustrator is showing?
What you are seeing is X, Y as of center point and you are using top, left coordinates. Try following snippet after selecting the object.
#target illustrator
function main() {
var doc = app.activeDocument, _item;
_item = app.selection[0];
alert("X : " + (_item.left + _item.width / 2) + ", Y : " + (-_item.top + _item.height / 2));
};
main();
Above script will give center X and Y coordinates of the selected item.
The coordinates return by script is based on the Global Rulers. So if you change ruler from Artboard ruler to Global rules. Please attached screen shot from to refer how to change it, then it will return correct coordinates. Right click on ruler and you will see option to change the ruler.
Since artboard does not start at X=0 and Y=0 therefore you see the difference. If artboard top,left is (0,0) then it will make no difference whether you change the ruler or not.
I hope I am able to make
...Copy link to clipboard
Copied
Your script shows me the correct coordinates, in points. Are you using points in Illustrator?
Copy link to clipboard
Copied
Yes, I have them in points. In screenshot below, rectangle coordinates are x:330 y:392. Script alert is telling me its - x: -17 y: -6. You're not getting these numbers?
Copy link to clipboard
Copied
Copy link to clipboard
Copied
What you are seeing is X, Y as of center point and you are using top, left coordinates. Try following snippet after selecting the object.
#target illustrator
function main() {
var doc = app.activeDocument, _item;
_item = app.selection[0];
alert("X : " + (_item.left + _item.width / 2) + ", Y : " + (-_item.top + _item.height / 2));
};
main();
Above script will give center X and Y coordinates of the selected item.
Copy link to clipboard
Copied
Thanks for you answer. When I try your script in new file, it works fine. However my original file is making mess of the coordinates. Take a look:
x and y axis are starting almost in the middle of the artboard! Is there something wrong with my artboard?
Copy link to clipboard
Copied
Could you please attach ai file?
This must be something related to the units.
Copy link to clipboard
Copied
Copy link to clipboard
Copied
It wont let me upload the ai file, will pdf file do?
Copy link to clipboard
Copied
The coordinates return by script is based on the Global Rulers. So if you change ruler from Artboard ruler to Global rules. Please attached screen shot from to refer how to change it, then it will return correct coordinates. Right click on ruler and you will see option to change the ruler.
Since artboard does not start at X=0 and Y=0 therefore you see the difference. If artboard top,left is (0,0) then it will make no difference whether you change the ruler or not.
I hope I am able to make you understand.
Copy link to clipboard
Copied
I see, now it makes sense.
Thank you, this is all I need
Have a nice day
Copy link to clipboard
Copied
Could it be that someone in the past changed the artboard in this old file and then manually set the origin of coordinates back to 0/0?
Find more inspiration, events, and resources on the new Adobe Community
Explore Now