0 Replies Latest reply on Nov 19, 2015 6:28 AM by The Gothamite

    Count Characters of Search Result

    The Gothamite Level 1

      Hey everyone!

       

      I am working on formatting code lines now. Bascially, they have line numbers and are handled using rule above/below to make the paragraphs look fancy. However, this requires me to make different paragraph styles for each line of code needed. However, I thought I'd automate this, because using a monospaced font, I can conclude that every line will never have more than 60 characters. In the raw text, a line that will have code formatting assigned will be prefixed with "bc. ". So the idea is simple.

       

      • Use RegEx ^bc\\. (.+?)$ to find code line
      • compare length of $1 or length of entire thing -4 with var lineLength which I set to 60.
      • Run search/replace to apply formatting and remove "bc. "

      Here's what I have so far:

       

      function codeLine() {
      ar curSel = app.selection[0];
      var searchTerm = "^bc\\. (.+?)$";
      //search term for ease of later use
      var lineLength = 60; //length of line in Code on page
      app.findGrepPreferences = app.changeGrepPreferences = null;
      // Reset search settings
      app.findGrepPreferences.findWhat = searchTerm;
      // Perform search for searchTerm
      var result = curSel.findGrep( true );
      app.findGrepPreferences = null;
        for ( var f = 0; f < result.length; f++ ) {
          //f is defined as 0, but if there are more results than 0, increment f by the number of results
          var curRes = result[f];
          // Defines curRes as the number of results f --> Last occurrence of searchTerm in selection
          var curCode = searchTerm.length - 4;
          if (curCode >= lineLength * 5) {
                //Applies Pre formatting in the correct length
                var applyFormat = "pre_5";
                findSettings();
                var myDocument = app.selection[0];
                app.findGrepPreferences.findWhat = searchTerm;
                app.changeGrepPreferences.changeTo = "$1"
                app.changeGrepPreferences.appliedParagraphStyle = applyFormat;
                resetFind(myDocument);
              } else if (curCode >= lineLength * 4) {
              //Applies Pre formatting in the correct length
              var applyFormat = "pre_4";
              findSettings();
              var myDocument = app.selection[0];
              app.findGrepPreferences.findWhat = searchTerm;
              app.changeGrepPreferences.changeTo = "$1"
              app.changeGrepPreferences.appliedParagraphStyle = applyFormat;
              resetFind(myDocument);
            } else if (curCode >= lineLength * 3) {
              //Applies Pre formatting in the correct length
              var applyFormat = "pre_3";
              findSettings();
              var myDocument = app.selection[0];
              app.findGrepPreferences.findWhat = searchTerm;
              app.changeGrepPreferences.changeTo = "$1"
              app.changeGrepPreferences.appliedParagraphStyle = applyFormat;
              resetFind(myDocument);
            } else if (curCode >= lineLength * 2) {
              //Applies Pre formatting in the correct length
              var applyFormat = "pre_2";
              findSettings();
              var myDocument = app.selection[0];
              app.findGrepPreferences.findWhat = searchTerm;
              app.changeGrepPreferences.changeTo = "$1"
              app.changeGrepPreferences.appliedParagraphStyle = applyFormat;
              resetFind(myDocument);
            } else {
              //Applies Pre formatting in the correct length
              var applyFormat = "pre_1";
              findSettings();
              var myDocument = app.selection[0];
              app.findGrepPreferences.findWhat = searchTerm;
              app.changeGrepPreferences.changeTo = "$1"
              app.changeGrepPreferences.appliedParagraphStyle = applyFormat;
              resetFind(myDocument);
            }
            }
      }
      

       

      Mind you, this code is as raw as it gets, is probably considered to be very ugly, and, most of all, it doesn't work. Basically, the only issue I have left is that when I go compare var curCode to var lineLength, curCode reverts back to searchTerm (minus four). It doesn't take the result, but the actual RegEx I've entered up there in line 03.

       

      So my question is this: How do I get to count characters in the search result. I don't mind if I can count the length of $1 or the entire thing. So, please, help?

       

      Thanks!