Skip to main content
May 24, 2012
Answered

Trouble assigning TextFrame to Justification.Left

  • May 24, 2012
  • 3 replies
  • 5829 views

Hello,

I need some help trouble shooting a script.

I am trying to flip the justification for all of a selected set of text frames. However, while most types of flipping work properly, the following does not

if(sel.story.textRange.justification == Justification.RIGHT){

         sel.story.textRange.justification = Justification.LEFT;

}

I would expect the justification for items that are Justification.RIGHT to switch to Justification.LEFT, but they remain as Justification.RIGHT

It does not fail, it just does not work as expected.

What is odd is that it seems to work correctly for the three other types of justification I am flipping in the same script. See below for the full script.

Any ideas what is causing this?

try

{

          // Check current document for textFrames.

          if ( app.documents.length < 1 ) {

                    alert ( "open a document with text objects and select them." );

          }

          else {

                    docRef = app.activeDocument;

                    if ( docRef.textFrames.length < 1 ) {

                              alert ( "Select some text objects." );

                    }

                    //where text fames are selected swap the jusification

                    else {

                              sel = docRef.selection;

                              var sellen = sel.length;

                              for (var i=0;i<sellen ;i++)

                              {

                                        if(sel.typename == "TextFrame"){

                                                  if(sel.story.textRange.justification == Justification.LEFT){

                                                            sel.story.textRange.justification = Justification.RIGHT;

                                                  }

                                                  else if(sel.story.textRange.justification == Justification.RIGHT){

                                                            sel.story.textRange.justification = Justification.LEFT;

                                                  }

                                                  else if(sel.story.textRange.justification == Justification.FULLJUSTIFYLASTLINELEFT){

                                                            sel.story.textRange.justification = Justification.FULLJUSTIFYLASTLINERIGHT;

                                                  }

                                                  else if(sel.story.textRange.justification == Justification.FULLJUSTIFYLASTLINERIGHT){

                                                            sel.story.textRange.justification = Justification.FULLJUSTIFYLASTLINELEFT;

                                                  }

                                        }

                              }

                    }

          }

          

          

 

}

catch (e){

          alert("Script Failed!\n"+e);

}

Correct answer sttk3

This problem can be abstracted as not being able to set the same attributes via script as the style applied to the text.
Therefore, the problem can be bypassed by changing the style to another attribute, setting the attribute of your choice to the text, and then reverting back to the style.

 

Similar topics:

 

/**
  * @File set justification to text range. 
  * workaround issue where scripts cannot set the same type of justification as the style being applied to the text.
  * https://community.adobe.com/t5/illustrator-discussions/trouble-assigning-textframe-to-justification-left/td-p/4211277
  * @3653945.0.0
  * @7111211 sttk3.com
*/

(function() {
  if(app.documents.length <= 0) {return ;}
  var doc = app.documents[0] ;
  var sel = doc.selection ;
  if(sel.length <= 0) {return ;}
  
  var textRange = sel[0].story.textRange ;
  setJustification(textRange, Justification.LEFT) ;
})() ;

/**
  * set justification to text range
  * @9397041 {TextRange} textRange - target TextRange
  * @9397041 {Justification} dstJustification - Justification to apply
  * @Returns {void} 
*/
function setJustification(textRange, dstJustification) {
  var appliedParagraphStyle = textRange.paragraphStyles[0] ;
  var targetPropName = 'justification' ;
  var styleJustification = appliedParagraphStyle[targetPropName] ;

  var swapTable = {
    'Justification.LEFT': Justification.FULLJUSTIFYLASTLINELEFT, 
    'Justification.CENTER': Justification.FULLJUSTIFYLASTLINECENTER, 
    'Justification.RIGHT': Justification.FULLJUSTIFYLASTLINERIGHT, 
    'Justification.FULLJUSTIFYLASTLINELEFT': Justification.LEFT, 
    'Justification.FULLJUSTIFYLASTLINECENTER': Justification.CENTER, 
    'Justification.FULLJUSTIFYLASTLINERIGHT': Justification.RIGHT, 
    'Justification.FULLJUSTIFY': Justification.FULLJUSTIFYLASTLINELEFT
  } ;

  if(styleJustification === dstJustification) {
    appliedParagraphStyle[targetPropName] = swapTable[dstJustification.toString()] ;
    textRange[targetPropName] = dstJustification ;
    appliedParagraphStyle[targetPropName] = styleJustification ;
  } else {
    textRange[targetPropName] = dstJustification ;
  }
}

3 replies

m1b
Braniac
May 23, 2025

Here I am from FAR IN THE FUTURE! Everything is different now!

 

... except this bug, which is the same. I've lodged a bug report for this. Please vote on it. - Mark

 

P.S. as @robinfredericf noticed: after resizing some text properties cannot be modified. I've lodged a bug report for that too. Please vote on it also!

sttk3Correct answer
Braniac
May 23, 2025

This problem can be abstracted as not being able to set the same attributes via script as the style applied to the text.
Therefore, the problem can be bypassed by changing the style to another attribute, setting the attribute of your choice to the text, and then reverting back to the style.

 

Similar topics:

 

/**
  * @File set justification to text range. 
  * workaround issue where scripts cannot set the same type of justification as the style being applied to the text.
  * https://community.adobe.com/t5/illustrator-discussions/trouble-assigning-textframe-to-justification-left/td-p/4211277
  * @3653945.0.0
  * @7111211 sttk3.com
*/

(function() {
  if(app.documents.length <= 0) {return ;}
  var doc = app.documents[0] ;
  var sel = doc.selection ;
  if(sel.length <= 0) {return ;}
  
  var textRange = sel[0].story.textRange ;
  setJustification(textRange, Justification.LEFT) ;
})() ;

/**
  * set justification to text range
  * @9397041 {TextRange} textRange - target TextRange
  * @9397041 {Justification} dstJustification - Justification to apply
  * @Returns {void} 
*/
function setJustification(textRange, dstJustification) {
  var appliedParagraphStyle = textRange.paragraphStyles[0] ;
  var targetPropName = 'justification' ;
  var styleJustification = appliedParagraphStyle[targetPropName] ;

  var swapTable = {
    'Justification.LEFT': Justification.FULLJUSTIFYLASTLINELEFT, 
    'Justification.CENTER': Justification.FULLJUSTIFYLASTLINECENTER, 
    'Justification.RIGHT': Justification.FULLJUSTIFYLASTLINERIGHT, 
    'Justification.FULLJUSTIFYLASTLINELEFT': Justification.LEFT, 
    'Justification.FULLJUSTIFYLASTLINECENTER': Justification.CENTER, 
    'Justification.FULLJUSTIFYLASTLINERIGHT': Justification.RIGHT, 
    'Justification.FULLJUSTIFY': Justification.FULLJUSTIFYLASTLINELEFT
  } ;

  if(styleJustification === dstJustification) {
    appliedParagraphStyle[targetPropName] = swapTable[dstJustification.toString()] ;
    textRange[targetPropName] = dstJustification ;
    appliedParagraphStyle[targetPropName] = styleJustification ;
  } else {
    textRange[targetPropName] = dstJustification ;
  }
}
m1b
Braniac
May 23, 2025

Haha, thanks @sttk3 I just wrote a very similar function that I came back to post here! Great minds thing alike? 🙂

 

P.S. I had forgotten we had discussed this already in the underline post! Yes same issue as you say.

Inspiring
February 8, 2013

I have experimented a bit this this and found out that once you resize the frame, it's possible to change justification from right to left. So right after creating the frame

myTextFrame.resize(100.01,100.01)

should help

pixxxelschubser
Braniac
February 8, 2013

Give redraw a chance:

          // Check current document for textFrames.

          if ( app.documents.length < 1 ) {

                    alert ( "open a document with text objects and select them." );

          }

          else {

                    docRef = app.activeDocument;

                    if ( docRef.textFrames.length < 1 ) {

                              alert ( "Select some text objects." );

                    }

                    //where text fames are selected swap the jusification

                    else {

                              sel = docRef.selection;

                              var sellen = sel.length;

                              for (var i=0;i<sellen ;i++)

                              {

                                        if(sel.typename == "TextFrame"){

                                                  if(sel.story.textRange.justification == Justification.LEFT){

                                                            sel.story.textRange.justification = Justification.RIGHT;

                                                  }

                                                  else if(sel.story.textRange.justification == Justification.RIGHT){

                                                            sel.story.textRange.justification = Justification.LEFT;

                                                  }

                                                  else if(sel.story.textRange.justification == Justification.FULLJUSTIFYLASTLINELEFT){

                                                            sel.story.textRange.justification = Justification.FULLJUSTIFYLASTLINERIGHT;

                                                  }

                                                  else if(sel.story.textRange.justification == Justification.FULLJUSTIFYLASTLINERIGHT){

                                                            sel.story.textRange.justification = Justification.FULLJUSTIFYLASTLINELEFT;

                                                  }

                                        }

                              }

                    }

          }

          redraw()

CarlosCanto
Braniac
February 8, 2013

this did not work on my system Win 7, CS5

myTextFrame.resize(100.01,100.01);

myTextFrame.story.textRange.justification = Justification.LEFT;

this did not work either

sel.story.textRange.justification = Justification.LEFT;

redraw()

John Hawkinson
Inspiring
May 24, 2012

Well, have you tried printing out sel.story.textRange.justification before the if to make sure that it's the way you think it is? Use $.writeln().

Does it work if you just run one line by hand and set the justification?

May 24, 2012

Hi john,

thanks for responding.

Yes, I have tried printing out the justification to double check the settings.

I also just tried running the one line by hand. Works for switching a left justified text to Justification.RIGHT but not for switching a right justified text to Justification.LEFT

app.activeDocument.selection[0].story.textRange.justification = Justification.RIGHT;

Works on known left or center justified text

app.activeDocument.selection[0].story.textRange.justification = Justification.LEFT;

Fails on known right or center justified text

Possible bug in the implementation?

Inspiring
May 24, 2012

I get the same results as you?