Skip to main content
Participant
May 10, 2021
Answered

Creating a script to search and change character-style for individual characters

  • May 10, 2021
  • 1 reply
  • 1095 views

Hi there,

I am trying to build a script that uses GREP to search the current open document, and changes all instances of 'm2' to 'm2' (superscript)
Due to the documents build, I can't easily just put it in the GREP styles in paragraph styles as most of the document text frames read as (No styles)+ due to a plugin we use.
I have used this code for creating a character style that has only got superscript applied, I just can't figure out how to apply it to all instances of 'm2'

var myDocument = app.activeDocument;
var myCharacterStyle = myDocument.characterStyles.item ("superscript" );
      !myCharacterStyle.isValid && myCharacterStyle = myDocument.characterStyles.add({name:"superscript"});
      myCharacterStyle.position = Position.SUPERSCRIPT

I have been trying to use the GREP: (?<=\l)2 as the search terms.
Is there a way to make GREP target the characters I want, and apply a character style directly to that character?

This topic has been closed for replies.
Correct answer Manan Joshi

Try the following

app.findGrepPreferences=app.changeGrepPreferences=null;
app.findGrepPreferences.findWhat="m\\K2";

//Change the name to your superscript style name
app.changeGrepPreferences.appliedCharacterStyle = app.documents[0].characterStyles.itemByName("Sp"); 

app.activeDocument.changeGrep();
app.findGrepPreferences=app.changeGrepPreferences=null;

-Manan

1 reply

Community Expert
May 10, 2021

Try the following, in FindGrep dialog

Find What:

m\K2

Change format:

Apply your character style Superscript

-Manan

-Manan
Alec vdvAuthor
Participant
May 10, 2021

Thank you, I wasn't aware of the change format section in that FindGrep dialog!  

 

 

Is there a way to get a script to auto fill the 'Find What' field, and change format to superscript, and then apply? This formatting usually needs to be applied across multiple documents, and i'd love to be able to add extra characters for it to consider (like Trademark or Registered symbols).