Copy link to clipboard
Copied
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?
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
Copy link to clipboard
Copied
Try the following, in FindGrep dialog
Find What:
m\K2
Change format:
Apply your character style Superscript
-Manan
Copy link to clipboard
Copied
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).
Copy link to clipboard
Copied
I found an example GREP script that does a simple Find/Replace, however i'm not sure how to get it to target the 'Change format' secion of the dialog. I am unsure of the correct syntax for it!
//reset GREP preferences
app.findGrepPreferences=app.changeGrepPreferences=null;
//app.findChangeGrepOptions = app.pageItem.selection
app.findGrepPreferences.findWhat="m\\K2";
//This is where I need to change preferences to make it superscript
app.changeGrepPreferences.changeTo="SUPERSCRIPT HERE";
app.activeDocument.changeGrep();
app.findGrepPreferences=app.changeGrepPreferences=null;
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
Thank you so much Manan! Works perfectly.