Copy link to clipboard
Copied
Hi,
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 prining 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 (always get undefined) ?
I'm using Illustrator CS5.
Copy link to clipboard
Copied
$.writeln(app.activeDocument.selection[0].position); // (781,250) works fine here, ruler origin is at bottom left (CS4).
to get x
$.writeln(app.activeDocument.selection[0].position[0]); // (781)
Copy link to clipboard
Copied
Position returns an Array of 2 numbers relative to the document origin but this is NOT always bottom left. You should reset this first if thats where you need it to be. If you want top,bottom,left,right then look at getting one of the bounds properties… (visibleBounds is best as it includes the strokes). All measurements are in points so you have no need to convert for pixels. (based at 72dpi).
Copy link to clipboard
Copied
Thanks Mark,
I had a look at the Object Model and saw visibleBounds and geometricBounds for PageItem.
This confuses me a bit, are these properties Rect or Rectangle instances ?
In the docs it says Rect, but clicking or looking for Rect leads me nowhere.
If I try to use Rectangle properties (x,y / top,left) I get undefined.
These properties seem to return an array of for elements.
Here's what I get if I display the values in the order you mentioned:
alert('geometricBounds: \n[top: '+sel.geometricBounds[0].toFixed(2)+'\nbottom: '+sel.geometricBounds[1].toFixed(2)+'\nleft: '+sel.geometricBounds[2].toFixed(2)+'\nright: '+sel.geometricBounds[3].toFixed(2)+']');
Will this order always be the same ?
How do I convert from points to pixels ?
Copy link to clipboard
Copied
The order is, in fact, [ top left bottom right ]. If you use that, you'll see at least the correct width (right minus left) and height (bottom minus top).
Maybe the geometricBounds don't adjust to your current ruler zero point (I would have expected that, actually), and hence the 'left' and 'top' are off.
How do I convert from points to pixels ?
"Pixels" is a non-existing measurement unit. You will see Illustrator is lying to you if it says something is 'xx pixels wide' -- it's just the size in points, but with "px" added instead of "pt". As Illustrator's internal measurements are all in points, you will always get your values in points, even if you are using millimeters or inches in the interface. Since 1 pixel == 1 point (according to Adobe, at least!), you don't have to convert anything.
Copy link to clipboard
Copied
It would make more sense with the order being [ top left bottom right ] as you mentioned,
but still I don't fully get it. I'm printing the geometric bounds in the original order [0,1,2,3],
which means that, using the [ top left bottom right ] order, 110 would by x/left and 370 would
be y/top, but my object is at 140 on y/top and 120 on x/left.
What are the numbers I'm getting ? I noticed that if I subtract the last element of the list,
from the second I get 100, similarly, if I subtract the 1st element from the 3rd, I get 100,
which would be width/height for my path. My 1st guess for the order was [x,h,w,y], but's
too counter-intuitive to be true.
With the [ top left bottom right ] order, 110 should be y, although it's 10px off from x.
I don't think I'm understanding things right
Copy link to clipboard
Copied
They are a rectangular bounding box. There are no 'rectangles' in Illustrator that's Indesign that has those objects. You only get 'rectangle' in the object model if you are using pathItems to create a new shape… Your preview has strokes so I would NOT use 'geometricBounds' nor would I use height & width as these are calculated off 'geometricBounds' use 'visibleBounds' and do the math for height & width ditto this for x,y position. Order is always the same.
Copy link to clipboard
Copied
At the moment I'm interested in getting x(same as left, correct ?) and y(same as top ?)
position.
visibleBounds returns values including the strokes in the same order as geometricBounds.
if I use visibleBounds[1] for left/x and visibleBounds[0] for top/y, the values don't look close to what I
see in the Transform Panel.
Copy link to clipboard
Copied
Hello Carlos,
What do you mean by:
ruler origin is at bottom left
Here's what I get if do an alert using the position property:
1st element seems close to x, but the second makes no sense to me.
I can't figure out what I'm missing out on here
Copy link to clipboard
Copied
Sorry I have only just noticed in you first post that you are using CS5. Origins shift in this between bottom left where they have always been to top left. Im not sure how that works now…
Copy link to clipboard
Copied
Mark (and mr. OP),
As Javascript internally will always report dimensions in points, rather than in the "active" units on screen, it's highly probable that the vertical inversion works the same -- you see in on the screen but it doesn't appear as such inside a script.
So the vertical positions start somewhere at the bottom -- of the screen, of the current artboard, of the current window ... Nothing left but to experiment, I'm afraid.
Copy link to clipboard
Copied
I meant the 0,0 you see on the rulers meet at the bottom left corner of the artboard, in your case (CS5) it should be at the top left unless you moved it on purpose or accidentally. In Javascript it means from the top/left corner; moving right will be positive X and moving down will be positive Y. In CS4 and previous versions moving up is positive Y.
Regardless of version or if the ruler origin is moved, javascript should return the same values you see on the screen as long as the Reference Point is set to the top/left on the GUI, as you have it on your screen shots.
I have no idea why you're getting those results, why don't you start a new document and add a rectangle and try again.
Before you do that, let's see more of your artboard. Post a pic including both ruler Zeros.
Copy link to clipboard
Copied
I'm back to this old issue.
I've followed CarlosCanto's advice, and noticed the two different cartesian coordinate systems:
- CS5 one (0,0 is at top left, Y increases downwards)
- CS4 one (0,0 is at the centre of the artboard, Y increases upwards)
This allows me to get the correct values.
Is there a way to find out which coordinate system is used by the activeDocument ?
(Could I check if it's a CS4 or CS5 document ? Also, I've noticed the coordinate system
doesn't change if I have CS4 document saved as a CS5 document)
Thanks
Copy link to clipboard
Copied
There is a new app method for CS4/5 which converts between document & artboard cordinates but I personally have not had the time to check out how this works and where it would be of use…
Copy link to clipboard
Copied
there's no need to find out the creator version, the active document coordinate system is based on the application used, for example if you used CS4 to save a document with the ruler origin at bottom/left, when you open the same document in CS5 the ruler origin will be at top/left (at least artboard rulers, since previous to CS4 there were no separate artobard/global rulers).
if you still need to find out the creator version, Mark made a script for it, look here
Copy link to clipboard
Copied
Big thanks guys!
Mark's suggestions on determining the .ai file version are great.
I've used Carlos' idea to convert from one coordinate system to another.
At least I don't have to keep track of the document dimensions to do the offset manually.
Currently I'm getting the values for use in actionscript, so I need to keep track of the symbol's
dimension(centre).
This small snippet does the job for me at the moment:
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.position, CoordinateSystem.DOCUMENTCOORDINATESYSTEM, CoordinateSystem.ARTBOARDCOORDINATESYSTEM) : sel.position;
code += 'new Vec('+(pos[0] + (sel.width * .5)).toFixed(2) + ' , ' + Math.abs((pos[1] - (sel.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);
Copy link to clipboard
Copied
you're welcome, glad to help
Copy link to clipboard
Copied
Is there a way to get the topLeft position of a layer in respect to the artboard?
Copy link to clipboard
Copied
layers have no relation to artboards, if you need to find out the position of the top/left most bounds out of all objects on a layer, cycle thru all pageItems in such layer and get the top most and the left most out of them. Then compare it to the ArtboardRect property values of a given Artboard.
Find more inspiration, events, and resources on the new Adobe Community
Explore Now