4

I have a simple problem, but can't seem to find my way around it: I have PathItem and Illustrator points out that it is at position (781px,250px).

How can I get those values in jsx ?

I've noticed that the PathItem inherits the position property from PageItem, and position is a Point, but when I try to print the values, I get undefined:

$.writeln(app.activeDocument.selection[0].position.x);

If I leave out .x from the line above I get this printed in the console:

521,510

What are these values ? Are they x,y coordinates ? In what unit ? How can I convert to pixels ?

Why can I not access x,y/top,left properties ?

I'm using Illustrator CS5.

Mark Amery
  • 143,130
  • 81
  • 406
  • 459
George Profenza
  • 50,687
  • 19
  • 144
  • 218

3 Answers3

5

@bradido's answer is helpful, it is incomplete.

It seems Illustrator has different coordinate systems: an overall document coordinate system and a second based on the currently-active artboard (you may have several defined but only one is 'active' at a time.) One has the origin at the centre of the document while the other has the origin at top-left. Also it's Y values increase upwards.

It is wise to first check for the coordinate system using the app.coordinateSystem property, and if needed, there is a conversion function (doc.convertCoordinate) which handles the offset from the centre.

Here's an snippet which demonstrates how to retrieve x,y values for symbols in Illustrator, which can later be used in actionscript (using the coordinate system conversion):

var doc = app.activeDocument;
var sel = doc.selection;
var selLen = sel.length;
var code = 'var pointsOnMap:Vector.<Vec> = Vector.<Vec>([';
for(var i = 0 ; i < selLen ; i++){
    var pos = doc.convertCoordinate(sel[i].position, app.coordinateSystem, CoordinateSystem.ARTBOARDCOORDINATESYSTEM);
    code += 'new Vec('+(pos[0] + (sel[i].width * .5)).toFixed(2) + ' , ' + Math.abs((pos[1] - (sel[i].height*.5))).toFixed(2); // Math.abs(pos-height) - same for both coord systems ?
    if(i < selLen-1) code +=  '),';
    else             code +=  ')]);pointsOnMap.fixed=true;';
}
$.writeln(code);

For more details, see this thread on the Adobe Forums.

George Profenza
  • 50,687
  • 19
  • 144
  • 218
  • 1
    Just an FYI. You can eliminate `hasDocCoords` and your ternary operator by simply passing in `app.coordinateSystem` directly, like this: `var pos = doc.convertCoordinate(sel[i].position, app.coordinateSystem, CoordinateSystem.ARTBOARDCOORDINATESYSTEM);` – Mark A. Donohoe Nov 23 '20 at 01:24
  • For that matter, you can eliminate the final `if` clause and replace it with a ternary operator itself (e.g. `code += (i < selLen-1) ? '),' : ')]);pointsOnMap.fixed=true;';` – Mark A. Donohoe Nov 23 '20 at 01:27
  • 1
    Very good point ! I totally missed that back then. Thank you for pointing that out. I've updated the snippet as advised. Unfortunately I no longer have access to Illustrator to test: hopefully it still works :) – George Profenza Nov 23 '20 at 11:57
  • 1
    Glad I can help! And yes, it does work! Just wrote my own and used exactly that. :) – Mark A. Donohoe Nov 23 '20 at 20:45
2

The example above is good but is for a very specific task. Here is a function that will do the correct coordinate conversion and return the center:

function convertPoint(item){
    var pos = doc.convertCoordinate (item.position, CoordinateSystem.DOCUMENTCOORDINATESYSTEM, CoordinateSystem.ARTBOARDCOORDINATESYSTEM);
    pos[0] += item.width * 0.5;
    pos[1] = Math.abs(pos[1] - (item.height * 0.5));
    return pos;
}

You must pass the ITEM not the point. And I must vent: the need to do this is not a good idea.

Mark A. Donohoe
  • 28,442
  • 25
  • 137
  • 286
Chris Hill
  • 1,914
  • 1
  • 16
  • 24
2

The Point is an array of the positions. So to get the coordinates:

x = app.activeDocument.selection[0].position[0];

y = app.activeDocument.selection[0].position[1];
George Profenza
  • 50,687
  • 19
  • 144
  • 218
bradido
  • 21
  • 2
  • The values returned by selection[0].position are different from the ones visible in the property inspector (x,y values). That is my problem. – George Profenza Mar 21 '11 at 10:07