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:
Apply Character direction to textframe characters via Extendscript
Removing underline not working in Illustrator using Extendscript
/**
* @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 ;
}
}
... View more