Save all new, unnamed files with automatically generated file names

Save all new, unnamed files with automatically generated file names

32
Basic UserBasic User
32

    Dec 02, 2010#1

    When killing UltraEdit (e.g. because Windows bugs me to reboot after an update) and recovering open files, I notice that not all the files that were open at the time are indeed restored.
    Because of this, I always copy all Edit.* files from C:\Documents and Settings\<current user>\Local Settings\Temp\ to a backup directory.

    The reason I do this, is that I find it a pain to actually come up with a name to save a file that only contains transient data.

    Before I write an AutoIt script to save unnamed files with dummy file names, is there a way in UltraEdit to simply save all files to disk without prompting the user for a file name?

    Thank you.

    6,680583
    Grand MasterGrand Master
    6,680583

      Dec 03, 2010#2

      There is Macro to save new files or you use the following UltraEdit script to save all not empty files without file name. The script as is requires UE v16.00 or later because of UltraEdit.activeDocumentIdx which could be replaced also by function getActiveDocumentIndex for versions of UltraEdit < 16.00.

      Script: SaveNotEmptyNewFiles.js

      Code: Select all

      var nFileCount = UltraEdit.document.length;
      if (nFileCount > 0) {
         var nIndexOfActiveFile = UltraEdit.activeDocumentIdx;
         var nFileIndex = 0;
         do {
            if (UltraEdit.document[nFileIndex].isName("") &&
                UltraEdit.document[nFileIndex].fileSize > 0) {
               UltraEdit.document[nFileIndex].setActive();
               UltraEdit.saveAs("C:\\Temp\\TempFile_"+nFileIndex+".txt");
            }
         } while (++nFileIndex < nFileCount);
         UltraEdit.document[nIndexOfActiveFile].setActive();
      }
      If you want close additionally all new empty files and save all modified named files, use following script:

      Script: SaveAllNotEmptyFiles.js

      Code: Select all

      var nFileCount = UltraEdit.document.length;
      if (nFileCount > 0) {
         var nFileIndex = 0;
         do {
            if (UltraEdit.document[nFileIndex].isName("")) {
               if (UltraEdit.document[nFileIndex].fileSize > 0) {
                  UltraEdit.document[nFileIndex].setActive();
                  UltraEdit.saveAs("C:\\Temp\\TempFile_"+nFileIndex+".txt");
               } else {
                  UltraEdit.closeFile(UltraEdit.document[nFileIndex].path,2);
                  nFileCount--;
                  nFileIndex--;
               }
            }
         } while (++nFileIndex < nFileCount);
         UltraEdit.saveAll(); // Save all modified named files
      }
      This second script is best for execution before exit of UltraEdit. Therefore it does not waste time with making the active file before script execution active again on script exit. Of course making the file before script execution active again before script exit could be also done, but care must be taken in this case because the file index of the file can change because of closing empty new files and the active file before script execution could be even a new empty file not present anymore after the loop.
      Best regards from an UC/UE/UES for Windows user from Austria

      32
      Basic UserBasic User
      32

        Dec 03, 2010#3

        Thanks for the tip. Since I'm running UE 15, I'll try the Macro to save new files.

        However, I've never used macros before, and the online help doesn't contain much.

        Is there a tutorial where I could learn how to write macros in UE, and get the macro above to run OK?

        Thank you.

        6,680583
        Grand MasterGrand Master
        6,680583

          Dec 03, 2010#4

          I posted a step by step introduction for macro creation at Help with macro creating. But I cannot recommend the usage of the macro nowadays. I developed the macro before IDM released UE v13.00 with the script support. The macro requires to modify the active file temporarily. That produces two unnecessary undo records and modifies active file even with nothing really changed by one. Also if the active file is read-only the macro would fail to insert the active file marker string. Therefore there is a loop count value to avoid an endless loop, but can result in not processing all files when the number of open files is greater than this loop value.

          The scripts are definitely better. The second script SaveAllNotEmptyFiles.js should work for UE v15.10 as is. The first script SaveNotEmptyNewFiles.js can be easily modified to be downwards compatible by either removing or commenting the two lines:

          var nIndexOfActiveFile = UltraEdit.activeDocumentIdx;
          and
          UltraEdit.document[nIndexOfActiveFile].setActive();

          or by making the small changes as I suggested (with keeping the faster activeDocumentIdx read access for UE v16.00+) resulting in:

          Code: Select all

          function getActiveDocumentIndex() {
             var tabindex = -1; /* start value */
          
             for (var i = 0; i < UltraEdit.document.length; i++)
             {
                if (UltraEdit.activeDocument.path==UltraEdit.document[i].path) {
                   tabindex = i;
                   break;
                }
             }
             return tabindex;
          }
          
          var nFileCount = UltraEdit.document.length;
          if (nFileCount > 0) {
             var nIndexOfActiveFile = 0;
             if (typeof(UltraEdit.activeDocumentIdx) != "number") {
                nIndexOfActiveFile = getActiveDocumentIndex();
             } else {
                nIndexOfActiveFile = UltraEdit.activeDocumentIdx;
             }
             var nFileIndex = 0;
             do {
                if (UltraEdit.document[nFileIndex].isName("") &&
                    UltraEdit.document[nFileIndex].fileSize > 0) {
                   UltraEdit.document[nFileIndex].setActive();
                   UltraEdit.saveAs("C:\\Temp\\TempFile_"+nFileIndex+".txt");
                }
             } while (++nFileIndex < nFileCount);
             UltraEdit.document[nIndexOfActiveFile].setActive();
          }
          Best regards from an UC/UE/UES for Windows user from Austria

          32
          Basic UserBasic User
          32

            Dec 03, 2010#5

            Thanks. What if I already have C:\Temp\TempFile_0.txt from a previous session, launch UE, create a new unnamed file, and run the script: Will it silently replace TempFile_0.txt with this new file, or does it check what the last index is, and take it from there? I just tested this. The script silently replaces the previous file with the new ones. Ouch, just lost several files :-/

            I'll see how to add code to check for this.

            I added this to generate a unique UUID.

            Code: Select all

            function S4() {
               return (((1+Math.random())*0x10000)|0).toString(16).substring(1);
            }
            function guid() {
               return (S4()+S4()+"-"+S4()+"-"+S4()+"-"+S4()+"-"+S4()+S4()+S4());
            }         
            
            function getActiveDocumentIndex() {
            ...
                     //UltraEdit.saveAs("C:\\Temp\\TempFile_"+nFileIndex+".txt");
                     UltraEdit.saveAs("C:\\Temp\\TempFile_"+guid()+".txt");
                  }
            The script works fine now :)

            BTW, what tool do you use to debug a JavaScript script? I had a typo somewhere, and it took me 15 min to figure it out, since UE doesn't return any info.

            Thank you.

            6,680583
            Grand MasterGrand Master
            6,680583

              Dec 04, 2010#6

              I don't use an additional tool. (I'm really good in writing code :-) ) The JavaScript engine itself checks on preprocessing stage for syntax mistakes and outputs errors found. The output of the JavaScript engine is written by UltraEdit to the output window. But the output window is not automatically opened. It is necessary that you open the output window to see errors reported by the JavaScript engine. Therefore I open the output window always when testing a new script.

              Further there is JavaScript Lint, a JavaScript code checker tool. UEStudio has built-in support for this code checker. In UltraEdit this tool must be integrated by configuring a user tool for checking active JavaScript file. There is the power tip Configure UltraEdit with JavaScript Lint.

              See also Newbie questions on debugging and positions of selection.
              Best regards from an UC/UE/UES for Windows user from Austria

              32
              Basic UserBasic User
              32

                Dec 04, 2010#7

                Thanks for the links.

                262
                MasterMaster
                262

                  Dec 05, 2010#8

                  fredtheman wrote:BTW, what tool do you use to debug a JavaScript script? I had a typo somewhere, and it took me 15 min to figure it out, since UE doesn't return any info
                  Quite often UE will only tell you: "...An error occured on line X..." in the output window. Which error ?

                  Well a technique to quickly locate an error is to wrap your code in try/catch blocks like:

                  Code: Select all

                  try {
                    var b = 1;
                    var a = nonexistingFunc();
                    UltraEdit.outputWindow.write("a+b="+a+b);
                  }
                  catch (err) {
                    UltraEdit.outputWindow.write(err.toString());
                  }
                  The actual error otherwise only reported as "an error occured" will now be written to the output window as:
                  "ReferenceError: nonexistingFunc is not defined"
                  a much more meaningful error text.

                  32
                  Basic UserBasic User
                  32

                    Dec 17, 2010#9

                    Thansk for the useful tip.

                    BTW, I just noticed a small bug in the script to save all unnamed files: My XP host crashed, so when I restarted UE, I was prompted whether I wanted to restore files that were open when it crashed (all "Edit.123" files), and when replying Yes and running the script, those files were not renamed: I guess UltraEdit doesn't consider them to be in the same state as "really" new files, so that this line fails and the files aren't saved:

                    Code: Select all

                    if (UltraEdit.document[nFileIndex].isName("") && UltraEdit.document[nFileIndex].fileSize > 0)
                    A simple work-around is to simply open a new document and copy/paste the contents, but if someone knows of another API to call to solve this, I'm interested :)

                    Thank you.