activeDocument is not activeDocument...

activeDocument is not activeDocument...

6
NewbieNewbie
6

    Jul 23, 2007#1

    I get some strange results with my little function that should cut out some lines, paste them into a new file, save + close it and return back to the previous file:

    Code: Select all

    file_path = "C:\\ba\\inputfiles\\"
    
    for (var f = 1; f < 5; f++) {
    	sep_HCM_files(file_path + "hcm_in_" + f + ".txt");
    }
    
    function sep_HCM_files(fname) {
    	UltraEdit.activeDocument.key("CTRL+HOME");
    	UltraEdit.activeDocument.startSelect();
    	for (var i = 0; i < 45; i++) {
    		UltraEdit.activeDocument.key("DOWN ARROW");
    	}
    	UltraEdit.activeDocument.endSelect();
    	UltraEdit.activeDocument.cut();
    	UltraEdit.newFile();
    	UltraEdit.activeDocument.paste();
    	UltraEdit.saveAs(fname);
    	UltraEdit.closeFile(fname, 2);
    }
    
    Ok, the problem is:
    The first time the function is called, the 45 lines are correctly selected, cut out and pasted into the new file. After the new file was saved and closed, the original file has correctly gotten the focus again.
    But now in the second run of the loop (the one that calls the function), something goes terribly wrong: The next 45 lines are NOT cut out. Instead, the same clipboard content of the first run is used. Nevertheless, everything else works - a new file is created and so on and I don't receive any error.

    I now discovered that everything works well if I replace activeDocument by, lets say, document[0]. Obviously, this is a big disadvantage. I think I could get around this by getting the index of the active document but this is only a hack - why does is not work with activeDocument?

    262
    MasterMaster
    262

      Jul 23, 2007#2

      I think this should be considered a known error. newFile() automatically becomes activeDocument. But when closing a file, the file which comes into focus doesn't become activeDocument. You need a setActive() before working with activeDocument again or work directly on the document[] object.

      Please report the error to IDM Support. See e-mail address at top of page.

      I prefer to take control of which document object I work on with the hack you mention. See this variation of your script:

      Code: Select all

      var file_path = "C:\\temp\\"
      var workDoc = UltraEdit.document[getActiveDocumentIndex()];
      
      for (var f = 1; f < 3; f++) {
         sep_HCM_files(file_path + "hcm_in_" + f + ".txt");
      }
      
      function sep_HCM_files(fname) {
         workDoc.key("CTRL+HOME");
         workDoc.startSelect();
         for (var i = 0; i < 10; i++) {
            workDoc.key("DOWN ARROW");
         }
         workDoc.endSelect();
         workDoc.cut();
         UltraEdit.newFile();
         UltraEdit.activeDocument.paste();
         UltraEdit.saveAs(fname);
         UltraEdit.closeFile(fname, 2);
      }
      
      /* Find the tab index of the active document */
      function getActiveDocumentIndex() {
      	var tabindex = -1; /* start value */
      
      	for (i = 0; i < UltraEdit.document.length; i++)
      	{
      		if (UltraEdit.activeDocument.path==UltraEdit.document[i].path) {
      			tabindex = i;
      			break;
      		}
      	}
      	return tabindex;
      }