Skip to main content
Participant
May 14, 2025
Answered

Need to change numbers in an index +2 after 336

  • May 14, 2025
  • 3 replies
  • 635 views

I have an index for which all of the numbers have increased by 2 in the middle of the book (page 336). The index was created manually, not using InDesign's built-in tools. The book is 848 pages long. I've been searching for a way to do this. However, I don't know scripting or GREP. I'm hoping that someone here may be able to help me with this.

 

Thanks!

Correct answer m1b

Hi @Anthony D. Paular, in addition to the AppleScript that Eugene gave us, here is an ExtendScript version. If you need to re-purpose it, you can just change this function:

 

var changeNumber = function (n) { return n < 336 ? n : n + 2 };

 

Can you understand it? If n < 336 you get n but otherwise you get n + 2.

- Mark

 

/**
 * @file Change Numbers.js
 *
 * Changes numbers in selected text.
 *
 * @author m1b
 * @version 2025-05-15
 * @discussion https://community.adobe.com/t5/indesign-discussions/need-to-change-numbers-in-an-index-2-after-336/m-p/15321400#M624683
 */
function main() {

    // this is the number changing logic, you can edit this:
    var changeNumber = function (n) { return n < 336 ? n : n + 2 };

    var doc = app.activeDocument,
        text = doc.selection[0];

    if (
        undefined == text
        || 'function' !== typeof text.findGrep
    )
        return alert('Please select some text and try again.');

    // clear the find/change grep preferences
    app.findGrepPreferences = NothingEnum.nothing;
    // set the grep to search for page numbers
    app.findGrepPreferences.findWhat = '\\d+';
    // perform the find
    var found = text.findGrep();

    for (var i = found.length - 1; i >= 0; i--) {
        found[i].contents = String(changeNumber(Number(found[i].contents)));
    }

};
app.doScript(main, ScriptLanguage.JAVASCRIPT, undefined, UndoModes.ENTIRE_SCRIPT, 'Change Numbers');

Edit 2025-05-15: better explanation.

3 replies

m1b
Community Expert
m1bCommunity ExpertCorrect answer
Community Expert
May 15, 2025

Hi @Anthony D. Paular, in addition to the AppleScript that Eugene gave us, here is an ExtendScript version. If you need to re-purpose it, you can just change this function:

 

var changeNumber = function (n) { return n < 336 ? n : n + 2 };

 

Can you understand it? If n < 336 you get n but otherwise you get n + 2.

- Mark

 

/**
 * @file Change Numbers.js
 *
 * Changes numbers in selected text.
 *
 * @author m1b
 * @version 2025-05-15
 * @discussion https://community.adobe.com/t5/indesign-discussions/need-to-change-numbers-in-an-index-2-after-336/m-p/15321400#M624683
 */
function main() {

    // this is the number changing logic, you can edit this:
    var changeNumber = function (n) { return n < 336 ? n : n + 2 };

    var doc = app.activeDocument,
        text = doc.selection[0];

    if (
        undefined == text
        || 'function' !== typeof text.findGrep
    )
        return alert('Please select some text and try again.');

    // clear the find/change grep preferences
    app.findGrepPreferences = NothingEnum.nothing;
    // set the grep to search for page numbers
    app.findGrepPreferences.findWhat = '\\d+';
    // perform the find
    var found = text.findGrep();

    for (var i = found.length - 1; i >= 0; i--) {
        found[i].contents = String(changeNumber(Number(found[i].contents)));
    }

};
app.doScript(main, ScriptLanguage.JAVASCRIPT, undefined, UndoModes.ENTIRE_SCRIPT, 'Change Numbers');

Edit 2025-05-15: better explanation.

Community Expert
May 15, 2025

Far smarter than what i came up with 

 

(function () {
    if (app.documents.length === 0) {
        alert("No document open.");
        return;
    }

    var doc = app.activeDocument;
    var grepPattern = "\\b(33[7-9]|3[4-9]\\d|[4-7]\\d{2}|8[0-4][0-8])\\b";
    var found = doc.findGrep();

    // Clear previous grep preferences
    app.findGrepPreferences = NothingEnum.NOTHING;
    app.changeGrepPreferences = NothingEnum.NOTHING;

    // Set new pattern
    app.findGrepPreferences.findWhat = grepPattern;

    found = doc.findGrep();

    if (found.length === 0) {
        alert("No numbers between 337 and 848 found.");
        return;
    }

    app.doScript(function () {
        for (var i = 0; i < found.length; i++) {
            var match = found[i].contents;
            var newNum = parseInt(match, 10) + 2;
            found[i].contents = newNum.toString();
        }
    }, ScriptLanguage.JAVASCRIPT, undefined, UndoModes.ENTIRE_SCRIPT, "Index +2");
    
    // Clear preferences
    app.findGrepPreferences = NothingEnum.NOTHING;
    app.changeGrepPreferences = NothingEnum.NOTHING;

    alert("Done. Increased " + found.length + " numbers.");
})();
m1b
Community Expert
Community Expert
May 15, 2025

No that's great Eugene! You are doing a hard work to target the numbers in the target range only, which I don't do—I test every single number, so your code might actually be slightly faster on very long documents. Mine is easier to edit and maintain though, I think. 🙂

- Mark

Robert at ID-Tasker
Legend
May 14, 2025

@Anthony D. Paular

 

You could use Excel for that - copy your Index into Excel, separate numbers from text, split numbers, add a few formulas, "join" everything together.

 

You can share your index with me privately and I can do it for you - please click my nickname. 

 

Community Expert
May 14, 2025

This was posted recently 

TEST ON A COPY OF YOUR INDEX

https://community.adobe.com/t5/indesign-discussions/here-s-an-applescript-to-increase-every-number-by-1-for-static-references-etc/td-p/15193969

 

It can be changed - looks like the used ChatGPT says it right in the code

Haven't tested it cos on my PC - but the GREP is good so it should work 

-- Renumber (+2) starting from 337 onwards based on page range (page 336)
-- Built with ChatGPT, March 2025

tell application "Adobe InDesign 2025"
    -- Clear previous find preferences
    set find grep preferences to nothing

    -- Set GREP find preferences (matching page numbers from 337 onwards)
    set properties of find grep preferences to {find what:"\\b(33[7-9]|3[4-9]\\d|[4-7]\\d{2}|8[0-4]\\d|849)\\b"}

    -- Get all stories in the active document
    tell active document
        set allStories to stories

        -- Process each story separately
        repeat with s in allStories
            set foundItems to find grep of s

            -- Process numbers in reverse order to prevent indexing issues
            repeat with i from (count of foundItems) to 1 by -1
                set t to item i of foundItems
                try
                    -- Extract numeric text safely
                    set rawText to text of t as string

                    -- Ensure the text is a valid number
                    if rawText is not "" and rawText is not " " then
                        -- Increase number by 2
                        set newValue to (rawText as integer) + 2
                        set text of t to newValue as string
                    else
                        error "Unexpected non-numeric text: '" & rawText & "'"
                    end if

                on error errorMessage
                    -- Get page number safely
                    set pageNumber to "Unknown (not in a frame)"
                    try
                        if (parent text frames of t is not {}) then
                            set pageNumber to name of parent page of item 1 of (parent text frames of t)
                        end if
                    end try

                    -- Get paragraph text safely
                    set paragraphText to "Unable to retrieve paragraph"
                    try
                        set paragraphText to contents of (paragraph of t)
                    end try

                    -- Display error message and stop script
                    set fullErrorMessage to "Error processing text: '" & rawText & "'" & return & ¬
                        "Page: " & pageNumber & return & ¬
                        "Paragraph: " & paragraphText & return & ¬
                        "AppleScript Error: " & errorMessage

                    display dialog fullErrorMessage buttons {"OK"} default button "OK"

                    -- Stop execution immediately after first error
                    return
                end try
            end repeat
        end repeat
    end tell

    -- Clear GREP preferences after running
    set find grep preferences to nothing
end tell