Skip to main content
Known Participant
October 3, 2024
Answered

Removing underline not working in Illustrator using Extendscript

  • October 3, 2024
  • 2 replies
  • 998 views

Hi Folks

The below example not woking in illustror. Initially I applied underline = true for the textrange working fine but while removing its not working. Please help on this.

var doc = app.activeDocument;
for (var i = 0; i < doc.textFrames.length; i++) {
var txtFrame = doc.textFrames[i];
var textRange = txtFrame.story.textRange;
textRange.start = 0;
textRange.length = 5;
textRange.characterAttributes.underline = false;
}
Thanks in advance
This topic has been closed for replies.
Correct answer sttk3
As for CharacterAttributes.underline, the result is caused by Illustrator's specification that it is ignored if we set the same value as the character style being applied.

The following script has a workaround function setUnderline to avoid this. Instead of just setting the underline property, try specifying it via setUnderline.
(function() {
  var doc = app.activeDocument ;
  for(var i = 0; i < doc.textFrames.length; i++) {
    var txtFrame = doc.textFrames[i] ;
    var textRange = txtFrame.story.textRange ;
    textRange.start = 0 ;
    textRange.length = 5 ;
    
    setUnderline(textRange, false) ;
  }
})() ;

function setUnderline(textRange, dstUnderline) {
  var appliedCharacterStyle = textRange.characterStyles[0] ;
  var styleUnderline = appliedCharacterStyle.underline ;

  if(styleUnderline === dstUnderline) {
    appliedCharacterStyle.underline = !styleUnderline ;
    textRange.underline = dstUnderline ;
    appliedCharacterStyle.underline = styleUnderline ;
  } else {
    textRange.underline = dstUnderline ;
  }
}

Similar post:

2 replies

sttk3Correct answer
Legend
October 3, 2024
As for CharacterAttributes.underline, the result is caused by Illustrator's specification that it is ignored if we set the same value as the character style being applied.

The following script has a workaround function setUnderline to avoid this. Instead of just setting the underline property, try specifying it via setUnderline.
(function() {
  var doc = app.activeDocument ;
  for(var i = 0; i < doc.textFrames.length; i++) {
    var txtFrame = doc.textFrames[i] ;
    var textRange = txtFrame.story.textRange ;
    textRange.start = 0 ;
    textRange.length = 5 ;
    
    setUnderline(textRange, false) ;
  }
})() ;

function setUnderline(textRange, dstUnderline) {
  var appliedCharacterStyle = textRange.characterStyles[0] ;
  var styleUnderline = appliedCharacterStyle.underline ;

  if(styleUnderline === dstUnderline) {
    appliedCharacterStyle.underline = !styleUnderline ;
    textRange.underline = dstUnderline ;
    appliedCharacterStyle.underline = styleUnderline ;
  } else {
    textRange.underline = dstUnderline ;
  }
}

Similar post:
m1b
Community Expert
Community Expert
October 3, 2024

Thanks @sttk3 that workaround is nice! I tried it with my test script and it worked. (I still consider this behaviour as a bug, by the way.)

- Mark

Legend
October 3, 2024

It was possible to specify this correctly until Illustrator CS3. I would consider it a bug too.

m1b
Community Expert
Community Expert
October 3, 2024

Hi @Jothi Sankar Anand S I think you have found a bug. I have posted a bug report. Please vote on it.

 

Here is my test script:

/**
 * @author m1b
 * @discussion https://community.adobe.com/t5/illustrator-discussions/removing-underline-not-working-in-illustrator-using-extendscript/m-p/14895226
 */
(function () {

    var doc = app.activeDocument,
        tf = doc.activeLayer.textFrames.add(),
        tr = tf.textRanges;

    tf.contents = 'ab';

    // change an attribute on the first character
    tf.textRange.characters[0].characterAttributes.baselineShift = 1;

    var results = ['Results:'];

    // test 1 on first character
    tr[0].characterAttributes.underline = true
    results.push('tr[0] underline = TRUE: '
        + (true === tr[0].characterAttributes.underline ? 'PASSED' : 'FAILED'));

    // test 2 on first character
    tr[0].characterAttributes.underline = false;
    results.push('tr[0] underline = FALSE: '
        + (false === tr[0].characterAttributes.underline ? 'PASSED' : 'FAILED'));

    // test 1 on second character
    tr[1].characterAttributes.underline = true;
    results.push('tr[1] underline = TRUE: '
        + (true === tr[1].characterAttributes.underline ? 'PASSED' : 'FAILED'));

    // test 2 on second character
    tr[1].characterAttributes.underline = false;
    results.push('tr[1] underline = FALSE: '
        + (false === tr[1].characterAttributes.underline ? 'PASSED' : 'FAILED'));

    app.redraw();
    alert(results.join('\n'));

})();

 

And my results are: