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

Replace each find match instance with an increasing value

Explorer ,
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

352

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

Votes

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

Votes

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
Explorer ,
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.

Votes

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

Votes

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

Copy link to clipboard

Copied

Thanks so much for taking the time!

Votes

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

 

Votes

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
Explorer ,
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!

Votes

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

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 

Votes

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
Explorer ,
Dec 16, 2022 Dec 16, 2022

Copy link to clipboard

Copied

Hi @m1b Mark and @Mike Bro Mike,

I've been using the above script on a project successfully for the past few months, but today I've run into an issue. I'm also using the native "FindChangeByList" script from time to time. The problem is that after I run FindChangeByList, the above script no longer works. It doesn't do anything. Other scripts still work, it's just this one that seems to be disabled when I run FindChangeByList first, apparently. However, if I fully quit InDesign and reopen the program, it works again. Very odd. Is there anything we can add to the above script to make it more stable?

Thanks in advance.

Votes

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
Explorer ,
Dec 16, 2022 Dec 16, 2022

Copy link to clipboard

Copied

Well, I figured it out, if you're interested. It seems the other script was changing the text find preference to:

wholeWord:true

 I just changed the above script to Grep as a workaround, but I thought that this line would have cleared the find preferences before performing the search:

app.findTextPreferences = app.changeTextPreferences = NothingEnum.NOTHING;

 That's obviously not the case; is there something else to add that would clear the "wholeWord:true" preference first?

Votes

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 ,
Dec 16, 2022 Dec 16, 2022

Copy link to clipboard

Copied

Yeah, that preferences is in a different place: FindChangeTextOption. So you could use

app.findChangeTextOptions.wholeWord = false;

- Mark

Votes

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
Explorer ,
Dec 18, 2022 Dec 18, 2022

Copy link to clipboard

Copied

LATEST

Cool, thanks!

Votes

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