Skip to main content
rarur97798982
Inspiring
Apr 16, 2018 11:49 AM UTC
Answered

How to change the alignment of text using javascript?

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 is closed to new replies. Start a new post to keep the conversation going.
pixxxelschubser
pixxxelschubserCorrect answer
Community Expert
Apr 16, 2018 7:28 PM UTC

Hi,

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

Try:

theEvilNameOfYourOwnVariable.textRange.justification = Justification.CENTER;

4 replies

pixxxelschubser
Community Expert
pixxxelschubserCommunity ExpertCorrect answer
Community Expert
Apr 16, 2018 7:28 PM UTC

Hi,

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

Try:

theEvilNameOfYourOwnVariable.textRange.justification = Justification.CENTER;

Translate
rarur97798982
Inspiring
Apr 17, 2018 7:39 AM UTC

Wow that worked... Thank-you

Translate
sync0x
Participant
May 21, 2023 4:39 AM UTC

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.

 

 

Translate