Move text with JavaScript

Community Beginner ,
Feb 08, 2022 Feb 08, 2022

Copy link to clipboard

Copied

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

 

TOPICS
Scripting

Views

82

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
community guidelines

correct answers 1 Correct answer

Adobe Community Professional , Feb 08, 2022 Feb 08, 2022
use nametext.position = [306, 368.9956+newText.height]; instead

Likes

Translate

Translate
Adobe Community Professional ,
Feb 08, 2022 Feb 08, 2022

Copy link to clipboard

Copied

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");

 

 

 

 

 

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
community guidelines
Community Beginner ,
Feb 08, 2022 Feb 08, 2022

Copy link to clipboard

Copied

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?

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
community guidelines
Adobe Community Professional ,
Feb 08, 2022 Feb 08, 2022

Copy link to clipboard

Copied

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?

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
community guidelines
Community Beginner ,
Feb 08, 2022 Feb 08, 2022

Copy link to clipboard

Copied

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

Screen Shot 2022-02-08 at 1.31.57 PM.png

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:

Screen Shot 2022-02-08 at 1.33.10 PM.png

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.

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
community guidelines
Adobe Community Professional ,
Feb 08, 2022 Feb 08, 2022

Copy link to clipboard

Copied

Again:

 

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

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
community guidelines
Adobe Community Professional ,
Feb 08, 2022 Feb 08, 2022

Copy link to clipboard

Copied

use

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

instead

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
community guidelines
Community Beginner ,
Feb 08, 2022 Feb 08, 2022

Copy link to clipboard

Copied

LATEST

Thank you!

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
community guidelines
Advocate ,
Feb 08, 2022 Feb 08, 2022

Copy link to clipboard

Copied

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. 

 

femkeblanco_0-1644353588949.png

 

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
community guidelines