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.

有帮助吗?

解决方案

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

It seems Illustrator has different coordinate systems: a document coordinate system and an artboard coordinate system. 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 hasDocCoords = app.coordinateSystem == CoordinateSystem.DOCUMENTCOORDINATESYSTEM;
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 = hasDocCoords ? doc.convertCoordinate (sel[i].position, CoordinateSystem.DOCUMENTCOORDINATESYSTEM, CoordinateSystem.ARTBOARDCOORDINATESYSTEM) : sel[i].position;
    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.

其他提示

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.

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];
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top