User to user discussion and support for UltraEdit, UEStudio, UltraCompare, and other IDM applications.

Help with writing and running scripts
6 posts Page 1 of 1
I created a script to count the number of characters in a double quoted string and return the value to the user with a messageBox call.

Code: Select all
// string length calculator

var len = 0;

var startFound = false;
var endFound   = false;

var msg = "Initial Message";

while (!UltraEdit.activeDocument.isChar('"') && UltraEdit.activeDocument.isColNumGt(1))
{
    UltraEdit.activeDocument.key("LEFT ARROW");
}

if (UltraEdit.activeDocument.isChar('"')
{
    startFound = true;
}

if (startFound)
{
    UltraEdit.activeDocument.key("RIGHT ARROW");
    while (!UltraEdit.activeDocument.isChar('"'))
    {
         len++;
         UltraEdit.activeDocument.key("RIGHT ARROW");
    }

    if (UltraEdit.activeDocument.isChar('"')
    {
        endFound = true;
    }

    if (endFound)
    {
        msg = "String Length = " + len;
    }
    else
    {
         msg = "ERROR: No end of string found!";
    }
}
else
{
    msg = "ERROR: No starting double quote found!";
}

UltraEdit.messageBox(msg, "String Length");

When I execute the script, I get nothing but a very brief dialog box about pressing cancel to abort the current operation.

But a test script I created to test the messageBox operation works just fine.

Code: Select all
// messageBox test

UltraEdit.messageBox("Message Box Test", "Message Box Title");

Can anyone explain to me what is going on and what the problem might be?

From a code logic standpoint, there is no reason why the messageBox call in my string length script should not execute.

Regards, Brian
There are two strings "if (UltraEdit.activeDocument.isChar('"')" with error - ")" is absent at end of the string.

Add UltraEdit.outputWindow.showWindow(true); to top of the script and you will able to see errors.
It's impossible to lead us astray for we don't care even to choose the way.
Arghhh!!!!

Thanks.

:oops:

I stared at this for hours and kept missing that.

Just an FYI,

I added the UltraEdit.outputWindow.showWindow(true); line Ovg suggested and it still didn't open the Output Window until I corrected the syntax errors.

The JavaScript engine must syntax check the entire script first before executing a single line.

I did get an Output Window report about an error on the line if I executed it with the output window already open.

I guess you should always have the output window open when executing a new script for the first time. :)

Regards, Brian
brianholl89 wrote: Just an FYI,

I added the UltraEdit.outputWindow.showWindow(true); line Ovg suggested and it still didn't open the Output Window until I corrected the syntax errors.

Yes, you are right, my fault :oops:. This should be invoked via Window->Output Window before execution of the script.
It's impossible to lead us astray for we don't care even to choose the way.
On developing a script the output window should be opened before running the script to see the error messages output by JavaScript interpreter.

Brian, UltraEdit displays the number of selected bytes in status bar at bottom of UltraEdit window. For an ASCII/ANSI file the number of selected bytes is equal the number of characters. For a Unicode file the number of selected bytes is double of selected characters.

Your script has several serious problems:

  1. It could run into an endless loop if a double quote is found in current line and there is no more double quote up to end of file.
  2. It could run into an endless loop if the script is executed on last line of file not containing a double quote and not having a line termination.
  3. It fails also if the setting Allow positioning beyond line end is enabled in configuration.
Here is my version of a script to determine length in characters of next double quoted string beginning in current line at current caret position or right of it not supporting an escape character.

Code: Select all
if (UltraEdit.document.length > 0)  // Is any file opened?
{
   UltraEdit.columnModeOff();
   var nLength = -1;

   // Get number of current line and current column.
   var nInitialLine = UltraEdit.activeDocument.currentLineNum;
   var nInitialColumn = UltraEdit.activeDocument.currentColumnNum;
   if (typeof(UltraEdit.activeDocumentIdx) == "undefined") nInitialColumn++;

   // Select string to end of current line.
   UltraEdit.activeDocument.startSelect();
   UltraEdit.activeDocument.key("END");
   UltraEdit.activeDocument.endSelect();

   // Was anything selected at all?
   if (UltraEdit.activeDocument.isSel())
   {
      var nQuotePos = UltraEdit.activeDocument.selection.indexOf('"');

      // Is there a double quote up to end of current line?
      if (nQuotePos >= 0)
      {
         // Set caret back to initial position.
         UltraEdit.activeDocument.gotoLine(nInitialLine,nInitialColumn);

         // Find the double quote in current line which definitely
         // exists and position caret after this double quote.
         UltraEdit.ueReOn();
         UltraEdit.activeDocument.findReplace.mode=0;
         UltraEdit.activeDocument.findReplace.matchCase=true;
         UltraEdit.activeDocument.findReplace.matchWord=false;
         UltraEdit.activeDocument.findReplace.regExp=false;
         UltraEdit.activeDocument.findReplace.searchDown=true;
         if (typeof(UltraEdit.activeDocument.findReplace.searchInColumn) == "boolean")
         {
            UltraEdit.activeDocument.findReplace.searchInColumn=false;
         }
         UltraEdit.activeDocument.findReplace.find('"');
         UltraEdit.activeDocument.key("LEFT ARROW");
         UltraEdit.activeDocument.key("RIGHT ARROW");

         // Is the character at this position a double quote?
         if (UltraEdit.activeDocument.isChar('"'))
         {
            nLength = 0;   // The double quoted string length is 0.
         }
         else
         {
            // Remember the current caret position after double quote.
            var nBeginLine = UltraEdit.activeDocument.currentLineNum;
            var nBeginColumn = UltraEdit.activeDocument.currentColumnNum;
            if (typeof(UltraEdit.activeDocumentIdx) == "undefined") nBeginColumn++;

            // Is there one more double quote anywhere in remaining file content?
            if(UltraEdit.activeDocument.findReplace.find('"'))
            {
               // Select everything to begin of double quoted string.
               UltraEdit.activeDocument.gotoLineSelect(nBeginLine,nBeginColumn);
               // Get number of characters of selected string.
               nLength = UltraEdit.activeDocument.selection.length;
            }
            else
            {
               nLength = -2;
            }
         }
      }
   }
   // Set caret back to initial position.
   UltraEdit.activeDocument.gotoLine(nInitialLine,nInitialColumn);

   var sMessage;

   if (nLength >= 0) sMessage = "Length of next double quoted string is: " + nLength.toString(10);
   else if (nLength == (-1)) sMessage = "ERROR: No double quote found up to end of current line!";
   else sMessage = "ERROR: End of string not found up to end of file!";
   UltraEdit.messageBox(sMessage,"String Length");
}
Best regards from Austria
Just a small adoption to Mofi's script.
It finds the first quoted string after the cursor, no matter if it's double quoted or single quoted. So for me it works in PL/SQL and in Basic and in C and ...
"bla" or 'blub' or "blub's bla" will be highlighted and length will be displayed.
Give it a try. :D

Code: Select all
// stringLength2.js
// Search next ' or ". That is then my String character. Determine its length and post it.
// "bla's text" or 'bla"s text' should both be interpreted automatically as one string
// thats why I have to search first if ' or " comes along. Then search again for THAT character
//
if (UltraEdit.document.length > 0)  // Is any file opened?
{
  UltraEdit.columnModeOff();
  var nLength = -1;
  var REGSEARCH = "";
  var REGSEARCH_EINFACHE = "['](.*)[']";
  var REGSEARCH_DOPPELTE = "[\"](.*)[\"]";
 
  // Get number of current line and current column for reset later on
  var nInitialLine   = UltraEdit.activeDocument.currentLineNum;
  var nInitialColumn = UltraEdit.activeDocument.currentColumnNum;
  //  UltraEdit.activeDocument.gotoLine(nInitialLine,nInitialColumn);
  if (typeof(UltraEdit.activeDocumentIdx) == "undefined") nInitialColumn++;
 
  // find first string following cursor either 'bla' or "blubb".
    UltraEdit.unixReOn();
    UltraEdit.activeDocument.findReplace.mode       = 0;
    UltraEdit.activeDocument.findReplace.matchCase  = true;
    UltraEdit.activeDocument.findReplace.matchWord  = false;
    UltraEdit.activeDocument.findReplace.regExp     = true;
    UltraEdit.activeDocument.findReplace.searchDown = true;
    if (typeof(UltraEdit.activeDocument.findReplace.searchInColumn) == "boolean")
    {
      UltraEdit.activeDocument.findReplace.searchInColumn=false;
    }
   
    // Search next ' or ". That is then my String character.
    // "bla's text" or 'bla"s text' should both be interpreted as one string.
    UltraEdit.activeDocument.findReplace.find("['\"]");
    if ( UltraEdit.activeDocument.selection == "'")
    {
      REGSEARCH = REGSEARCH_EINFACHE;
    }
    else
    {
      REGSEARCH = REGSEARCH_DOPPELTE;
    }
    UltraEdit.activeDocument.key("LEFT ARROW");
    UltraEdit.activeDocument.findReplace.find(REGSEARCH);
    nLength = UltraEdit.activeDocument.selection.length - 2;
//        if ( UltraEdit.activeDocument.currentLineNum != nInitialLine)
//        {
//         nLength = -1;
//        }
}

var sMessage = "Nothing found" ;
if (nLength >= 0)
{
  sMessage = "Length of the next String (marked) is: " + nLength.toString(10);
}
UltraEdit.messageBox(sMessage,"String Length");

UltraEdit.activeDocument.gotoLine(nInitialLine,nInitialColumn);
Normally using all newest english version incl. each hotfix. Win 10 64 bit
6 posts Page 1 of 1
cron