Document specific macros?

Document specific macros?

2
NewbieNewbie
2

    Apr 01, 2010#1

    I have successfully created a macro but it runs when I open any document. Is there a way to have a macro only run when a specific document (like C:\testing.txt) is opened? Like with MS Word? Thank you.

    6,603548
    Grand MasterGrand Master
    6,603548

      Apr 01, 2010#2

      Microsoft Word documents are binary files (or XML and other files in ZIP archives) and therefore can contain hidden data like Visual Basic code. Text files cannot contain hidden data. So it is not possible to save data like macro code, formatting styles, etc. together with the file itself.

      You can only design your macro executed on every file open to do something only for a specified file. You have to include your macro code in

      IfNameIs "name of the file without path and file extension"
      your macro code
      EndIf

      Alternatively there is

      IfExtIs "file extension without point"
      your macro code
      EndIf


      Another method is that the file on which the macro should run has as first character (= top of file) a character which normally (never) exists at top of any other file, for example character × (that is not a lower case X, that is the multiplication X) and your macro starts with

      IfCharIs "×"
      your macro code
      EndIf


      Last suggestion: Your macro first searches for a string existing only in this file and when it is found, it sets the cursor back to top of the file and runs the macro code.

      UnixReOff
      Find MatchCase "unique document string"
      IfNotFound
      ExitMacro
      EndIf
      your macro code

      This method has the disadvantage that it runs the search from top to bottom of every file making all file loads slower, especially for large and huge files.


      Unfortunately for macros it is not possible to evaluate the full file name. Scripts can do that, but not macros, not directly. The command CopyFilePath can be used to copy the full name of the active file into a clipboard (best clipboard 9), but for evaluating it for example which a search it must be inserted into the file and then a search upward is used to evaluate if this is the file on which the macro should run. This method modifies all files on which this macro is executed even when deleting the inserted file name after evaluation and therefore it is not really good practice.
      Best regards from an UC/UE/UES for Windows user from Austria

      2
      NewbieNewbie
      2

        May 15, 2010#3

        I need a way to to identify the full path of the file: path, file name, and extension. For example:

        "C:\MyDocs\MyApp.log"

        But it sounds like you are saying that UE can't do that? And I don't mean by placing that string somewhere in the file and having a macro look for that ..

        Just making sure, thanks.

        901
        MasterMaster
        901

          May 16, 2010#4

          If you don't mind an initial edit to the file, you could do it this way:

          Code: Select all

          Top
          Clipboard 9
          CopyFilePath
          Paste
          ClearClipboard
          Clipboard 0
          Find Up "C:\MyDocs\MyApp.log"
          IfFound
          Delete
             ...your macro code here...
          Else
          SelectToTop
          Delete
          EndIf
          Not very elegant, but it should work. The downside is that it will always leave a red line changed marker in the left margin of the first line.
          My long-term suggestion is that you write an email to IDM and ask for one or both of the following enhancements:


          1) New macro command: IfPathIs

          This would allow you to write your script as:

          Code: Select all

          IfPathIs "C:\MyDocs\"
          IfNameIs "MyApp"
          IfExtIs "log"
             ...your macro code here...
          EndIf
          EndIf
          EndIf
          2) New macro command: IfClipboardIs

          This would allow you to write your script as:

          Code: Select all

          CopyFilePath
          IfClipboardIs "C:\MyDocs\MyApp.log"
             ...your macro code here...
          EndIf