Skip to main content
j.khakase
Inspiring
July 21, 2023
Answered

How to Clear Text formatting by script?

  • July 21, 2023
  • 2 replies
  • 849 views

Hello Friends,

Is there any script to clear text formatting?

I know though Reset panel we can do it, but I am expecting with minimum clicks

This topic has been closed for replies.
Correct answer sttk3

Could it be the influence of paragraph styles? I also changed paragraph style settings to revert to default.

/**
  * @File make selected text default attributes
  * @version 1.1.0
  * @7111211 sttk3.com
  * @7340357 © 2023 sttk3.com
*/

(function() {
  if(app.documents.length <= 0) {return ;}
  var doc = app.documents[0] ;
  
  // get selected text
  var targetRanges = getTextSelection() ;
  var targetRangeLength = targetRanges.length ;
  if(targetRangeLength <= 0) {return ;}

  // get [Normal Character Style], [Normal Paragraph Style]
  var defaultCharacterStyle = doc.characterStyles[0] ;
  var defaultParagraphStyle = doc.paragraphStyles[0] ;

  // reset text style
  for(var i = 0 ; i < targetRangeLength ; i++) {
    var currentRange = targetRanges[i] ;
    defaultCharacterStyle.applyTo(currentRange, true) ;
    defaultParagraphStyle.applyTo(currentRange, true) ;
  }
})() ;

/**
  * returns selection as an Array of TextRange
  * @Returns {Array<TextRange>}
*/
function getTextSelection() {
  var sel = app.selection ;
  var selLength = sel.length ;
  var res = [] ;
  
  switch(sel.constructor.name) {
    case 'Array' :
      if(!sel[0]) {return res ;}
      for(var i = 0 ; i < selLength ; i++) {
        var itemType = sel[i].constructor.name ;
        if(itemType == 'TextFrame') {
          res.push(sel[i].textRange) ;
        } else if(itemType == 'TextRange') {
          res.push(sel[i]) ;
        }
      }
      break ;
    case 'TextRange' :
      res.push(sel) ;
      break ;
  }
  return res ;
}

 

2 replies

Legend
July 21, 2023

It can make selected text a default attribute. Do you mean that?

/**
  * @File make selected text default attributes
  * @version 1.0.0
  * @7111211 sttk3.com
  * @7340357 © 2023 sttk3.com
*/

(function() {
  if(app.documents.length <= 0) {return ;}
  var doc = app.documents[0] ;
  
  // get selected text
  var targetRanges = getTextSelection() ;
  var targetRangeLength = targetRanges.length ;
  if(targetRangeLength <= 0) {return ;}

  // get [Normal Character Style]
  var defaultCharacterStyle = doc.characterStyles[0] ;

  // reset text style
  for(var i = 0 ; i < targetRangeLength ; i++) {
    defaultCharacterStyle.applyTo(targetRanges[i], true) ;
  }
})() ;

/**
  * returns selection as an Array of TextRange
  * @Returns {Array<TextRange>}
*/
function getTextSelection() {
  var sel = app.selection ;
  var selLength = sel.length ;
  var res = [] ;
  
  switch(sel.constructor.name) {
    case 'Array' :
      if(!sel[0]) {return res ;}
      for(var i = 0 ; i < selLength ; i++) {
        var itemType = sel[i].constructor.name ;
        if(itemType == 'TextFrame') {
          res.push(sel[i].textRange) ;
        } else if(itemType == 'TextRange') {
          res.push(sel[i]) ;
        }
      }
      break ;
    case 'TextRange' :
      res.push(sel) ;
      break ;
  }
  return res ;
}
j.khakase
j.khakaseAuthor
Inspiring
July 21, 2023

Thank you so much, you are almost close to my expectectation. and the script works well while no predefined text styles I mean Character and Paragraph style. but when I run this script on the text where applied predefined text styles. the result come different. it doesn't reset font and color as normal below is the screenshot of it

 

 

sttk3Correct answer
Legend
July 21, 2023

Could it be the influence of paragraph styles? I also changed paragraph style settings to revert to default.

/**
  * @File make selected text default attributes
  * @version 1.1.0
  * @7111211 sttk3.com
  * @7340357 © 2023 sttk3.com
*/

(function() {
  if(app.documents.length <= 0) {return ;}
  var doc = app.documents[0] ;
  
  // get selected text
  var targetRanges = getTextSelection() ;
  var targetRangeLength = targetRanges.length ;
  if(targetRangeLength <= 0) {return ;}

  // get [Normal Character Style], [Normal Paragraph Style]
  var defaultCharacterStyle = doc.characterStyles[0] ;
  var defaultParagraphStyle = doc.paragraphStyles[0] ;

  // reset text style
  for(var i = 0 ; i < targetRangeLength ; i++) {
    var currentRange = targetRanges[i] ;
    defaultCharacterStyle.applyTo(currentRange, true) ;
    defaultParagraphStyle.applyTo(currentRange, true) ;
  }
})() ;

/**
  * returns selection as an Array of TextRange
  * @Returns {Array<TextRange>}
*/
function getTextSelection() {
  var sel = app.selection ;
  var selLength = sel.length ;
  var res = [] ;
  
  switch(sel.constructor.name) {
    case 'Array' :
      if(!sel[0]) {return res ;}
      for(var i = 0 ; i < selLength ; i++) {
        var itemType = sel[i].constructor.name ;
        if(itemType == 'TextFrame') {
          res.push(sel[i].textRange) ;
        } else if(itemType == 'TextRange') {
          res.push(sel[i]) ;
        }
      }
      break ;
    case 'TextRange' :
      res.push(sel) ;
      break ;
  }
  return res ;
}

 

pixxxelschubser
Community Expert
Community Expert
July 21, 2023

Not enough information.

 

But: try to apply the normal paragraph style to all paragraphs.