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

Increment numbers (preceded by a letter) by 1 in a given range

Explorer ,
Sep 03, 2023 Sep 03, 2023

Say I have this sequence of letters and numbers:

A200, B45, C38, D400, B800, C44, B304, D2000, B1101, C22, A1234, D420, B2022, C200, B995, A1020.

B4200, C34, B1024, D40.

D200, B11, B975.

I would need to increment by 1 any numbers preceded by the letter B in the --say-- 800-1200 range, as in (in bold type the numbers that were changed):

A200, B45, C38, D400, B801, C44, B304, D2000, B1102, C22, A1234, D420, B2022, C200, B996, A1020.

B4200, C34, B1025, D40.

D200, B11, B976.

Numbers are always preceded by a capital letter. Numbers are always followed by a comma, except for the last number, which is followed by a period. Is this doable?

 

This query is somewhat similar to the following thread:

https://community.adobe.com/t5/indesign-discussions/increment-long-sequence-of-numbers-by-1/m-p/1398...

 

TOPICS
Feature request , Scripting
514
Translate
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 2 Correct answers

Community Expert , Sep 03, 2023 Sep 03, 2023
var doc = app.activeDocument;
app.findGrepPreferences = null;
app.findGrepPreferences.findWhat = "(?<=\\b\\u)\\d+(?=,|\\.)";
var fs = doc.findGrep();
var i = fs.length;
var range = [800,1200];
var num;
while(i--) {
   num = Number(fs[i].contents;
   if (num >= range[0] && num <= range[1]) {
        fs[i].contents = num + 1 + "";
}
}
  

Sure doable. Something like the above. Untested (on mobile)

Translate
Community Expert , Sep 03, 2023 Sep 03, 2023

Then change \\u to A (or B or C, etc) in the findWhat, then change the range as appropriate 

Translate
Community Expert ,
Sep 03, 2023 Sep 03, 2023
var doc = app.activeDocument;
app.findGrepPreferences = null;
app.findGrepPreferences.findWhat = "(?<=\\b\\u)\\d+(?=,|\\.)";
var fs = doc.findGrep();
var i = fs.length;
var range = [800,1200];
var num;
while(i--) {
   num = Number(fs[i].contents;
   if (num >= range[0] && num <= range[1]) {
        fs[i].contents = num + 1 + "";
}
}
  

Sure doable. Something like the above. Untested (on mobile)

Translate
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 03, 2023 Sep 03, 2023

Thanks, but I get an error: 

Offending text: ;

It seems to be the semicolon in 

   num = Number(fs[i].contents;

 

Translate
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 ,
Sep 03, 2023 Sep 03, 2023

Missed parentheses after contents);

Translate
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 03, 2023 Sep 03, 2023

Seems to work, thanks a lot!

 

Would it be possible to apply the script to a text selection, not the full document? 

 

I see can change the number range easily, but I must be dumb because I can't see where to change the letter preceding the number. In the example above, it was B, but I'd need to change some number ranges preceded A or C or D...

 

Translate
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 ,
Sep 03, 2023 Sep 03, 2023

Should work for any preceding capital letter. To do to a text selection, think you should be able to change doc.findGrep() to app.selection[0).findGrep(). But might throw an error if text selection is funky

Translate
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 03, 2023 Sep 03, 2023

I'd need to be able to do this by a specific capital letter. That is, I'd need to change the 800-1200 range for B numbers, but I'd need to change the 500-900 range for A numbers. 

Translate
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 ,
Sep 03, 2023 Sep 03, 2023

Then change \\u to A (or B or C, etc) in the findWhat, then change the range as appropriate 

Translate
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 04, 2023 Sep 04, 2023
LATEST

Great, works fine. Much appreciated!

 

Translate
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