Copy link to clipboard
Copied
Hi,
Newbie here!
I have a very long list of entries that I imported from Word and they were not automatically sequenced. That is, if I remove an entry, the numbering does NOT change.
Thing is, I do need to add an entry in the middle of this long list, and I was wondering if there's a way to automatically find all the entry numbers and increment them by 1.
The formatting is always the same:
paragraph break + entry number + period + text, as in:
1. text
2. text
3. text
4. text
5. text
If I add a new entry after entry 3, for instance, how do I change change entry 4 to entry 5, and entry 5 to entry 6.
I think I can find all the numbers using \r[\d.]+ but I have no idea how to replace those numbers incrementally.
Thanks!
1 Correct answer
Ugh. Add another backslash before \.:
'^\\d+(?=\\.)'
Copy link to clipboard
Copied
You could apply paragraph numbering to the paragraph style for the numbers, then delete all manual numbers:
Find: ^\d+\.\t
Replace with <Leave empty
to make it future-proof.
Copy link to clipboard
Copied
Thanks, but I really don't want to apply paragraph numberging for a number of reasons.
Is there any way to replace the manual numbers incrementally?
Copy link to clipboard
Copied
Sure. Click in any of the paragraphs that have a manual number, then run this script:
(function () {
app.findGrepPreferences = null;
app.findGrepPreferences.findWhat = '^\\d+';
app.findGrepPreferences.appliedParagraphStyle =
app.selection[0].appliedParagraphStyle;
var n = app.selection[0].parentStory.findGrep();
for (var i = n.length-1; i >= 0; i--) {
n[i].contents = String(i+1);
}
}());
This assumes that all the manual numbers are in the same paragraph style. If that's not the case, i.e. the numbers can be in any paragraph and all paragraphs that start with a number should be targeted, then use this one (now select a frame or an insertion point):
(function () {
app.findGrepPreferences = null;
app.findGrepPreferences.findWhat = '^\\d+';
var n = app.selection[0].parentStory.findGrep();
for (var i = n.length-1; i >= 0; i--) {
n[i].contents = String(i+1);
}
}());
Copy link to clipboard
Copied
Thanks, that seems to work fine. All manual numbers are in the same paragraph style.
Thing is, there are some 4000 entries in the document, but I only need to make this change in some 1200 entries.
Is there any way to apply this script to a given page range instead of the full document?
Copy link to clipboard
Copied
Peter,
the script works wonders, but resets numbering to 1, right? That is, if the sequence I want to replace --with a dupe entry-- is
23. text
24. text
24. text
25. text
when I apply the script I get
1. text
2. text
3. text
4. text
instead of
23. text
24. text
25. text
26. text
Is there any way for the script to select a given page range and keep the actual numbers instead of reseting it to 1?
Thanks!
Copy link to clipboard
Copied
Sure 🙂
Select the paragraph that should be renumbered and the paragraph before that range. Make sure that the entire first paragraph is selected. In your example, you select these paragraphs:
24. text
24. text
25. text
and the first one must be selected entirely. Of the last paragraph (25) only the number needs to be selected.
(function () {
app.findGrepPreferences = null;
app.findGrepPreferences.findWhat = '^\\d+';
var start = Number (app.selection[0].paragraphs[0].contents.match(/^\d+/)[0]);
var n = app.selection[0].findGrep();
for (var i = n.length-1; i >= 0; i--) {
n[i].contents = String(i+start);
}
}());
Copy link to clipboard
Copied
Works great indeed! Thanks a lot.
Thing is, there are a few --not many-- numbers in the main body of text that the script is picking and replacing as if they were entry numbers.
Would it be possible to add to the "findWhat" the period that follows the number in each entry? That would definitely skip those numbers in the main body of text.
Thanks!
Copy link to clipboard
Copied
Or, if it's any easier, add an extra paragraph break right before the number in the "findWhat" since the sequence is actually:
24. Text
24. Text
26. Text
That is, there are two paragraph breaks between entries.
Copy link to clipboard
Copied
You can add the period:
app.findGrepPreferences.findWhat = '^\\d+(?=\.)';
The caret (^) limits the search to the start of the paragraph, adding returns makes no difference. The period should do it.
P.
Copy link to clipboard
Copied
I added the period as above, but the script is picking some instances where the number is preceded by a paragraph break and followed by either a ( or a / but not a period. See below:
Copy link to clipboard
Copied
Ugh. Add another backslash before \.:
'^\\d+(?=\\.)'
Copy link to clipboard
Copied
Yay! Now it does work. Thanks a lot, Peter.
Just curious, what's the function of the double slash?
Copy link to clipboard
Copied
In the interface you'd use a single slash (\. stands for 'dot'), but when you use that in a string you need to escape the backslash. Without it, \. is entered as ., which is 'any character'. That's why it went wrong.
Copy link to clipboard
Copied
Hi, sorry for my english. I have a lot of text boxes that starts with a progressive number with a dedicated paragraph style. If i have to increment or decrement these numbers is possible to use a script or find/change grep action?

