Save all new, unnamed files with automatically generated file names

Help with writing and running scripts

Save all new, unnamed files with automatically generated file names

Postby fredtheman » Thu Dec 02, 2010 3:01 pm

BTW, when killing UltraEdit (eg. 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.

Here's what my UE looks like on any given day:-)

Before I write an AutoIT script to save unnamed files with dummy filenames, is there a way in UltraEdit to simply save all files to disk without prompting the user for a filename?

Thank you.
User avatar
fredtheman
Basic User
Basic User
 
Posts: 18
Joined: Sun Sep 05, 2004 11:00 pm

Re: Save all new, unnamed files with automatically generated file names

Postby Mofi » Fri Dec 03, 2010 1:23 am

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.
User avatar
Mofi
Grand Master
Grand Master
 
Posts: 4055
Joined: Thu Jul 29, 2004 11:00 pm
Location: Vienna

Re: Save all new, unnamed files with automatically generated file names

Postby fredtheman » Fri Dec 03, 2010 6:36 am

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.
User avatar
fredtheman
Basic User
Basic User
 
Posts: 18
Joined: Sun Sep 05, 2004 11:00 pm

Re: Save all new, unnamed files with automatically generated file names

Postby Mofi » Fri Dec 03, 2010 10:44 am

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 2 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 scriptSaveNotEmptyNewFiles.js can be easily modified to be downwards compatible by either removing or commenting the 2 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();
}
User avatar
Mofi
Grand Master
Grand Master
 
Posts: 4055
Joined: Thu Jul 29, 2004 11:00 pm
Location: Vienna

Re: Save all new, unnamed files with automatically generated file names

Postby fredtheman » Fri Dec 03, 2010 12:35 pm

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.
User avatar
fredtheman
Basic User
Basic User
 
Posts: 18
Joined: Sun Sep 05, 2004 11:00 pm

Re: Save all new, unnamed files with automatically generated file names

Postby Mofi » Sat Dec 04, 2010 5:17 pm

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.
User avatar
Mofi
Grand Master
Grand Master
 
Posts: 4055
Joined: Thu Jul 29, 2004 11:00 pm
Location: Vienna

Re: Save all new, unnamed files with automatically generated file names

Postby fredtheman » Sat Dec 04, 2010 6:49 pm

Thanks for the links.
User avatar
fredtheman
Basic User
Basic User
 
Posts: 18
Joined: Sun Sep 05, 2004 11:00 pm

Re: Save all new, unnamed files with automatically generated file names

Postby jorrasdk » Sun Dec 05, 2010 2:46 pm

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.
User avatar
jorrasdk
Master
Master
 
Posts: 275
Joined: Mon Mar 19, 2007 11:00 pm
Location: Denmark

Re: Save all new, unnamed files with automatically generated file names

Postby fredtheman » Fri Dec 17, 2010 5:52 am

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.
User avatar
fredtheman
Basic User
Basic User
 
Posts: 18
Joined: Sun Sep 05, 2004 11:00 pm


Return to Scripts