Copy link to clipboard
Copied
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:
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)
Then change \\u to A (or B or C, etc) in the findWhat, then change the range as appropriate
Copy link to clipboard
Copied
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)
Copy link to clipboard
Copied
Thanks, but I get an error:
Offending text: ;
It seems to be the semicolon in
num = Number(fs[i].contents;
Copy link to clipboard
Copied
Missed parentheses after contents);
Copy link to clipboard
Copied
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...
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
Then change \\u to A (or B or C, etc) in the findWhat, then change the range as appropriate
Copy link to clipboard
Copied
Great, works fine. Much appreciated!