Skip to main content
rarur97798982
Inspiring
April 16, 2018
Answered

How to change the alignment of text using javascript?

  • April 16, 2018
  • 1 reply
  • 3673 views

Hi,
I need to change the alignment of text to center.

for (var i = 1 ; i <= graphNumberOfLines + 1; i++)

            {

               var ii=i;

               ii= doc.textFrames.add();  //adding text

               ii.textRange.characterAttributes.size = 8;  //character size for text

               ii.textRange.characterAttributes.textFont = app.textFonts.getByName("Times-Roman");

                ii.textRange.StyleRunAlignmentType = "Center";

               ii.top = firstPoint[1]+yGraphInc;  //x coordinate for the new value

               ii.left = firstPoint[0]+xGraphInc;  //y coordinate for the new value

               finalContent=Number(graphFirstValue)+graphIncValue;

               finalContent=finalContent.toString();

               var finalContentcheck=Number(finalContent.indexOf("-"));

               if(finalContentcheck>=0){

                   finalContent=finalContent.replace(/-/g,"–"); 

               }

               ii.contents=(finalContent);  //content for the text frame

               xGraphInc=(xGraphDiff/graphNumberOfLines)*i;  //incrementing the x coordinate

               yGraphInc=(yGraphDiff/graphNumberOfLines)*i;  //incrementing the y coordinate

               if(graphValues[1]>=0){

                    graphIncValue=graphIncValue+graphIncrement;  //incrementing the content

               }else{

                   graphIncValue=graphIncValue-graphIncrement;  //incrementing the content

               }

            }

This is how I am writing in the document. I need to align just after writing.
Thanks in advance.

This topic has been closed for replies.
Correct answer pixxxelschubser

Hi,

and again - it is really better if you don't use ii as variable name!

Try:

theEvilNameOfYourOwnVariable.textRange.justification = Justification.CENTER;

1 reply

pixxxelschubser
Community Expert
pixxxelschubserCommunity ExpertCorrect answer
Community Expert
April 16, 2018

Hi,

and again - it is really better if you don't use ii as variable name!

Try:

theEvilNameOfYourOwnVariable.textRange.justification = Justification.CENTER;

rarur97798982
Inspiring
April 17, 2018

Wow that worked... Thank-you

sync0x
Participant
May 21, 2023

Does anyone know how to vertically align text to the centre with a script? In the Illustrator properties dialog for a text box, we find horizontal alignment under "Paragraph," and vertical alignment under "Area Type." I do not know the equivalent property and object parents in JavaScript to accomplish exactly this; yet, it must be done, as I wish to insert measurement legends at the proper place in my diagrams.

 

So for horizontal:

  myText.textRange.justification = Justification.CENTER;

 

And for vertical:

  ???

 

We are not going to find it in textRange because there could be many of these with their own horizontal alignments, and only one vertical alignment is applicable for the text box.