Skip to main content
Known Participant
February 25, 2024
Answered

Converting Nested Styles into Local Character Styles

  • February 25, 2024
  • 4 replies
  • 1164 views

Hello, I am having some problems with paragraph styles (when I export to html or epub) where I use grep style.
To convert grep styles to local character styles

http://in-tools.com/products/scripts/

I use (Apply Nested Style). But here, my extra underlined words on the paragraph are not visible.
Does anyone know another solution? Thanks in advance for your help.

 

Correct answer Peter Kahrel

In that case you use this one:

 

(function () {
  
  var indd = app.documents.item(0);

  function applyAsLiteral (gstyle) {
    app.findGrepPreferences.findWhat = gstyle.grepExpression;
    app.changeGrepPreferences.appliedCharacterStyle = gstyle.appliedCharacterStyle;
    indd.changeGrep();
  }

  function grepStyleToNormal (ps) {
    var gstyles = ps.nestedGrepStyles.everyItem().getElements();
    for (var i = 0; i < gstyles.length; i++) {
      if (gstyles[i].name !== 'myChar') {
        applyAsLiteral (gstyles[i]);
      }
    }
    ps.nestedGrepStyles.everyItem().remove();
  }

  app.findGrepPreferences = app.changeGrepPreferences = null;
  var pstyles = indd.allParagraphStyles;
  for (var i = pstyles.length-1; i >= 1; i--) {
    grepStyleToNormal (pstyles[i]);
  }

}());

4 replies

Peter Kahrel
Community Expert
Community Expert
May 20, 2025

The script posted here has two flaws (one fatal): It doesn't deal with overlapping character styles and it doesn't handle based-on styles.

 

I upgraded the script and posted it here:

https://creativepro.com/files/kahrel/indesign/grep-styles-to-local-styles.html

 

Peter Kahrel
Community Expert
Peter KahrelCommunity ExpertCorrect answer
Community Expert
March 1, 2024

In that case you use this one:

 

(function () {
  
  var indd = app.documents.item(0);

  function applyAsLiteral (gstyle) {
    app.findGrepPreferences.findWhat = gstyle.grepExpression;
    app.changeGrepPreferences.appliedCharacterStyle = gstyle.appliedCharacterStyle;
    indd.changeGrep();
  }

  function grepStyleToNormal (ps) {
    var gstyles = ps.nestedGrepStyles.everyItem().getElements();
    for (var i = 0; i < gstyles.length; i++) {
      if (gstyles[i].name !== 'myChar') {
        applyAsLiteral (gstyles[i]);
      }
    }
    ps.nestedGrepStyles.everyItem().remove();
  }

  app.findGrepPreferences = app.changeGrepPreferences = null;
  var pstyles = indd.allParagraphStyles;
  for (var i = pstyles.length-1; i >= 1; i--) {
    grepStyleToNormal (pstyles[i]);
  }

}());
uniq1Author
Known Participant
March 1, 2024

Hi @Peter Kahrel;

It works perfectly, thank you very much, I can do more specific work on this script. 

Peter Kahrel
Community Expert
Community Expert
February 29, 2024

The In-Tools script doesn't deal with GREP styles, that's why it doesn't work for you.

 

Here's a script that converts GREP styles to character styles applied in the document. Be aware though that any character style applied directly will be overridden, and also that GREP styles are additive, i.e. they don't cancel each other out, whereas directly applied character style do cancel each other out: the last character style applied wins.

 

 

(function () {
  
  var indd = app.documents.item(0);

  function applyAsLiteral (gstyle) {
    app.findGrepPreferences.findWhat = gstyle.grepExpression;
    app.changeGrepPreferences.appliedCharacterStyle = gstyle.appliedCharacterStyle;
    indd.changeGrep();
  }

  function grepStyleToNormal (ps) {
    var gstyles = ps.nestedGrepStyles.everyItem().getElements();
    for (var i = 0; i < gstyles.length; i++) {
      applyAsLiteral (gstyles[i]);
    }
    ps.nestedGrepStyles.everyItem().remove();
  }

  app.findGrepPreferences = app.changeGrepPreferences = null;
  var pstyles = indd.allParagraphStyles;
  for (var i = pstyles.length-1; i >= 1; i--) {
    grepStyleToNormal (pstyles[i]);
  }

}());

 

 

uniq1Author
Known Participant
March 1, 2024

Worked great Peter, thank you very much.

Except for one problem you mentioned. Subsequently applied character styles.
There is a character style called “myChar” which I then applied. How can I proceed if I don’t want this to change? Thank again...

danaken3
Participating Frequently
February 28, 2024

I'm not sure I understand your question. Can you provide some more detail about what your desired vs. actual outcome is, and a screenshot?

uniq1Author
Known Participant
March 1, 2024

Thank you for your interest. I guess I didn't explain it clearly. But @Peter Kahrel saw the problem and helped me find the right answer. Thanks a lot.