Skip to main content
Known Participant
September 26, 2022
Answered

Replace each find match instance with an increasing value

  • September 26, 2022
  • 1 reply
  • 1116 views

I have a text with a lot of reference notes. The original text simply has asterisks(*) to indicate the presence of a note, but I need to change these to serial letter values like footnote references (a, b, c, etc.)

 

Is there a way to select all the text on the page, find all asterisks, and replace them in a serial sequence? So the first instance gets replaced with a, the second with b, the third with c, and so on? If not with letters, I could use number values instead, and then do a second round of replacing 1 with a, 2 with b, 3 with c, etc.

 

I tried using the find/replace list script, but I can't figure out how to do this in sequence. Any suggestions would be appreciated!

 

Note: I'm not using the inbuilt footnotes feature for a variety of reasons, so that's not an option.

This topic has been closed for replies.
Correct answer Mike Bro

Thanks so much for taking the time!


You're welcome!

If you want to convert the "*" to a, b, c...try this script.

 

 

var alpha = ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'];

app.findTextPreferences = app.changeTextPreferences = NothingEnum.NOTHING;
app.findTextPreferences.findWhat = "*";

var finds = app.activeDocument.selection[0].findText();
if (finds.length > 0) {
  for (var i = 0; i < finds.length; i++){  
   finds[i].contents = String(alpha[i]);
  }  
}
app.findTextPreferences = app.changeTextPreferences = NothingEnum.NOTHING;

 

 

I'm sure someone here has a better way...

 

Regards,

Mike

 

1 reply

Legend
September 26, 2022

Hello @defaultu0e43kqloi6n,

 

Give the below script a try...

 

app.findTextPreferences = app.changeTextPreferences = NothingEnum.NOTHING;
app.findTextPreferences.findWhat = "*";

var finds = app.activeDocument.findText();
if (finds.length > 0) {
  for (var i = 0; i < finds.length; i++){  
   finds[i].contents = String(i+1);
  }  
}
app.findTextPreferences = app.changeTextPreferences = NothingEnum.NOTHING;

 

Regards,

Mike

Known Participant
September 26, 2022

Thanks, Mike! Works like a charm. Do you know what I would modify here to only search selected text? This script searches the entire document.

Legend
September 26, 2022

this will work on a selected text box...

app.findTextPreferences = app.changeTextPreferences = NothingEnum.NOTHING;
app.findTextPreferences.findWhat = "*";

var finds = app.activeDocument.selection[0].findText();
if (finds.length > 0) {
  for (var i = 0; i < finds.length; i++){  
   finds[i].contents = String(i+1);
  }  
}
app.findTextPreferences = app.changeTextPreferences = NothingEnum.NOTHING;

Regards,

Mike