Skip to main content
Known Participant
Jun 4, 2024 1:12 PM UTC
Answered

Apply Character direction to textframe characters via Extendscript

Hi Folks,

How to apply character direction to TextFrame characters like LEFT_TO_RIGHT_DIRECTION | RIGHT_TO_LEFT_DIRECTION using extendscript.

Thanks in advance 

This topic is closed to new replies. Start a new post to keep the conversation going.
sttk3Correct answer
Legend
Jun 20, 2024 9:49 AM UTC

There have been a few similar topics recently, so I installed the Arabic version of Illustrator to investigate these items.

 

The conclusion is as follows:

/**
  * @File An example of ExtendScript to set paragraph direction and character direction
  * @version 1.0.0
  * @7111211 sttk3.com
*/

(function() {
  var doc = app.documents[0] ;
  var sel = doc.selection[0] ;

  /* Paragraph Direction
  ParagraphDirectionType.RIGHT_TO_LEFT_DIRECTION
  ParagraphDirectionType.LEFT_TO_RIGHT_DIRECTION
  */
  setParagraphDirection(sel.textRange, ParagraphDirectionType.RIGHT_TO_LEFT_DIRECTION) ;

  /* Character Direction
  DirOverrideType.DEFAULT_DIRECTION
  DirOverrideType.RIGHT_TO_LEFT_DIRECTION
  DirOverrideType.LEFT_TO_RIGHT_DIRECTION
  */
  sel.textRange.dirOverride = DirOverrideType.RIGHT_TO_LEFT_DIRECTION ;
})() ;

/**
  * Set the paragraph direction. This will succeed even if the specified property is the same as [Normal Paragraph Style]
  * @9397041 {TextRange} range target text range
  * @9397041 {ParagraphDirectionType} dstDirection destination paragraph direction
*/
function setParagraphDirection(range, dstDirection) {
  var appliedParagraphStyle = range.paragraphStyles[0] ;
  var styleDirection = appliedParagraphStyle.paragraphDirection ;

  if(styleDirection === dstDirection) {
    var swapMap = {
      'ParagraphDirectionType.RIGHT_TO_LEFT_DIRECTION': ParagraphDirectionType.LEFT_TO_RIGHT_DIRECTION, 
      'ParagraphDirectionType.LEFT_TO_RIGHT_DIRECTION': ParagraphDirectionType.RIGHT_TO_LEFT_DIRECTION
    } ;
    appliedParagraphStyle.paragraphDirection = swapMap[dstDirection.toString()] ;
    range.paragraphDirection = dstDirection ;
    appliedParagraphStyle.paragraphDirection = styleDirection ;
  } else {
    range.paragraphDirection = dstDirection ;
  }
}

 

Check the AppleScript scripting dictionary and you will find the properties that can be controlled.

 

 

12 replies

sttk3Correct answer
Legend
Jun 20, 2024 9:49 AM UTC

There have been a few similar topics recently, so I installed the Arabic version of Illustrator to investigate these items.

 

The conclusion is as follows:

/**
  * @File An example of ExtendScript to set paragraph direction and character direction
  * @version 1.0.0
  * @7111211 sttk3.com
*/

(function() {
  var doc = app.documents[0] ;
  var sel = doc.selection[0] ;

  /* Paragraph Direction
  ParagraphDirectionType.RIGHT_TO_LEFT_DIRECTION
  ParagraphDirectionType.LEFT_TO_RIGHT_DIRECTION
  */
  setParagraphDirection(sel.textRange, ParagraphDirectionType.RIGHT_TO_LEFT_DIRECTION) ;

  /* Character Direction
  DirOverrideType.DEFAULT_DIRECTION
  DirOverrideType.RIGHT_TO_LEFT_DIRECTION
  DirOverrideType.LEFT_TO_RIGHT_DIRECTION
  */
  sel.textRange.dirOverride = DirOverrideType.RIGHT_TO_LEFT_DIRECTION ;
})() ;

/**
  * Set the paragraph direction. This will succeed even if the specified property is the same as [Normal Paragraph Style]
  * @9397041 {TextRange} range target text range
  * @9397041 {ParagraphDirectionType} dstDirection destination paragraph direction
*/
function setParagraphDirection(range, dstDirection) {
  var appliedParagraphStyle = range.paragraphStyles[0] ;
  var styleDirection = appliedParagraphStyle.paragraphDirection ;

  if(styleDirection === dstDirection) {
    var swapMap = {
      'ParagraphDirectionType.RIGHT_TO_LEFT_DIRECTION': ParagraphDirectionType.LEFT_TO_RIGHT_DIRECTION, 
      'ParagraphDirectionType.LEFT_TO_RIGHT_DIRECTION': ParagraphDirectionType.RIGHT_TO_LEFT_DIRECTION
    } ;
    appliedParagraphStyle.paragraphDirection = swapMap[dstDirection.toString()] ;
    range.paragraphDirection = dstDirection ;
    appliedParagraphStyle.paragraphDirection = styleDirection ;
  } else {
    range.paragraphDirection = dstDirection ;
  }
}

 

Check the AppleScript scripting dictionary and you will find the properties that can be controlled.

 

 

Translate
Known Participant
Jul 10, 2024 12:13 PM UTC

Thanks @sttk3 

Character Direction working properly. 

In Illustrator Arabic Version ParagraphDirectionType.LEFT_TO_RIGHT_DIRECTION working properly. But ParagraphDirectionType.RIGHT_TO_LEFT_DIRECTION - ParagraphDirectionType.DEFAULT_DIRECTION not wroking as expected. Please help on this.

Note: Other than Arabic version both ParagraphDirectionType working properly.

Thanks in advance

Translate
Legend
Sep 30, 2024 2:40 AM UTC

ParagraphDirectionType.DEFAULT_DIRECTION should not work because the property does not exist.

 

As for ParagraphDirectionType.RIGHT_TO_LEFT_DIRECTION, the result is caused by Illustrator's specification that it is ignored if we set the same property as the paragraph style being applied.

 

The above script has a workaround function setParagraphDirection to avoid this. Instead of just setting the paragraphDirection property, try specifying it via setParagraphDirection.

Translate
Sergey Osokin
Inspiring
Jun 6, 2024 3:28 AM UTC

I'm not sure if this is what you're looking for, but you can try it.

 

activeDocument.selection[0].textRange.paragraphDirection = ParagraphDirectionType.LEFT_TO_RIGHT_DIRECTION

 

and

 

activeDocument.selection[0].textRange.paragraphDirection = ParagraphDirectionType.RIGHT_TO_LEFT_DIRECTION

 

Translate
Known Participant
Jun 17, 2024 1:02 PM UTC

Hi @Sergey Osokin 
For paragraphDirection ParagraphDirectionType.LEFT_TO_RIGHT_DIRECTION its working fine. But ParagraphDirectionType.RIGHT_TO_LEFT_DIRECTION not working in illustratror. Please help on this.

Thanks in advance

Translate
Known Participant
Jun 20, 2024 4:07 AM UTC

I don't know much about character direction in Illustrator, and I don't have a choice of directions in the Character panel menu like in your screenshots.

 


Thanks for reply

@Sergey Osokin I'm using Arabic version of illustrator. Normal version of illustartor RIGHT_TO_LEFT_DIRECTION working fine. But in Arabic version of illustrator not working. 

 

Note: In Arabic Version of illustrator there is option for paragraph direction and character directions in paragraph and character menu.

Translate