Automatic File Name Suggestion

Automatic File Name Suggestion

2
NewbieNewbie
2

    May 03, 2011#1

    Is there any method available, through scripting or .INI configuration, to get UltraEdit to automatically suggest a file name based upon the content of the first line in the file? MS Office applications and other text editors do this and I've grown accustomed to having this capability.

    Since the first line of many files state the intent of the document, it seems like a natural feature for UltraEdit to have but I've found no mention of it in the help files or within this forum.

    Thanks,
    ~S

    6,675585
    Grand MasterGrand Master
    6,675585

      May 04, 2011#2

      A text editor like UltraEdit is mainly used by programmers, HTML/PHP writers, script writers, etc., but not by document writers. Therefore usually the first line of a file does not contain something being useful for a file name. There is no option in UltraEdit to use the first line of a file as file name in the Save As dialog.

      You can use a script executed by hotkey to prepare the file name based on first line in file in the clipboard which also opens the Save As dialog. But you have to press Ctrl+V to paste the file name into the edit field in the Save As dialog.

      Code: Select all

      var nLine = UltraEdit.activeDocument.currentLineNum;
      var nColumn = UltraEdit.activeDocument.currentColumnNum;
      UltraEdit.activeDocument.top();
      UltraEdit.activeDocument.startSelect();
      UltraEdit.activeDocument.key("END");
      if (UltraEdit.activeDocument.isSel()) {
         UltraEdit.selectClipboard(0);
         UltraEdit.activeDocument.copy();
      }
      UltraEdit.activeDocument.endSelect();
      UltraEdit.activeDocument.gotoLine(nLine,nColumn);
      UltraEdit.saveAs("");
      Using the selected first line in call of saveAs command would result in saving the file automatically with that string in current working directory without showing the Save As dialog and therefore without possibility to modify the file name if not useful or if the first line of the file contains characters not valid for a file name.


      My personal point of view about that "feature" in MS Office: It makes users lazy in thinking about and entering a good file name which really let the users later know what the file contains by just seeing its file name. As a user who gets often *.doc files without good file names because file was saved with first line in the document, I would wish MS Word would not have this feature and users would need to enter a file name.

      2
      NewbieNewbie
      2

        May 04, 2011#3

        Thank you for your excellent script suggestion and insight, Mofi!

        In my case, I write HTML, scripts, XML, and unformatted document content (for later insertion into a formatted layout) too. And I agree that the auto file name generation of MS Office applications can promote misleading file names. I routinely modify the name suggestion to something more appropriate. However, the initial suggestion itself often points me in the right direction.

        I find it slightly faster (at times) to modify something that exists than to create a name from scratch. The default "Edit1" file name provided by UltraEdit is never used in my new file names . . . so it seemed to me that if the application can suggest "Edit1" then perhaps it could suggest a more context appropriate name as well.

        Thanks again!
        ~S

        2
        NewbieNewbie
        2

          Jul 03, 2012#4

          I am trying to aumatically save a newly created file to a file name created programmatically in a variable.

          Without a file name, the SaveAs dialog appears.

          Code: Select all

          UltraEdit.saveAs("");
          With a variable created in the script (like appending "new" to the original file), I always get an error:

          Code: Select all

          var newtext = "\"" + "newfile.txt" + "\"";
          UltraEdit.outputWindow.write(newtext);
          
          UltraEdit.readOnlyOff;
          UltraEdit.saveAs(newtext);
          The error message is File/Device maybe ReadOnly, or open for write by another application. Or if I hard code the file name, .saveAs("newfile.txt"), then it works fine. But I do need to create a new file name, related to an existing file name for this script.

          Thanks in advance.

          6,675585
          Grand MasterGrand Master
          6,675585

            Jul 04, 2012#5

            The mistake in your script code is that you created a file name with double quotes. The double quote character is not allowed in a file name. Double quotes must be used only on command line if full file name contains a space character. The double quotes determine where the string of the file name starts and where it ends, but do not belong to the file name itself.

            I offer two solutions. The first one is very quick on execution, but does not work for all possibilities that could occur. However, it works very well if you execute the script always on a file which has already a file extension or is a new file not yet saved.

            Code: Select all

            var sActFileName = UltraEdit.activeDocument.path;
            var nLastDot = sActFileName.lastIndexOf('.');
            if (nLastDot >= 0) {
               // Insert "_new" left to last dot which is hopefully the dot separating the
               // file extension from the file name and not a dot anywhere in file path.
               var sNewFileName = sActFileName.substr(0,nLastDot) + "_new" + sActFileName.substr(nLastDot);
            } else {
               // The file name has no dot (new file) and therefore append ".txt".
               var sNewFileName = sActFileName + ".txt";
            }
            UltraEdit.saveAs(sNewFileName);
            The ultimate solution working in all possible cases would require using the general file name evaluating functions. You would need to copy and paste the code of the functions GetFilePath, GetFileName and GetFileExt into your script and append at end of the script below the functions following code:

            Code: Select all

            // Insert ".new" left to ".extension" or in case the active document is a
            // new file not yet saved, append ".txt" to name displayed in the file tab.
            var sNewFileName = GetFilePath();
            if(sNewFileName != "") sNewFileName += GetFileName() + "_new" + GetFileExt(-1,true);
            else sNewFileName = UltraEdit.activeDocument.path + ".txt";
            UltraEdit.saveAs(sNewFileName);

            2
            NewbieNewbie
            2

              Jul 05, 2012#6

              Thanks very much. I was misled by the text in the Scripting commands help which reads, "Save the active file as the filename specified. The filename must be in quotes."