How do I get the position of the selected Illustrator PathItem in pixels using?

George Profenza picture George Profenza · Jan 30, 2011 · Viewed 7.2k times · Source

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.

Answer

George Profenza picture George Profenza · Mar 26, 2011

@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.