This is another advanced topic!
It's a bit tricky. Since the script is being run by AutoKey, you
probably can't just kill it using its process ID (which you could obtain
and expose) without borking AutoKey. (If sending it a signal is
possible, that would be the most robust approach.) So that leaves the
alternative of asking it politely to stop what it's doing.
That can be done with a dialog as Andy suggests or you can have the
script monitor a parameter/semaphore/lock file and change what it does
when the file changes.
One issue with this is that the script has to get to (one of) the
place(s) where it issues the dialog or checks the file. If it's stuck in
a loop, etc., that might not happen.
Another issue is that the dialog may need to be able to time out so that
the script won't hang unnecessarily if you step away from the screen. If
it times out, it should know whether to continue or quit.
Typically, AutoKey is used to perform simple, fast operations and then
return control to the user, so there's usually no need to stop a working
script manually.
What sort of use cases do you have in mind?
This is compounded by the fact (at least, I'm pretty sure this is how it
works) that AutoKey can't walk and chew gum at the same time. Once you
start a macro, I don't believe that AutoKey will do anything else
besides running that macro - specifically, I don't think it will detect
or run any other phrases or macros until the current macro completes.
I.e. I don't think it is designed to be reentrant/multitasking. This
makes long running macros generally undesirable.
On another note, in all the macros I have written so far, execution just
ends by running off the bottom of the code. I don't use a command like
stop, etc. This works. I wonder whether a stop or similar command will
cause more than just the current macro to stop. I don't know how macros
are invoked in the code. I would have thought something like a return
statement would be the way to go - but I don't know Python very well yet.
Have you tried this?
Joe
On 12/11/2017 10:41 AM, Andy Lavarre wrote:
> This is a response to a necro-post, because I haven't found any other
> correct answer:
>
> /*Is there a way to stop a running script?*/
>
> Well, yes, sort of: you can insert a dialog at strategic points where
> you can reply NO to cause it to *stop* (keyword *stop*, or *sys.exit(0)*)
> **
https://stackoverflow.com/questions/543309/programmatically-stop-execution-of-python-script**
>
> RetCode, response =
> dialog.input_dialog(title="Test",message="Proceed?",default="y")
> if response == "y":
> pass
> else:
> keyboard.send_keys("Quitting")
> stop
> time.sleep(0.5)
> clipboard.fill_clipboard (response)
> time.sleep(0.5)
> keyboard.send_keys("<ctrl>+v")
>
>
> Cheers.
>
> On Friday, June 4, 2010 at 11:06:49 AM UTC-4, Geck0 wrote:
>
> Dear All,
>
> I've written a script which send some mouse click events into a
> window. Some sleep command with more then one seconds had been
> required to wait for the browser to get done. While I was debugging
> this small script I'd to be able to stop the running of it.
> Is there any way to stop/exit from the running script ex. with a
> defined keyboard shortcut?
>
> Thank you in advance.
>
> Best regards,
> Gyorgy
>
> --
> You received this message because you are subscribed to the Google
> Groups "autokey-users" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to
autokey-user...@googlegroups.com
> <mailto:
autokey-user...@googlegroups.com>.
> For more options, visit
https://groups.google.com/d/optout.