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

New Here ,
May 09, 2021 May 09, 2021

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

Screen Shot 2021-05-10 at 10.24.15 am.pngI 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?

TOPICS
Scripting, Type

Views

375

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct Answer

Adobe Community Professional , May 09, 2021 May 09, 2021
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

Likes

Translate

Translate
Adobe Community Professional ,
May 09, 2021 May 09, 2021

Copy link to clipboard

Copied

Try the following, in FindGrep dialog

Find What:

m\K2

Change format:

Apply your character style Superscript

-Manan

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
May 09, 2021 May 09, 2021

Copy link to clipboard

Copied

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

 

Screen Shot 2021-05-10 at 2.52.50 pm copy.png

 

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).

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
May 09, 2021 May 09, 2021

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;

 

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
May 09, 2021 May 09, 2021

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

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
May 09, 2021 May 09, 2021

Copy link to clipboard

Copied

LATEST

Thank you so much Manan! Works perfectly.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines