Per file script or macro?

Per file script or macro?

8
NewbieNewbie
8

    Apr 05, 2021#1

    I've read that you can assign a script or macro to a file type such as .txt files.
    Is there a way to assign a script or macro to a specific file?
    For example: C:\Users\T\Documents\notes.rtf
    I'm using UE version 25.00.0.58.
    Thanks.

    6,675585
    Grand MasterGrand Master
    6,675585

      Apr 05, 2021#2

      It is not really possible to assign a script or macro to a file type such as .txt files or to a specific file.

      But an UltraEdit script can be coded to run only on a specific file or files with a specific extension.

      Example for a script running only on C:\Users\T\Documents\notes.rtf:

      Code: Select all

      // Case-sensitive compared full qualified file name of the file to process.
      var g_sFullFileName = "C:\\Users\\T\\Documents\\notes.rtf";
      
      function SelectOrOpenFile()
      {
         // Look if one of the already opened files is the file to process
         // and make this file the active file on really already opened.
         for (var nDocIndex = 0; nDocIndex < UltraEdit.document.length; nDocIndex++)
         {
            if (UltraEdit.document[nDocIndex].path == g_sFullFileName)
            {
               UltraEdit.document[nDocIndex].setActive();
               UltraEdit.activeDocument.top();
               return true;
            }
         }
         var nFileCount = UltraEdit.document.length;
         UltraEdit.open(g_sFullFileName);
         // Return true if the number of opened files changed because the file
         // to process really exists and is opened now in UltraEdit. Otherwise
         // return false on file to process does not exist. There is an error
         // message displayed by UltraEdit on opening of the file fails.
         return (nFileCount != UltraEdit.document.length);
      }
      
      if (SelectOrOpenFile())
      {
         // Code to process the file.
      }
      
      An UltraEdit macro does not support variables. For that reason it is more difficult to write a macro for a specific file. It is possible to open the file to process which makes the file active on being already opened and check if the file name without path is the expected file name and the file extension is the expected file extension to verify if the active file is most likely the file specified to open.

      Code: Select all

      InsertMode
      ColumnModeOff
      HexOff
      Open "C:\Users\T\Documents\notes.rtf"
      IfNameIs "notes"
      IfExtIs "rtf"
      Top
      "Code to process the file."
      EndIf
      EndIf
      
      The macro makes the opened file the active file even on file being already opened on execution of the macro. If the file is already opened and there are not saved modifications, UltraEdit prompts the user if the file should be reloaded from storage media with loosing all modifications or keep the file as is with the modifications. It is not possible to check against the full name, at least not without writing the full file name of active file temporarily into the active file if the active file is not read-only. The simple checks if file name and file extension of active file are the expected strings should be enough to avoid running the macro on a different file in case of the file to open does not exist at all.
      Best regards from an UC/UE/UES for Windows user from Austria