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

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

Community Beginner ,
Jan 29, 2011 Jan 29, 2011

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.

TOPICS
Scripting
10.8K
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
Adobe
Community Expert ,
Jan 30, 2011 Jan 30, 2011

$.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)

position.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
Guide ,
Jan 30, 2011 Jan 30, 2011

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

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 ,
Jan 30, 2011 Jan 30, 2011

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)+']');

Picture 17.png

Will this order always be the same ?

How do I convert from points to pixels ?

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
Advocate ,
Jan 30, 2011 Jan 30, 2011

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.

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 ,
Jan 30, 2011 Jan 30, 2011

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

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
Guide ,
Jan 30, 2011 Jan 30, 2011

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.

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 ,
Jan 30, 2011 Jan 30, 2011

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.

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 ,
Jan 30, 2011 Jan 30, 2011

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:

Picture 18.png

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

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
Guide ,
Jan 30, 2011 Jan 30, 2011

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…

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
Advocate ,
Jan 30, 2011 Jan 30, 2011

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.

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 ,
Jan 30, 2011 Jan 30, 2011

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.

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 22, 2011 Mar 22, 2011

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

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
Guide ,
Mar 22, 2011 Mar 22, 2011

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…

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 22, 2011 Mar 22, 2011

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

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 25, 2011 Mar 25, 2011

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);
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 25, 2011 Mar 25, 2011

you're welcome, glad to help

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
Guest
Apr 06, 2011 Apr 06, 2011

Is there a way to get the topLeft position of a layer in respect to the 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 ,
Apr 06, 2011 Apr 06, 2011
LATEST

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.

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