Skip to main content
Participating Frequently
March 8, 2023
Answered

script

  • March 8, 2023
  • 3 replies
  • 1515 views

Hi I need a Script to add a grep style to every existing paragraph style in the document.

The character style that will be used to add the grep to is allready in the document and is called "MAGENTA". The grep needed is "\?{2,}|\bX{2,}\b"

 

is this even possible to script?

 

thanks in advance

 

 

This topic has been closed for replies.
Correct answer nicosh
doc = app.activeDocument;
parStyles = doc.paragraphStyles;
charStyles = doc.characterStyles;

props = {
appliedCharacterStyle: charStyles.itemByName('MAGENTA'),
grepExpression: "\\?{2,}|\\bX{2,}\\b"
}


for (i = 0; i < parStyles.length; i++) {
try {
parStyles[i].nestedGrepStyles.add(props);
} catch (error) { }
}
 
 
This option adds grep style to basic paragrph style, you can delete it.

3 replies

Inspiring
March 8, 2023

'Magenta' in uppercase: 'MAGENTA'

doc = app.activeDocument;
parStyles = doc.paragraphStyles;
charStyles = doc.characterStyles;

props = {
  appliedCharacterStyle: charStyles.itemByName('MAGENTA'),
  grepExpression: "\\?{2,}|\\bX{2,}\\b"
}


for (i = 0; i < parStyles.length; i++) {
  // Avoid add grep to basic paragraph style & none paragraph style
  // Substitute for english names
  if (parStyles[i].name != '[Ningún estilo de párrafo]' && parStyles[i].name != '[Párrafo básico]') {
    parStyles[i].nestedGrepStyles.add(props);
  }
}
Participating Frequently
March 8, 2023

Hi Nicosh,

 

This doesnt seem to be working. 

 

Error number : 516

parStyles[i].nestedGrepStyles.add(props);

Community Expert
March 8, 2023

Works fine for me, the code by @nicosh would not add the grep to styles within groups, for that you can use the following variation of the code

var doc = app.activeDocument;
var parStyles = doc.allParagraphStyles;
var charStyles = doc.characterStyles;
var props = {
  appliedCharacterStyle: charStyles.itemByName('MAGENTA'),
  grepExpression: "\\?{2,}|\\bX{2,}\\b"
}
for (i = 2; i < parStyles.length; i++)
    parStyles[i].nestedGrepStyles.add(props);

If it still does not work for you, kindly share a sample document for us to test it out

-Manan

 

-Manan
Inspiring
March 8, 2023

Try this

 

doc = app.activeDocument;
parStyles = doc.paragraphStyles;
charStyles = doc.characterStyles;

props = {
  appliedCharacterStyle: charStyles.itemByName('Magenta'),
  grepExpression: "\\?{2,}|\\bX{2,}\\b"
}


for (i = 0; i < parStyles.length; i++) {
  // Avoid add grep to basic paragraph style & none paragraph style
  // Substitute for english names
  if (parStyles[i].name != '[Ningún estilo de párrafo]' && parStyles[i].name != '[Párrafo básico]') {
    parStyles[i].nestedGrepStyles.add(props);
  }
}
Mike Witherell
Community Expert
Community Expert
March 8, 2023

This script that can send GREP expressions into multiple paragraph styles all at once:

InDesign: A simple GREP editor | Peter Kahrel

Mike Witherell
Participating Frequently
March 8, 2023

Hi Mike, Script looks promissing but i want it to add this certain grep to every paragrapgh style the moment i klik the script. It just needs to do it. i dont need to figure out the grep in advance. i need the part "send to grep style" from the script and do it automaticcally to every paragraph style. I dont need the popup

Mike Witherell
Community Expert
Community Expert
March 8, 2023

That is the only one I currently know of. I am more of a script watcher than a script writer.

Mike Witherell