Skip to main content
brian_p_dts
Community Expert
Community Expert
February 1, 2020
Answered

New here - my y coordinate position is reversed

  • February 1, 2020
  • 3 replies
  • 2549 views

Hi all - dipping my toes into Illustrator scripting; mostly have done InDesign to date. 

 

I'm passing a top level layer to the following function: 

 

var makeFrame = function(layer) {    
    var tf = layer.textFrames.add();
    tf.contents = str;
    tf.position = [72, 0];
}

The x coordinate positions correctly, but the y coordinate is reversed. If I put [72, 2448] (the height of the artboard), then it positions at the top of the page. Instead, it's positioning just under the artboard. I've checked the anchor point and measurement settings. Any other things I'm not thinking of? Thanks. 

This topic has been closed for replies.
Correct answer pixxxelschubser

Cheers mate.

 

I'll do the annoying thing of answering your question with a question. Do you really need to move the origin and potentially complicate/break other actions/calculations? If you know exactly where the program sets the origin by default, can you not write your calculations based around that?

 

Also, did you intentionally write "upper right" in reference to the origin? I ask because the origin was previously in the bottom left and is now in the top left.


You are right. Illustrator scripting is not so comfortable as in other programs.

brianp311 wrote:

"… So is there any way, if a doc was created dynamically, to revert the y-axis to the upper right? Can I change the page origin property? "

 

Changing this property or reverting the y-axis causes other problems - which you don't want!

 

That's why:

TextFrames.add() creates a new text frames with anchorpoint [0,0] for the baseline of that frame.

Sure - the easiest way for moving the text frame to the bottom - seems to be to use the position property with a negativ y-value --> TextFrame.position[72, -2448]

tf.position = [72, -2448];

 

But there is one problem with this method: position values are [left, top]. And using this property - now the top of your text frame will be at position -2448pt (34in) and not longer the baseline. And I'm very sure, that is also not what you really want. Because of your text frame will be (at the bottom) outside of your artboard.

 

Assumed you want a text frame with font-baseline-anchorpoint-position [72, -2448]

Create the textframe and move it with translate method. Use the artboardRect values for exact positioning.

 

For example -->

var aDoc = app.activeDocument;
var ABR = aDoc.artboards[0].artboardRect;

var tf = aDoc.activeLayer.textFrames.add();
tf.contents = "abcdefg";
tf.translate(72, ABR[3]);

 

Have fun

😉

 

3 replies

pixxxelschubser
Community Expert
Community Expert
February 2, 2020

Ok.

There seems to exists a newer (better) variant in Illustrator scripting:

var tf = app.activeDocument.textFrames.pointText([50, app.activeDocument.artboards[0].artboardRect[3]]);
tf.contents = "abc";

 

pixxxelschubser
Community Expert
Community Expert
February 2, 2020

I saw there is now one correct answer in the thread.

@brianp311

Only for interesting: Did you tried the code snippets that I shown before?

 

brian_p_dts
Community Expert
Community Expert
February 2, 2020

I did. Thanks for the explanation. 

Michael Bullo
Community Expert
Community Expert
February 1, 2020

This might help...

 

https://illustrator-scripting-guide.readthedocs.io/scripting/positioning/

 

For the artboard, the default coordinate origin, (0,0), is the top-left corner, reflected in the ruler origin property of the artboard object. X coordinate values increase from left to right, and Y values increase from top to bottom. This changed in the CS5 release; to maintain script compatability, a document created by a script still uses the older system, with the origin at the bottom left of the artboard, and the Y value increasing from bottom to top. The page origin property of a document object defines the bottom-left corner of the printable region of the document as a fixed point.

brian_p_dts
Community Expert
Community Expert
February 2, 2020

Thanks, Mike. That explains the behavior. So is there any way, if a doc was created dynamically, to revert the y-axis to the upper right? Can I change the page origin property? 

Michael Bullo
Community Expert
Community Expert
February 2, 2020

Cheers mate.

 

I'll do the annoying thing of answering your question with a question. Do you really need to move the origin and potentially complicate/break other actions/calculations? If you know exactly where the program sets the origin by default, can you not write your calculations based around that?

 

Also, did you intentionally write "upper right" in reference to the origin? I ask because the origin was previously in the bottom left and is now in the top left.