• Login
Community
  • Login

Macro script when txt content changes

Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
20 Posts 4 Posters 1.3k Views
Loading More Posts
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M
    Mirko Rossi
    last edited by Feb 26, 2021, 12:46 PM

    thank you all for the quick responses and I apologize for my very rough english.

    The key combination “Ctrl + Shift + 9” simply calls a macro.

    The txt file is generated by an old radio automation and contains a simple text “Title - Interpreter”. Below is an example of the txt:
    “Regardless - Raye”

    I need to invert the title with the interpreter in order to allow the correct association of the cover albums that are requested and retrieved by the streaming players.
    I have created the macro and it works correctly; The whole process, up to the final acquisition of the cover, works … obviously by manually activating the macro.

    I used notepad ++, the macro and only the “Document monitor” plugin (which reads the file in real time).

    the txt is updated every song change and so I wanted to create a script, which calls the macro or the key combination, whenever the content of the file changes.

    thank you all
    Mirko

    grazie a tutti per le rapide risposte e mi scuso per il mio inglese molto approssimativo.

    La combinazione di tasti “Ctrl+Shift+9” richiama semplicemente una macro.

    Il file txt viene generato da una vecchia radio automation e contiene un semplice testo “Titolo - Interprete”. A seguire un esempio del txt:
    “Regardless - Raye”

    Ho necessità di invertire il titolo con l’interprete, al fine di permettere l’associazione corretta delle cover album che vengono richieste e recuperate dai player streaming.
    Ho creato la macro e funziona correttamente; Tutto il processo, fino all’acquisizione finale della cover, funziona…ovviamente attivando manualmente la macro.

    ho usato notepad++, la macro e il solo plugin “Document monitor” (che legge in real time il file).

    il txt viene aggiornato ogni cambio canzone e quindi volevo creare uno script, che richiama la macro o la combinazione di tasti, ogni qual volta il contenuto del file cambia.

    grazie
    Mirko

    1 Reply Last reply Reply Quote 0
    • M
      Mirko Rossi
      last edited by Feb 26, 2021, 12:49 PM

      @Ekopalypse said in Macro script when txt content changes:

      PythonScript

      no problem, just get to the solution

      A 1 Reply Last reply Feb 26, 2021, 1:16 PM Reply Quote 0
      • A
        Alan Kilborn @Mirko Rossi
        last edited by Feb 26, 2021, 1:16 PM

        @Mirko-Rossi

        So if I’m putting the pieces of the above together correctly…

        The idea of using SC_MOD_INSERTCHECK, if it will truly work in this case, would probably require the PythonScript callback, and not the macro, to do the changing of the text? At least that’s the only way I’ve ever used such a thing (via the editor.changeInsertion() function).

        So…probably publishing the macro here, and having its functionality converted to PythonScript code, is probably a good idea?

        M 1 Reply Last reply Feb 26, 2021, 1:52 PM Reply Quote 1
        • M
          Mirko Rossi @Alan Kilborn
          last edited by Feb 26, 2021, 1:52 PM

          @Alan-Kilborn

          I think so. if the final result doesn’t change, you can use PythonScript completely.
          is there any way to give you the code of a macro? to then convert it with PythonScript

          thank you

          penso di si. se il risultato finale non cambia, si può usare completamente PythonScript.
          esiste un modo per darvi il codice di una macro? per poi convertirlo con PythonScript

          grazie

          1 Reply Last reply Reply Quote 0
          • M
            Mirko Rossi
            last edited by Feb 26, 2021, 1:56 PM

            <Macro Key=“53” Shift=“yes” Alt=“yes” Ctrl=“yes” name=“intero”>

            <Action sParam=“” lParam=“0” wParam=“0” message=“2302” type=“0”/>
            <Action sParam=“” lParam=“0” wParam=“0” message=“2453” type=“0”/>
            <Action sParam=“” lParam=“0” wParam=“0” message=“2180” type=“0”/>
            <Action sParam=“” lParam=“0” wParam=“0” message=“2451” type=“0”/>
            <Action sParam=“” lParam=“0” wParam=“0” message=“2326” type=“0”/>
            <Action sParam=“” lParam=“0” wParam=“0” message=“1700” type=“3”/>
            <Action sParam=“-” lParam=“0” wParam=“0” message=“1601” type=“3”/>
            <Action sParam=“” lParam=“2” wParam=“0” message=“1625” type=“3”/>
            <Action sParam=“” lParam=“768” wParam=“0” message=“1702” type=“3”/>
            <Action sParam=“” lParam=“1” wParam=“0” message=“1701” type=“3”/>
            <Action sParam=“” lParam=“0” wParam=“0” message=“2319” type=“0”/>
            <Action sParam=“” lParam=“0” wParam=“0” message=“2177” type=“0”/>
            <Action sParam=“” lParam=“0” wParam=“0” message=“2453” type=“0”/>
            <Action sParam=“” lParam=“0” wParam=“0” message=“2179” type=“0”/>
            <Action sParam=“” lParam=“0” wParam=“0” message=“2326” type=“0”/>
            <Action sParam=" " lParam=“0” wParam=“0” message=“2170” type=“1”/>
            <Action sParam=“-” lParam=“0” wParam=“0” message=“2170” type=“1”/>
            <Action sParam=" " lParam=“0” wParam=“0” message=“2170” type=“1”/>
            <Action sParam=“” lParam=“0” wParam=“0” message=“2453” type=“0”/>
            <Action sParam=“” lParam=“0” wParam=“0” message=“2180” type=“0”/>
            <Action sParam=“” lParam=“0” wParam=“0” message=“2180” type=“0”/>
            </Macro>

            it’s enough?

            A P 2 Replies Last reply Feb 26, 2021, 2:02 PM Reply Quote 0
            • M
              Mirko Rossi
              last edited by Feb 26, 2021, 2:00 PM

              the path to the file is “\SERVER\Disco_C\rds.txt”
              …on the local network LAN, not locally

              1 Reply Last reply Reply Quote 1
              • A
                Alan Kilborn @Mirko Rossi
                last edited by Feb 26, 2021, 2:02 PM

                @Mirko-Rossi said in Macro script when txt content changes:

                it’s enough?

                Yes. I was worried that it was going to be extremely long. That one is of a moderate length so that it would not be too difficult for someone to reverse engineer what it does.

                1 Reply Last reply Reply Quote 0
                • P
                  PeterJones @Mirko Rossi
                  last edited by Feb 26, 2021, 2:09 PM

                  @Mirko-Rossi said in Macro script when txt content changes:

                  path to the file is “\SERVER\Disco_C\rds.txt”
                  …on the local network LAN, not locally

                  Do you mean \\SERVER\Disco_C\rds.txt ? (the forum treats \\ as an escaped backslash, so only displayed one)

                  [macro contents…]
                  it’s enough?

                  That is the macro code, yes, so that can be translated into equivalent PythonScript commands.

                  But I think you still haven’t responded to when @Ekopalypse asked,

                  What puzzles me a bit is the document monitor, when it is on, the document is read-only from the Npp point of view, isn’t it? If you are willing to explain in more detail what needs to be done, …

                  In case you didn’t understand, often times with document-monitoring turned on, the file is marked as read-only, so that Notepad++ cannot edit it while it’s being written to by some external process. Otherwise, you get in the dangerous condition where both the external process and Notepad++ both try to write the file to disk at the same time. It might be nice to know how often the external process writes to the file, because that might influence whether or not there’s enough time to safely run your commands and save the file before the next time that the external process writes to it. (Then again, since you seem able to run the macro manually between refreshes, without adverse effects, it’s probably okay for the script, too.)

                  The process you want, as I understand it:

                  1. The Document Monitor plugin will be used to refresh the contents of the editor window periodically
                  2. You want to check the contents after such a refresh, and see if the contents have changed
                  3. If they have changed, run the Macro you listed (or the equivalent translation into Python Script commands), then save the file
                  4. Repeat ad infinitum

                  Please answer about “read only” status, and please add any additional details not captured in my summary of the process. After that, there will probably be enough for someone who understands @Ekopalypse’s SC_MOD_INSERTCHECK suggestion to implement a possible script.

                  A M 3 Replies Last reply Feb 26, 2021, 2:12 PM Reply Quote 0
                  • A
                    Alan Kilborn @PeterJones
                    last edited by Feb 26, 2021, 2:12 PM

                    @PeterJones

                    It might be nice to know how often the external process writes to the file, because that might influence whether or not there’s enough time to safely…

                    Here’s a hint:

                    the txt is updated every song change

                    I’m envisioning a DJ type situation, including disco ball…roughly 3 minute songs. Could be totally wrong, though!

                    1 Reply Last reply Reply Quote 1
                    • E
                      Ekopalypse
                      last edited by Ekopalypse Feb 26, 2021, 9:17 PM Feb 26, 2021, 9:15 PM

                      Ok, I misunderstood what was meant by “Document Monitor”.
                      I assumed we are talking about this one

                      2ef2cedf-0430-4c80-ae35-735b6ddf852c-image.png

                      So, as @PeterJones already mentioned, this one is generating a buffer activated event, which means your could do something like this

                      from Npp import notepad, editor, NOTIFICATION
                      import hashlib
                      
                      def md5sum(): return hashlib.md5(editor.getText()).hexdigest()
                      
                      def on_buffer_activate(args):
                          global CURRENT_HASH
                          if args['bufferID'] == FILE_TO_BE_MONITORED:
                              NEW_HASH = md5sum()
                              if CURRENT_HASH != NEW_HASH:
                                  CURRENT_HASH = NEW_HASH
                                  notepad.runMenuCommand('Macro', 'YOUR_MACRO_NAME')
                      
                      FILE_TO_BE_MONITORED = notepad.getCurrentBufferID()
                      CURRENT_HASH = md5sum()
                      notepad.callback(on_buffer_activate, [NOTIFICATION.BUFFERACTIVATED])  
                      

                      You have to give the document, which is monitored, the focus and run the script. I how this does what you want it to be done.

                      If you are running a localized version of npp, you need to replace Macro with the localized name.

                      1 Reply Last reply Reply Quote 2
                      • M
                        Mirko Rossi @PeterJones
                        last edited by Mar 1, 2021, 10:59 AM

                        it seems not to work.
                        I created and ran the script from the plugin menu. Am I wrong some steps?

                        This is the script code:

                        from Npp import notepad, editor, NOTIFICATION
                        import hashlib
                        
                        def md5sum(): return hashlib.md5(editor.getText()).hexdigest()
                        
                        def on_buffer_activate(args):
                            global CURRENT_HASH
                            if args['bufferID'] == FILE_TO_BE_MONITORED:
                                NEW_HASH = md5sum()
                                if CURRENT_HASH != NEW_HASH:
                                    CURRENT_HASH = NEW_HASH
                                    notepad.runMenuCommand('\\SERVER\Disco_C\rds.txt', 'intero')
                        
                        FILE_TO_BE_MONITORED = notepad.getCurrentBufferID()
                        CURRENT_HASH = md5sum()
                        notepad.callback(on_buffer_activate, [NOTIFICATION.BUFFERACTIVATED]) 
                        

                        @Ekopalypse said in Macro script when txt content changes:

                                notepad.runMenuCommand('Macro', 'YOUR_MACRO_NAME')
                        

                        i think i didn’t understand how to compile

                        E 1 Reply Last reply Mar 1, 2021, 11:04 AM Reply Quote 2
                        • E
                          Ekopalypse @Mirko Rossi
                          last edited by Mar 1, 2021, 11:04 AM

                          @Mirko-Rossi

                          \\SERVER\Disco_C\rds.txt is the file which is monitored, correct? And intero is the name of the macro?

                          If so, call notepad.runMenuCommand('Macro', 'intero')
                          but only, if Macro is the name of the menu.

                          1 Reply Last reply Reply Quote 2
                          • M
                            Mirko Rossi @PeterJones
                            last edited by Mar 1, 2021, 11:27 AM

                            ok, it starts up but doesn’t work properly. The macro, if called manually, works correctly; if called via script (automatically) it compares badly.

                            I write an example…
                            Original txt, starting: “Shake you down - Gregory Abbott”
                            This is the result with the automatic script: Shake you dow - regory Abbott
                            This is the result if I run the macro manually: Gregory Abbott - Shake you down

                            strange, right?
                            thanks

                            E 1 Reply Last reply Mar 1, 2021, 11:35 AM Reply Quote 0
                            • M
                              Mirko Rossi
                              last edited by Mirko Rossi Mar 1, 2021, 11:29 AM Mar 1, 2021, 11:29 AM

                              another example
                              txt original: “No woman no cry - Bob Marley”
                              no script: Bob Marley - No woman no cry
                              with script: No woman no cr - ob Marley

                              deletes one more character, next to the final leading quotes "

                              1 Reply Last reply Reply Quote 0
                              • E
                                Ekopalypse @Mirko Rossi
                                last edited by Ekopalypse Mar 1, 2021, 11:35 AM Mar 1, 2021, 11:35 AM

                                @Mirko-Rossi

                                that is strange, indeed. May I ask you how exactly you did it?
                                I expect the following:

                                1. The file \SERVER\Disco_C\rds.txt is open in npp.
                                2. You start the document monitor
                                3. You start the script via the PythonScript menu, BUT only once. If you call it multiple times, the macro will be called the same number of times. I should have mentioned that before, sorry.

                                For testing, open the PythonScript console (via menu) and run notepad.clearCallbacks() after each try to be sure that no
                                callback is active anymore. Or add this line before notepad.callback(on_buffer_activate, [NOTIFICATION.BUFFERACTIVATED])

                                M 1 Reply Last reply Mar 1, 2021, 12:04 PM Reply Quote 3
                                • M
                                  Mirko Rossi @Ekopalypse
                                  last edited by Mirko Rossi Mar 1, 2021, 12:05 PM Mar 1, 2021, 12:04 PM

                                  @Ekopalypse said in Macro script when txt content changes:

                                  You start the script via the PythonScript menu, BUT only once. If you call it multiple times, the macro will be called the same number of times. I should have mentioned that before, sorry.

                                  ahahah you are right :) The title and the interpreter are reversed twice … I should have guessed it.

                                  It seems to work perfectly.
                                  now, I just have a txt encoding problem but that goes beyond this post

                                  thank you all

                                  1 Reply Last reply Reply Quote 3
                                  14 out of 20
                                  • First post
                                    14/20
                                    Last post
                                  The Community of users of the Notepad++ text editor.
                                  Powered by NodeBB | Contributors