9 Replies Latest reply on Jun 6, 2014 1:57 AM by Kasyan Servetsky

    Automatically count total number of steps

    oneoff_swe Level 1

      I have a procedure with x steps. In the title we manually type the total number of steps. As you may understand this process often result in failure, where the number in the title does not correspond with the actual number of steps.


      Is there a solution to automatically get Indesign to count the steps below a certain paragraph style and add the number to the title. I haven’t found anything similar.


      For example, where "5" in the title is automatically generated:



      How to Count (5 Steps)

      1. Be patient, start by whispering "one".
      2. Then mentally count to two…
      3. … three
      4. … four
      5. Suddenly cry out “FIVE” and pretend that nothing has happen.


      // Thanks in advance

        • 2. Re: Automatically count total number of steps
          oneoff_swe Level 1

          Thanks for the info. But this didn't solve my problem, if it does please point out where I can find the information...


          I know how to handle and format an ordered list. But how can I make Indesign count the steps, and print the result in the title?

          • 3. Re: Automatically count total number of steps
            jmlevy Adobe Community Professional

            Sorry, I think I misunderstood the question. English is not my native language.

            • 4. Re: Automatically count total number of steps
              Peter Spier Most Valuable Participant (Moderator)

              You might be able to do it with a cross-reference and a numbered list, but that's at least as much work as counting the steps manually. I'd ask this over in scripting: InDesign Scripting

              • 5. Re: Automatically count total number of steps
                oneoff_swe Level 1

                The thought has also hit me to use cross-references, but as you say, it is not foolproof and will not automatically update iteself if a step is added/removed to the list.


                I'll move on to the Indesign scripting thread.

                • 6. Re: Automatically count total number of steps
                  Peter Spier Most Valuable Participant (Moderator)

                  Actually, auto-update is the one thing that will happen, as long as you don't add a new step after the current last one or delete that current last step (which is the not fool-proof part, I guess).

                  • 7. Re: Automatically count total number of steps
                    Kasyan Servetsky Level 5

                    This can be done quite easily by script. I wrote it while drinking my morning coffee.






                    I don't know which style names you're using so I used "Header" and "Steps". Change them to your style names in the script (in a plain text editor -- e.g. Notepad, ESTK):


                    If you add/remove some steps, rerun the script and it will update the numbers.

                    Below is the script and here is a sample document for testing.

                    Warning: I didn't see your docs so can't promise it will work for you as it is.

                    /* Copyright 2014, Kasyan Servetsky
                    June 4, 2014
                    Written by Kasyan Servetsky
                    e-mail: askoldich@yahoo.com */
                    var scriptName = "Count number of steps - 1.1",
                    //===================================== FUNCTIONS ======================================
                    function Main() {
                        var foundItem, header, lastPar, num;
                        app.findGrepPreferences = app.changeGrepPreferences = NothingEnum.NOTHING;
                        app.findGrepPreferences.findWhat = "(?s).+";
                        app.findGrepPreferences.appliedParagraphStyle = "Steps";
                        var foundItems = doc.findGrep(true);
                        if (foundItems.length == 0) ErrorExit("Found nothing", true);
                        for (var i = 0; i < foundItems.length; i++) {
                            foundItem = foundItems[i];
                            if (foundItem.paragraphs.length == 0) continue;
                            header = foundItem.parentStory.paragraphs.previousItem(foundItem.paragraphs[0]);
                            if (header.appliedParagraphStyle.name != "Header") continue;
                            lastPar = foundItem.paragraphs[-1];
                            num = lastPar.numberingResultNumber;
                            if (num == -1) continue;
                            InsertNum(header, num);
                        app.findGrepPreferences = app.changeGrepPreferences = NothingEnum.NOTHING;
                    function InsertNum(header, num) {
                        app.findGrepPreferences = app.changeGrepPreferences = NothingEnum.NOTHING;
                        app.findGrepPreferences.findWhat = "( \\()(\\d+)( Steps\\))$";
                        app.changeGrepPreferences.changeTo = "$1" + num + "$3";
                        app.findGrepPreferences.appliedParagraphStyle = "Header";
                        var changed = header.changeGrep();
                        if (changed.length == 0) {
                            header.insertionPoints[-2].contents = " (" + num + " Steps)";
                    function PreCheck() {
                        if (app.documents.length == 0) ErrorExit("Please open a document and try again.", true);
                        doc = app.activeDocument;
                        if (!app.activeDocument.saved) ErrorExit("The current document has not been saved since it was created. Please save the document and try again.", true);
                    function ErrorExit(error, icon) {
                        alert(error, scriptName, icon);
                    • 8. Re: Automatically count total number of steps
                      oneoff_swe Level 1

                      There goes my hero!

                      Thanks a lot. It was not my meaning to disturb you in the morning coffee.

                      My programming skills are quite limited. I'm trying to play with the code to
                      fix the two glitches I found, without any success....


                      1. Instead of using "Restart Numbering" we are using a specific
                        paragraph style for the first step that is always number one. (Maybe this is
                        just a show stopper and we may remove this paragraph style with "Restart Numbering"-command.) The problem is that the
                        script will not add this step to the counting, and next problem occurs.
                      2. There is a problem with other text (other paragraph styles) in front of
                        or between the steps. Such text seems to interrupt the counting. If another
                        paragraph style is in front of the ordered list, the script will not generate
                        any number at all in the heading. If there is other paragraph style in the
                        middle of the ordered list, the script will only count to the step before the interrupting
                        paragraph style. I want offcourse still restart the counting after a header.


                      Hope this is just a minor edit for you.


                      PS, Found your homepage where you are sharing other scripts you have made.
                      Quite impressive, and useful.



                      Thanks again.

                      • 9. Re: Automatically count total number of steps
                        Kasyan Servetsky Level 5

                        I was writing the script looking at the sample you gave in the original post.

                        The scenario is the following:

                        1. The script looks for blocks of text with “Steps” paragraph style applied
                        2. in the last paragraph in a block it reads the step number
                        3. then it looks at the paragraph preceding the first paragraph in the block, if it’s style name is “Header”, it continues
                        4. if the header already contains “(“ 1+ digits “ steps)” at the end, it updated only the number; if doesn’t, it inserts “(“  + the number + “ steps)”

                        That’s the way the script currently works. My purpose was to prove that it’s possible to do in principle.

                        In my opinion, developing a script, in most cases, is dependent on a specific document it is written for. Requesting me to write a script without providing me with a sample document for testing is alike asking me to draw your portrait without showing your face (I’m artist by education).