Skip to main content
josiearz
Participating Frequently
February 8, 2022
Answered

Move text with JavaScript

  • February 8, 2022
  • 5 replies
  • 1150 views

How do I move a Text Frame using JavaScript? The scripting guide says there is a .move() method that takes a relativeObject and an insertionLocation, but I'm not sure what these parameters refer to. Assigning textframe.position to a pair of coordinates is not working the way I expect it to (the text frame ends up somewhere else and I'm not sure why!). 

 

Code below. Any help would be appreciated!

 

// destination is the name of the document
var layer = destination.layers[0];
var nametext = layer.textFrames.add();
nametext.contents = "Name LastName"

var charStyle = destination.characterStyles.add("NameStyle");
var charAttr = charStyle.characterAttributes;

charAttr.size = 55;
charAttr.capitalization = FontCapsOption.ALLCAPS;
charAttr.textFont = app.textFonts.getByName("CormorantGaramond-Bold");


charStyle.applyTo(nametext.textRange);

nametext.position = [306, 368.9956]; // Does not seem to do anything

 

This topic has been closed for replies.
Correct answer pixxxelschubser

use

nametext.position = [306, 368.9956+newText.height];

instead

5 replies

femkeblanco
Legend
February 8, 2022

This should give you two "point text" textFrames, the first at the document's top left and the second at the document's centre.  Note that y is negative. 

 

var doc = app.activeDocument;
var nametext1 = app.activeDocument.textFrames.add();
nametext1.contents = "Name LastName 1";
nametext1.position = [0, 0];
var nametext2 = app.activeDocument.textFrames.add();
nametext2.contents = "Name LastName 2";
nametext2.position = [doc.width / 2, - doc.height / 2];

 

Is this what you get?  If so, you can work it out from there. 

 

 

pixxxelschubser
Community Expert
pixxxelschubserCommunity ExpertCorrect answer
Community Expert
February 8, 2022

use

nametext.position = [306, 368.9956+newText.height];

instead

josiearz
josiearzAuthor
Participating Frequently
February 8, 2022

Thank you!

pixxxelschubser
Community Expert
Community Expert
February 8, 2022

Again:

 

PageItem.position  
Data Type: Point
Adobe Illustrator 26 Type Library
The position of the top left corner of the art item.

pixxxelschubser
Community Expert
Community Expert
February 8, 2022

Are we really talking about the top left position of your text frame?

 

Please explain a bit more (maybe with screenshots before/after)

The coordinate system has been changed in the last versions.

Therefore say:

  • Illustrator Version
  • Document size
  • Unit of measurement?
josiearz
josiearzAuthor
Participating Frequently
February 8, 2022

Illustrator version: 26.0.3

Document size: 792 x 612 px

Units: pixels

 

I'm writing a script to essentially duplicate a document, but replace the name. In the document I am duplicating, the bottom left corner (yes, bottom left) of the text frame containing the name is at [75.7461, 398.9502] (new coordinates but the issue is the same). 

So my assumption would be that putting those into the .position() method would result in the text frame appearing in the same place. But when I run the code, the result is this:

The workaround I found is to just put 453.3027 in as the y coord. Then it ends up at 398.9502, where I wanted it. I just have no idea why.

pixxxelschubser
Community Expert
Community Expert
February 8, 2022

At first - do not use layer as variable name. This causes confusion.

 

Where should the text frame be moved to?

  • To another position in the hierarchy of the layer palette ( move() )?
  • Or to another xy position in the document? ( see the following snippet )

 

 

 

/* var layer = destination.layers[0];*/
var aLay = app.activeDocument.layers[0]; // don't use layer as variable name
var nametext = aLay.textFrames.add();
nametext.contents = "Name LastName"

//var charStyle = destination.characterStyles.add("NameStyle");
//var charAttr = charStyle.characterAttributes;

//charAttr.size = 55;
//charAttr.capitalization = FontCapsOption.ALLCAPS;
//charAttr.textFont = app.textFonts.getByName("CormorantGaramond-Bold");


//charStyle.applyTo(nametext.textRange);
alert (newText.position);
nametext.position = [306, 368.9956];
redraw();
alert ("position changed");

 

 

 

 

 

josiearz
josiearzAuthor
Participating Frequently
February 8, 2022

Thanks--I'm trying to move it to another xy position on the same layer. I ran your snippet and the text ends up with the right x coordinate but the wrong y coordinate (436.0488 measured at the bottom left corner of the frame). Any idea why this is happening?