Make a Copy/Backup and Open in a New Window

Make a Copy/Backup and Open in a New Window

7
NewbieNewbie
7

    May 18, 2014#1

    Ever find yourself in the middle of what you thought would be some simple changes to a file which turned into massive changes and you wish you had started on a new file? Or maybe you want to use the current document as the basis of a an entirely different one, but you still want to keep the old one open? Of course you can either: click "Save As" and save the file with a new name then reopen the original file, or click on "Make a Copy/Backup" and save this new file, then "Revert To Saved" on the current file then open up the new file.

    So this is my version of "Make a Copy/Backup and Open in a New Window". I leave the "Revert to Saved" or many CTRL-Zs in the original file up to you.

    NOTE: This code was liberally and respectfully built (stolen) almost entirely from code written by Mofi and published on the UE/US forums as well as including the several required functions published by Mofi in the scripts downloads. I believe, as I'm sure do many others, that NOTHING would be accomplished in UE/US without the tremendous presence of Mofi among us. Thanks for everything Mofi!

    Code: Select all

    /* Script Name:   EditNewVersionOfFile.js
       Creation Date: 2014-05-18
       Last Modified: 2014-05-18
       Copyright:     Copyright (c) 2014 by Robert W. Hirn (rwhirn)
    
    The script   EditNewVersionOfFile   copies the active document to a new window
    and saves it as a new version of the original file with a quasi-versioning
    system by choosing the next available version number from the files in the same
    directory as the original file.
    
    Ever find yourself in the middle of what you thought would be some simple
    changes to a file which turned into massive changes and you wish you had started
    on a new file? Or maybe you want to use the current document as the basis of a
    an entirely different one, but you still want to keep the old one open? Of
    course you can either: click "Save As" and save the file with a new name then
    reopen the original file, or click on "Make a Copy/Backup" and save this new
    file, then "Revert To Saved" on the current file then open up the new file.
    
    So this is my version of "Make a Copy/Backup and open it in a new window". I
    leave the "Revert to Saved" or many CTRL-Zs in the original file up to you.
    
    NOTE: This code was liberally and respectfully built (stolen) almost entirely
    from code written by Mofi and published on the UE/US forums as well as including
    the several required functions published by Mofi in the scripts downloads. I
    believe, as I'm sure do many others, that NOTHING would be accomplished in UE/US
    without the tremendous presence of Mofi among us. Thanks for everything Mofi!
    
    ATTENTION:
    This script requires the functions in Mofi's FileNameFunctions which is by
    default not present in this script file. Download the source code of
    FileNameFunctions from
    https://www.ultraedit.com/resources/scripts/FileNameFunctions.js  then copy
    the whole function without the comments at the top and the demonstration
    code at the bottom and paste it into this file.
    
    This script requires Mofi's function GetListOfFiles which is by default not
    present in this script file. Download the source code of function GetListOfFiles
    from   https://www.ultraedit.com/resources/scripts/GetListOfFiles.js  then copy the
    whole function without the comments at the top and the demonstration code at the
    bottom and paste it into this file.
    
    This script is copyrighted by rwhirn for free usage by UE/UES users. The
    author cannot be responsible for any damage caused by this script. You
    use it at your own risk. */
    
    // Verify existence of required functions from FileNameFunctions collection
    if ((typeof(GetFileExt) == "function") && (typeof(GetFileName) == "function") &&
        (typeof(GetFilePath) == "function"))
    {
       // Verify existence of required function GetListOfFiles.
       if (typeof(GetListOfFiles) == "function")
       {
          // Remember current line and column.
          var nLine = UltraEdit.activeDocument.currentLineNum;
          var nColumn = UltraEdit.activeDocument.currentColumnNum;
          // Remember current column mode.
          var bColumnMode = false;
          if (typeof(UltraEdit.columnMode) == "boolean") bColumnMode = UltraEdit.columnMode;
          else if (typeof(UltraEdit.activeDocument.columnMode) == "boolean") bColumnMode = UltraEdit.activeDocument.columnMode;
          // Remember which clipboard is currently active.
          var nActiveClipboard = UltraEdit.clipboardIdx;
          // Use clipboard 9 for copying the data.
          UltraEdit.selectClipboard(9);
          UltraEdit.activeDocument.selectAll();
          UltraEdit.activeDocument.copy();
          UltraEdit.activeDocument.endSelect();
          UltraEdit.activeDocument.gotoLine(nLine,nColumn);
    
          var sOldFilePath = GetFilePath();
          if (sOldFilePath != "") {
             sOldFileName = GetFileName();
             sOldFileExt = GetFileExt(-1,true);
          } else {
             // new file not yet saved, save name displayed in the file tab.
             sOldFileName = UltraEdit.activeDocument.path;
             sOldFileExt = ".txt";
          }
          // create of new document and save its document id
          UltraEdit.newFile();
          UltraEdit.activeDocument.paste();
          var nActiveFile = UltraEdit.activeDocumentIdx;
    
          // default filename will be old filename with ".v2" inserted before file extension (if that filename isn't used already).
          var nVersion = 2;
          var sNewFileName = sOldFileName + ".v" + nVersion + sOldFileExt;
    
          // find a ".v" number that hasn't been used already
          if (GetListOfFiles(0,sOldFilePath,"",false)) {
             /* New active document is the newly-created list file. */
             UltraEdit.activeDocument.bottom();
             nNumFiles = UltraEdit.activeDocument.currentLineNum;
    
             if (typeof(UltraEdit.columnModeOff) == "function") UltraEdit.columnModeOff();
             else if (typeof(UltraEdit.activeDocument.columnModeOff) == "function") UltraEdit.activeDocument.columnModeOff();
             UltraEdit.ueReOn();
    
             // check in list of files for each ".v" number until one isn't found
             for (; nVersion <= nNumFiles; nVersion++) {
                // Insert ".v#" to left of ".extension"
                sNewFileName = sOldFileName + ".v" + nVersion + sOldFileExt;
                UltraEdit.activeDocument.top();
                UltraEdit.activeDocument.findReplace.searchDown=true;
                UltraEdit.activeDocument.findReplace.matchCase=false;
                UltraEdit.activeDocument.findReplace.matchWord=false;
                UltraEdit.activeDocument.findReplace.regExp=false;
                UltraEdit.activeDocument.findReplace.mode=0;
                UltraEdit.activeDocument.findReplace.find("\\" + sNewFileName);
                if (UltraEdit.activeDocument.isNotFound())
                {
                   break;
                }
             }
             UltraEdit.closeFile(UltraEdit.activeDocument.path,2);
          }
          sNewFilePathName = sOldFilePath + sNewFileName;
    
          UltraEdit.document[nActiveFile].top();
          UltraEdit.document[nActiveFile].setActive();
          // save this copy of file with the new filename
          UltraEdit.saveAs(sNewFilePathName);
          // we selectAll and then go to top here to force syntax highlighting to show
          UltraEdit.document[nActiveFile].selectAll();
          UltraEdit.document[nActiveFile].top();
          // free memory used by clipboard
          UltraEdit.clearClipboard();
          // restore environment
          UltraEdit.selectClipboard(nActiveClipboard);
          if (bColumnMode)
          {
             if (typeof(UltraEdit.columnModeOn) == "function") UltraEdit.columnModeOn();
             else if (typeof(UltraEdit.activeDocument.columnModeOn) == "function") UltraEdit.activeDocument.columnModeOn();
          }
       }
       else   // Function GetListOfFiles is missing. Report this error with the
       {      // same message in the output window as well as with a message box.
          UltraEdit.outputWindow.clear();
          UltraEdit.outputWindow.write("This script requires function GetListOfFiles.");
          UltraEdit.outputWindow.write("Please download the source code of GetListOfFiles from");
          UltraEdit.outputWindow.write("");
          UltraEdit.outputWindow.write("http://www.ultraedit.com/files/scripts/GetListOfFiles.js");
          UltraEdit.outputWindow.write("");
          UltraEdit.outputWindow.write("and copy the function into this script file.");
          UltraEdit.messageBox("This script requires function GetListOfFiles.\nPlease download the source code of GetListOfFiles from\n\nhttp://www.ultraedit.com/files/scripts/GetListOfFiles.js\n\nand copy the function into this script file.","EditNewVersionOfFile Error");
       }
    
    }
    else   // Function FileNameFunctions is missing. Report this error with the
    {      // same message in the output window as well as with a message box.
       UltraEdit.outputWindow.clear();
       UltraEdit.outputWindow.write("This script requires function FileNameFunctions.");
       UltraEdit.outputWindow.write("Please download the source code of FileNameFunctions from");
       UltraEdit.outputWindow.write("");
       UltraEdit.outputWindow.write("http://www.ultraedit.com/files/scripts/FileNameFunctions.js");
       UltraEdit.outputWindow.write("");
       UltraEdit.outputWindow.write("and copy the function into this script file.");
       UltraEdit.messageBox("This script requires function FileNameFunctions.\nPlease download the source code of FileNameFunctions from\n\nhttp://www.ultraedit.com/files/scripts/FileNameFunctions.js\n\nand copy the function into this script file.","EditNewVersionOfFile Error");
    }
    

    6,680583
    Grand MasterGrand Master
    6,680583

      May 18, 2014#2

      Nice script. Good work.
      Best regards from an UC/UE/UES for Windows user from Austria