Communicate Back and Forth with Running AHK Script #355
-
Is there a way to start a script and communicate with the script at any given moment without ending the script? I understand that you can use stdout in AHK to get the resulting string from The only solution I can think of is having AHK set the clipboard and have Python waiting for clipboard change but this seems very inefficient. Is there a better solution to this? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Sorry for the late response on this. Basically, this is what this library does. It invokes an AHK script as a subprocess and communicates back and forth with it via stdin/stdout. You can poke around the code in this repo and see how it's implemented. When you use this library to add Python functions as hotkey callbacks -- When the AHK hotkey is invoked, it sends messages to Python via stdout, which Python in turn reads and the contents of the message indicates which Python callback function should be called. When you call a method like One limitation of AHK here, however, is that it lacks threading capabilities and blocking actions (like awaiting input from stdin) completely prevent any background messages from being processed in AHK (which are used for things like timers, hotkeys, and more). Because of this, simultaneous bidirectional communication using stdin/stoud is not practical within a single AHK process. So, an implementation detail in this library is that this is worked around with two processes: one for receiving commands from Python (like for method calls like These ~30 lines of AHK code (ignoring the jinja blocks) is essentially what lets AHK receive inputs from Python and return responses. You could reasonably invert this relationship to have AHK call Python functions and receive its results, too. For hotkeys and clipboard callbacks, these lines and these lines of Python are what are responsible for receiving messages sent from AHK. There are a number of other details involved that make this protocol work (like message formats, and so on), but those are the basics. Lastly, this library also provides means for writing extensions to extend the functionality by adding your own AHK code to the daemon that accepts messages from Python. So you can write a bit of AHK code and leverage the communication framework already implemented by this library. Depending on the exact details, this might be a way that you can implement the back-and-forth communication you describe for your use-case. See one simple example of an extension that manipulates state in AHK and later can retrieve that state from Python. This is a really simple example, but may put you in the right direction. I hope that helps answer your question. |
Beta Was this translation helpful? Give feedback.
Sorry for the late response on this. Basically, this is what this library does. It invokes an AHK script as a subprocess and communicates back and forth with it via stdin/stdout. You can poke around the code in this repo and see how it's implemented.
When you use this library to add Python functions as hotkey callbacks -- When the AHK hotkey is invoked, it sends messages to Python via stdout, which Python in turn reads and the contents of the message indicates which Python callback function should be called. When you call a method like
win_get
Python sends a message containing the function parameters to the AHK subprocess via stdin; the AHK script reads stdin to know which function to cal…