1

I have a python script I want ran on a shortcut key, but I want the Msgbox to tell me it's done once it finishes. How do I do that?

I've tried putting the MsgBox, Done in different places like so

F8::Runwait, C:\python36\python.exe  "C:\Users\jason\Google Drive\pycharm\test.py"; 
MsgBox, Done
F9::Run, C:\python36\python.exe  "C:\Users\jason\Google Drive\pycharm\test1.py"; 
MsgBox, Done1

Didn't see any examples of this in the Run or MsgBox section of the docs.

Would like a "Done" after any hotkeys are executed.

jason
  • 3,811
  • 18
  • 92
  • 147

2 Answers2

2

Could you just use RunWait instead of Run so the program waits for the script to finish before it continues? Also you need to use the multi-line hotkey syntax if you want multiple lines to execute. Below is an edited version of your script:

F8::
    RunWait, C:\python36\python.exe  "C:\Users\jason\Google Drive\pycharm\test.py"; 
    MsgBox, Done
    Return

F9::
    RunWait, C:\python36\python.exe  "C:\Users\jason\Google Drive\pycharm\test1.py"; 
    MsgBox, Done1
    Return

Please note that if your python script starts another process, your AutoHotkey script will not wait on that second process.

Charlie Armstrong
  • 2,332
  • 3
  • 13
  • 25
  • It works for me, but you also seem to have your formatting a little bit off. You have the `RunWait` command on the same line as the `F8::` hotkey, so AutoHotkey only runs that one command. In this case you want to execute multiple lines, so you need to put everything beneath the hotkey. I have updated my answer to demonstrate this. – Charlie Armstrong Jun 05 '20 at 19:10
  • 1
    It is marked "accepted answer" so that's fine, but note there are some situations were executing a script under `RunWait` will return before the script completes - especially if the script calls other functions, scripts, or interacts with other programs. So the real answer to knowing when a script is done would involve something more, such as writing to a file and the AHK script checking it, or sending a result to StdOut or StdErr and AHK awaiting that result. Or even putting something into the Clipboard from py and AHK waiting for the clipboard change event. But does OP any of need this? – PGilm Jun 05 '20 at 21:38
  • 1
    It depends on how you define "completed". `RunWait` waits for the process it started to die. I believe what you are trying to say is that the process could start other processes, and those may not have finished when the first process finishes. +1 for the valid concern, I will add it to my answer. – Charlie Armstrong Jun 05 '20 at 22:37
1

To have more than one command executed by a hotkey, put the first line beneath the hotkey definition and make the last line a return:

F8::
Runwait, C:\python36\python.exe  "C:\Users\jason\Google Drive\pycharm\test.py"; 
MsgBox, Done
return

https://www.autohotkey.com/docs/Hotkeys.htm#Intro

user3419297
  • 9,537
  • 2
  • 15
  • 24