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

Help with writing and running scripts
4 posts Page 1 of 1
Hello,

To build travel plans, I often need to generate a list of days like this:

03 August
04 August
etc.

Does UE (20) provide a native tool to generate this kind of text, or maybe some macro/script I could use?

Thank you.
Writing such date data into a file can be done easily with a script.

Code: Select all
if (UltraEdit.document.length > 0)  // Is any file opened?
{
   var nDays = UltraEdit.getValue("How many days?",1);
   if (nDays > 0)
   {
      // Create an empty array for the date strings.
      var asDates = new Array(nDays);
      // Create a date object with today's date.
      var oDate = new Date();
      // Get milliseconds since 01.01.1970 00:00:00
      var nMilliSeconds = oDate.getTime();
      // Create an array for the month names.
      var asMonths = [ "January", "February", "March", "April", "May", "June", "July",
                       "August", "September", "October", "November", "December" ];

      // Create the date string for today and next (n-1) days.
      for (var nDate = 0; nDate < nDays; nDate++)
      {
         // Update date object from current value of milliseconds since 01.01.1970 00:00:00.
         oDate.setTime(nMilliSeconds);
         // Get day of month as string and insert a leading 0 if < 10.
         var sDay = oDate.getDate().toString();
         if (sDay.length < 2) sDay = "0" + sDay;
         // Append a space character and name of month and add the date string to the array.
         asDates[nDate] = sDay + " " + asMonths[oDate.getMonth()];
         // Increase milliseconds since 01.01.1970 00:00:00 by 1 day.
         nMilliSeconds += 86400000;
      }

      // Determine line termination type of active file.
      var sLineTerm;
      if (UltraEdit.activeDocument.lineTerminator < 1) sLineTerm = "\r\n";
      else if (UltraEdit.activeDocument.lineTerminator == 1) sLineTerm = "\n";
      else sLineTerm = "\r";

      // Write the dates joined with line termination into
      // active file in insert mode with column mode disabled.
      UltraEdit.insertMode();
      UltraEdit.columnModeOff();
      UltraEdit.activeDocument.write(asDates.join(sLineTerm));
   }
}

See documentation of JavaScript's Array, Date and String objects.
Best regards from Austria
Thanks much.

I can't get it to run in UE 20 (I can only see a dialog box being displayed in a fraction of a second, too fast to read what it says), but I'll look deeper in doing this in JavaScript.
You have saved the script code most likely into a Unicode file (UTF-16 or UTF-8 with BOM). Script files must be ASCII/ANSI files without BOM. I suppose that you see in output window opened before or after script execution the lines:

Code: Select all
An error occurred on line 1:

Script failed.

This always means the first character of the file could not be interpreted by internal JavaScript interpreter because of file is stored as Unicode file (with byte order mark).
Best regards from Austria
4 posts Page 1 of 1
cron