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

comparison of objects on X/Y axis

Community Beginner ,
Mar 17, 2021 Mar 17, 2021

Hi, I have this script where I:

  1. recognize number of objects in a layer
  2. recognize number of paths with specified colors
  3. position of object I choose

 

 

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?

 
 
TOPICS
Scripting
1.6K
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 2 Correct answers

Community Expert , Mar 18, 2021 Mar 18, 2021

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. 

Translate
Community Expert , Mar 18, 2021 Mar 18, 2021

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.

 

Screenshot 2021-03-18 at 8.39.11 PM.png

 

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

...
Translate
Adobe
Guide ,
Mar 17, 2021 Mar 17, 2021

Your script shows me the correct coordinates, in points.  Are you using points in Illustrator? 

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
Community Beginner ,
Mar 18, 2021 Mar 18, 2021

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?

 

 

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
Community Beginner ,
Mar 18, 2021 Mar 18, 2021

properties1.pngproperties2.png

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
Community Expert ,
Mar 18, 2021 Mar 18, 2021

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. 

Best regards
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
Community Beginner ,
Mar 18, 2021 Mar 18, 2021

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:

coordinates.png

x and y axis are starting almost in the middle of the artboard! Is there something wrong with my artboard?

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
Community Expert ,
Mar 18, 2021 Mar 18, 2021

Could you please attach ai file?

This must be something related to the units.

Best regards
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
Community Beginner ,
Mar 18, 2021 Mar 18, 2021

Here

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
Community Beginner ,
Mar 18, 2021 Mar 18, 2021

It wont let me upload the ai file, will pdf file do?

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
Community Expert ,
Mar 18, 2021 Mar 18, 2021

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.

 

Screenshot 2021-03-18 at 8.39.11 PM.png

 

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.

 

Best regards
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
Community Beginner ,
Mar 18, 2021 Mar 18, 2021
LATEST

I see, now it makes sense.

Thank you, this is all I need

 

Have a nice day

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
Community Expert ,
Mar 18, 2021 Mar 18, 2021

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?

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