Highlighted

Confused about how coordinates work in scripts

Participant ,
Jun 21, 2020

Copy link to clipboard

Copied

So strictly speaking inside of a script, the origin is the lower left corner of the artboard... Then say with the sample code here:

 

myDoc.pathItems.rectangle(8, 4, 4, 4);

 

This goes (x, y, x, y) ?  and the 2nd x, y is in relation to the first point, not to the origin?  And x being positive is always to the right, and y being positive is always up?

TOPICS
Scripting

Views

192

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more

Confused about how coordinates work in scripts

Participant ,
Jun 21, 2020

Copy link to clipboard

Copied

So strictly speaking inside of a script, the origin is the lower left corner of the artboard... Then say with the sample code here:

 

myDoc.pathItems.rectangle(8, 4, 4, 4);

 

This goes (x, y, x, y) ?  and the 2nd x, y is in relation to the first point, not to the origin?  And x being positive is always to the right, and y being positive is always up?

TOPICS
Scripting

Views

193

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Jun 21, 2020 0
Contributor ,
Jun 21, 2020

Copy link to clipboard

Copied

The coordinates in Illustrator are a bit confusing (confused?). In artboard coordinates, the 0,0 point is top left of artboard.

 

Try this:

var top = 10, left = 20, width = 30, height = 40;
app.activeDocument.pathItems.rectangle(-top, left, width, height);

 

Notice the minus sign before 'top'? This is because the illustrator Y axis starts at 0 at the top of artboard, and goes down. So -10 means 10 down from the top of the artboard.

 

FYI, I mentioned artboard coordinates earlier. This means that values are relative to the active artboard. So when drawing with a script, the artboard matters. If that's a problem for your script, look into document coordinates.

 

Regards,

Mark

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jun 21, 2020 0
Participant ,
Jun 21, 2020

Copy link to clipboard

Copied

I keep playing around with my numbers trying to wrap my head around things, and with my line of code I posted originally, all my shapes are appearing in the lower left corner of the artboard as I keep my numbers hovering around all 0's, giving me the impression that the lower left is the origin?

 

Oh, it's not x,y x,y .. it's y, x, width, height?

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jun 21, 2020 0
Contributor ,
Jun 21, 2020

Copy link to clipboard

Copied

When I run my above code I get a box that's 10 down and 20 to the right of the top left of the artboard. Where does it appear when you run my code?

 

Yes, it is top, left, width, height. I find it helps during a learning phase to use different values for each (eg. 10,20,30,40), so I can get my head around it more easily.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jun 21, 2020 1
Community Beginner ,
Jun 23, 2020

Copy link to clipboard

Copied

There are also application-level preferences that dictate how coordinates are referenced in relation to the origin.

 

preferences.setBooleanPreference( "isRulerOriginTopLeft", true )
preferences.setBooleanPreference( "isRulerIn4thQuad", true )

 

Without explicitly setting these, two machines could perform the same script on the same document and get differing results.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jun 23, 2020 2
Contributor ,
Jun 23, 2020

Copy link to clipboard

Copied

Great info! Thanks 🙂

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jun 23, 2020 1
Adobe Community Professional ,
Jun 21, 2020

Copy link to clipboard

Copied

as if things weren't confusing enough, here's the kicker. If your script creates the document the origin is bottom/left. If your script references an already open document, the origin is top/left.

 

that might seem absolutely crazy, but it kinds of make sense. Adobe changed the origin from bottom/left to top/left in CS5. So in order to not break existing scripts, origin had to remain as it was (as in CS4) when the script creates documents, but accessing existing documents had to align with the new and shining Cartesian system (CS5).

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jun 21, 2020 2
Contributor ,
Jun 21, 2020

Copy link to clipboard

Copied

Ouch! That is a horrible kicker. Thanks for the info.

Maple_Stirrup does your script create the document? My test was on a new blank document created via the normal UI.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jun 21, 2020 0
Participant ,
Jun 22, 2020

Copy link to clipboard

Copied

Yeah my script did create the document, this is all great information, I appreciate it.

 

 

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jun 22, 2020 0
Participant ,
Jun 23, 2020

Copy link to clipboard

Copied

Okay does the artboard rect work the same as making a rectangle?  It's y, x, width, height?

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jun 23, 2020 1
Community Beginner ,
Jun 23, 2020

Copy link to clipboard

Copied

Unfortunately no. artboardRect returns an array in the form [ x1, y1, x2, y2 ].

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jun 23, 2020 2
Adobe Community Professional ,
Jun 23, 2020

Copy link to clipboard

Copied

it is [left, top, right, bottom]

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jun 23, 2020 3
Enthusiast ,
Jun 24, 2020

Copy link to clipboard

Copied

Salut!
Une méthode à l'ancienne pour ne pas se préoccuper de la position de l'origine globale.

Il suffit de faire un choix au départ du script? (se référer au bas ou au haut du plan de travail)

Mais depuis il y a d'autres méthodes mais celle ci peut aider à comprendre...

 

 

// elleere Wed, 24 June 2020 08:11:52 GMT
// Traitement dans le plan de travail actif
// orignine fictive en haut à gauche avec rulerOrigin
var docRef = app.activeDocument;
var Origin = docRef.rulerOrigin;
var hauteur = docRef.height;
var origX = -Origin[0];
var origY = -Origin[1]+hauteur;

var top = origY-50, left = origX+50, width = 60, height = 40;
docRef.pathItems.rectangle(top, left, width, height);
// orignine fictive en bas à gauche avec rulerOrigin
    var origX = -Origin[0];
    var origY = -Origin[1];
var  height = 40, top = origY+50+height, left = origX+50, width = 60;
docRef.pathItems.rectangle(top, left, width, height);

 

 

de elleere

 

 

 

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jun 24, 2020 0