• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers

Replace each find match instance with an increasing value

Community Beginner ,
Sep 26, 2022 Sep 26, 2022

Copy link to clipboard

Copied

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.

TOPICS
How to , Scripting

Views

159

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

Advisor , Sep 26, 2022 Sep 26, 2022

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

Likes

Translate

Translate
Advisor ,
Sep 26, 2022 Sep 26, 2022

Copy link to clipboard

Copied

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

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
Community Beginner ,
Sep 26, 2022 Sep 26, 2022

Copy link to clipboard

Copied

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.

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
Advisor ,
Sep 26, 2022 Sep 26, 2022

Copy link to clipboard

Copied

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

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
Community Beginner ,
Sep 26, 2022 Sep 26, 2022

Copy link to clipboard

Copied

Thanks so much for taking the time!

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
Advisor ,
Sep 26, 2022 Sep 26, 2022

Copy link to clipboard

Copied

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

 

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
Community Beginner ,
Sep 26, 2022 Sep 26, 2022

Copy link to clipboard

Copied

Even better, that saves me the 2nd step of replacing the integers with alphas.

 

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

 

Doubtful, it's pretty smooth!

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
Community Expert ,
Oct 01, 2022 Oct 01, 2022

Copy link to clipboard

Copied

LATEST

Hi Mike,  just for learning, a string in javascript can be accessed using the index of characters. So you can write

var alpha = 'abcdefghijklmnopqrstuvwxyz';
var e = alpha[4];

 but another way is to generate the character using the ASCII code:

var e = String.fromCharCode(97 + 4); // 'e'

- Mark 

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