Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Trouble assigning TextFrame to Justification.Left

Guest
May 24, 2012 May 24, 2012

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

}

TOPICS
Scripting
5.1K
Translate
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

Community Expert , May 22, 2025 May 22, 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. 
  * wor
...
Translate
Adobe
LEGEND ,
May 24, 2012 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?

Translate
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
Guest
May 24, 2012 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?

Translate
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
Guru ,
May 24, 2012 May 24, 2012

I get the same results as you?

Translate
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 Expert ,
May 24, 2012 May 24, 2012

Me too.

Translate
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
Contributor ,
Feb 08, 2013 Feb 08, 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

Translate
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 Expert ,
Feb 08, 2013 Feb 08, 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()

Translate
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 Expert ,
Feb 08, 2013 Feb 08, 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()

Translate
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 Expert ,
Feb 08, 2013 Feb 08, 2013

Too bad, because on Windows with CS3 it works

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

redraw()

Translate
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
Contributor ,
Feb 08, 2013 Feb 08, 2013

I'm using

myTextFrame.resize(100.1,100.1);

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

100.01 turns out to be not enough! sorry for this

redraw(); doesn't work for me neither (CS5, OS X 10.8.2)

and I don't see justification property on textRange(?) - but

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

works as well. it seems that only resizing by X axis matters actually. indeed, frame can be resized back afterwards, it just takes to 'touch' the frame.

Translate
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 Expert ,
Feb 08, 2013 Feb 08, 2013

great finding Evgeny!!! it works with

myTextFrame.resize(100.1,100.1);

pixxxel, I'll give it a try with CS4...it seems they broke it accidentally in that version then.

Translate
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 Expert ,
Feb 08, 2013 Feb 08, 2013

@CarlosCanto,

yes, I tried it now with CS5.1 on Win 7 and it doesn't work.

But I found the reason for that:

Your paragraphstyle is the normal paragraph style, right?

And you can change to all justifications, but not the LEFT one?

Make a new paragraph style with justification.FULLJUSTIFYLASTLINECENTER and apply it.

Now your script can change to all justifications, but not back to the FULLJUSTIFYLASTLINECENTER !

IMHO you can change to all, but in CS5 you can't go back to the justification, which is defined in the used paragraph style.

Is this so?

The solution should be: Use multiple paragraph styles in script

(or Adobe wishform  --> bug or feature???)

Translate
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 Expert ,
Feb 08, 2013 Feb 08, 2013

interesting...then based on what you're saying, the safest thing to do is resize all text frames, align/justify, then resize back. I can live with that, I made an Align palette script a couple of weeks ago, I'll revise it to include these findings.

Translate
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
Contributor ,
Feb 08, 2013 Feb 08, 2013

I think it can be done like this:

1) try to change justification

2) check if it's changed

3) if not, resize 100.1, then back to original, then apply justification again

this is enough to do only once since the frame creation, next time justification will change fine

Translate
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
Explorer ,
May 04, 2019 May 04, 2019

I recently used your trick tf.resize(80, 80); tf.resize(125, 125); to solve this problem that I too encountered long ago when trying to set justification to left so I thank you for that, however I also discovered yesterday that this bug/“feature” affects not only the justification attribute but also the spaceAfter when you want to set it back to zero, and maybe others attributes that I haven’t noticed yet.

Translate
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 Expert ,
Feb 08, 2013 Feb 08, 2013

in our eyes, it is a bug...Adobe will come up saying it is a feature.

it seems to be easier to resize the text frames than to create paragraph styles and check each item for which one it has...

Translate
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 Expert ,
May 22, 2025 May 22, 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!

Translate
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 Expert ,
May 22, 2025 May 22, 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
  * @Version 1.0.0
  * @author 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
  * @Param {TextRange} textRange - target TextRange
  * @Param {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 ;
  }
}
Translate
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 Expert ,
May 22, 2025 May 22, 2025
LATEST

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.

Translate
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