Stop running script

9,182 views
Skip to first unread message

Geck0

unread,
Jun 4, 2010, 11:06:49 AM6/4/10
to autokey-users
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

Christiaan Dekter

unread,
Jun 6, 2010, 4:10:41 AM6/6/10
to autoke...@googlegroups.com
At the moment there is no way to stop a running script, other than to restart AutoKey completely.

Ivan Garcia

unread,
Jul 26, 2013, 9:33:09 PM7/26/13
to autoke...@googlegroups.com


El domingo, 6 de junio de 2010 10:10:41 UTC+2, Chris D escribió:
At the moment there is no way to stop a running script, other than to restart AutoKey completely.


Has it been any improvement about this after 3 years? I'm using 0.90.4 and still my script gets unstoppable with a while loop.

thanks
Ivan

Joe

unread,
Jul 28, 2013, 2:02:14 PM7/28/13
to autoke...@googlegroups.com, Ivan Garcia
We're looking for developers to work on AutoKey. Chris, the developer,
"retired" from the project around a year ago. Since then, there has been
some interest, but it's going slow.

Joe

On 07/26/2013 09:33 PM, Ivan Garcia wrote:
>
>
> El domingo, 6 de junio de 2010 10:10:41 UTC+2, Chris D escribi�:
> --
> 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.
> For more options, visit https://groups.google.com/groups/opt_out.
>
>

Ivan Garcia

unread,
Jul 29, 2013, 7:02:05 PM7/29/13
to autoke...@googlegroups.com
On Sun, Jul 28, 2013 at 8:02 PM, Joe <jos...@main.nc.us> wrote:
We're looking for developers to work on AutoKey. Chris, the developer,
"retired" from the project around a year ago. Since then, there has been
some interest, but it's going slow.

Joe


That's insteresting, I'm myself a pyQT developer.(see https://launchpad.net/subdownloader ). Who else is interested in participating? Can we have a meeting/chat to decide what is the most important tasks?

Best Regards.
Ivan

Andy Lavarre

unread,
Dec 11, 2017, 10:41:27 AM12/11/17
to autokey-users
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))

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.

Joe

unread,
Dec 12, 2017, 3:08:41 AM12/12/17
to autoke...@googlegroups.com, Andy Lavarre
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.

C. Andrews Lavarre

unread,
Dec 12, 2017, 6:04:51 AM12/12/17
to Joe, autoke...@googlegroups.com
On Tue, 2017-12-12 at 03:08 -0500, Joe wrote:
Have you tried this?

Much much more to explore In Due Course, thanks. 

Searching for a PID by keyword is a good idea. Where I'm going right now is using Wnck (Python) to see if an expected window exists then xdotool (BASH) to set its focus then do things with it... 

I did try return, quit, exit before arriving at stop and sys.exit(). None of them worked, but may with additional () or other niceties... "Just do it" takes time... :-) IDC.

Cheers, Andy

Ivan Garcia

unread,
Dec 22, 2017, 6:53:39 AM12/22/17
to autoke...@googlegroups.com
Hi, well thanks for the input_dialog and the stop functions suggestions.

I also found out that there is a fork of autokey supporting python3 and with many new features, like detecting images or portions in the screen, which is very useful in my opinion for autoscripting when only mouse clicks is possible.

I have created an script to automatically scan book pages by using the scangear tool of Canon.

import time

#Variables to change
is_color = False
color_choose = True

page = 3
last_page = 155

#Code
total_scan_times = int((last_page - page) / 2) + 1

time.sleep(1) 

if color_choose:
     COLOR_CHOSE_WAIT = 3
else:
     COLOR_CHOSE_WAIT = 0
     
if is_color:
    SECONDS_WAIT = 15 + COLOR_CHOSE_WAIT
else:
    SECONDS_WAIT = 11

window.activate('Canon')
time.sleep(1)
for i in range(total_scan_times): 
    page_string = "{0:03d}".format(page) #to show 3 figures with leading zeros 00X or 0XX
    RetCode, response = dialog.input_dialog(title="Continue scanning",
                                            message="Enter the page number to save the file...".format(page_string),
                                            default="{0}".format(page_string))
    if response == "":
        stop
    
    page = int(response)
    page_string = "{0:03d}".format(page) #to show 3 figures with leading zeros 00X or 0XX
    time.sleep(0.5)
    mouse.click_absolute(1250, 670, 1) # Save Scan Button. Get with xdotool getmouselocation
    time.sleep(0.5)
    keyboard.send_keys("<alt>+n")
    time.sleep(0.5)
    
    keyboard.send_keys(page_string)
    time.sleep(0.25)
    keyboard.send_keys("<alt>+p")
    time.sleep(0.25)
    keyboard.send_key("<tab>")
    time.sleep(0.25)
    keyboard.send_key("<tab>")
    time.sleep(0.25)
    keyboard.send_key("<tab>")
    time.sleep(0.25)
    keyboard.send_key("<enter>")
    time.sleep(SECONDS_WAIT)
    page += 2


--
You received this message because you are subscribed to a topic in the Google Groups "autokey-users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/autokey-users/AGjyshzqdYY/unsubscribe.
To unsubscribe from this group and all its topics, send an email to autokey-users+unsubscribe@googlegroups.com.

Joe

unread,
Dec 22, 2017, 6:50:59 PM12/22/17
to autoke...@googlegroups.com, Ivan Garcia
Welcome back to the list. It's been awhile (2013).

Thanks for the macro.

A few comments:

Although the previous versions of AutoKey are still available and
supported on this list, the only version with a developer is the -py3
version you found.
We encourage all users to upgrade to that version. Aside from the
Xautomation support which you note, there are many bug fixes and greatly
enhanced support for multibyte character sets.

I took a look at he macro itself (I hope you don't mind.):

The first time.sleep() happens before you emit anything. Is there a
reason for that?

What does dialog.input_dialog() do if you enter something non-numeric? I
haven't used it with formatting, so I don't know.  (I use yad for
everything, so I just call it from within a macro if I need a dialog.)

There are a number of hard-coded constants in your macro. While this is
fine, it would help to have some comments in the code explaining them.
The initial and ending page numbers could also be entered via a dialog.
(If you do and you're going to be using this a lot, I would also suggest
adding a bit of input validation (that both are positive and that
last_page >= page.)

It seems odd to need delays when you're just tabbing from field to
field. Does the application do more than just select the next field when
you tab?

total_scan_times = int((last_page - page) / 2) + 1

looks like you're scanning two pages (of an open book) at a time.

You ask for the page number in every iteration. If the pages are
contiguous, you could just increment the page count (by 2?) .
It looks like you're trying to do that with
page += 2,
but that appears to be overwritten by the next input request at the top
of the loop.
If they're not contiguous, then your calculation of total_scan_times
doesn't make sense to me.

What am I missing?

One general note: Whenever you run a macro which can run for some time,
it  ties up AutoKey, so you should make sure that it eventually ends (as
your code does).

If you leave it in the middle to do something else, AutoKey won't detect
any triggering events in that new activity.

Joe
> I did try *return*, *quit*, *exit* before arriving at *stop* and
> *sys.exit(). *None of them worked, but may with additional *() *or
> other niceties... "Just do it" takes time... :-) IDC.
>
> Cheers, Andy
> --
> You received this message because you are subscribed to a topic in
> the Google Groups "autokey-users" group.
> To unsubscribe from this topic, visit
> https://groups.google.com/d/topic/autokey-users/AGjyshzqdYY/unsubscribe
> <https://groups.google.com/d/topic/autokey-users/AGjyshzqdYY/unsubscribe>.
> To unsubscribe from this group and all its topics, send an email
> to autokey-user...@googlegroups.com
> <mailto:autokey-user...@googlegroups.com>.
> For more options, visit https://groups.google.com/d/optout
> <https://groups.google.com/d/optout>.
>
>
> --
> 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>.

Joe

unread,
Dec 22, 2017, 7:12:55 PM12/22/17
to autoke...@googlegroups.com, Ivan Garcia
I just looked at my post below. The part:

(I hope you don't mind.):

is not meant to end in any sort of emoticon. It's just a parenthetical
phrase followed by a colon. :)
Message has been deleted

Joe

unread,
Mar 30, 2018, 3:47:44 PM3/30/18
to autoke...@googlegroups.com
Sorry if this is a repost. I sent it 12 hours ago and haven't seen it on
the list.

Interesting. Can you provide a bit more context on how this is used?

Do you put it inside a loop and use it to break out?

Since it looks like it waits to time out, would you use it instead of a
time.sleep()?

If the mouse click happens before this function is called, will it
detect it somehow or do you have to click at just the right time?

Thanks.

Joe


On 03/25/2018 08:41 PM, tim cotter wrote:
> i needed to be able to stop a script.
> i chose to make it stop when i clicked the mouse.
> mouse.wait_for_click() should return True if a click happened and
> False if it timed out.
> this version does.
>
> import autokey.iomediator
>
> def my_wait_for_click(button, timeout):
>     w = iomediator.Waiter(None, None, button, timeout)
>     w.wait()
>     ret = w.event.is_set()
>     return ret
>
> enjoy.
> >> <alav...@gmail.com <javascript:> <mailto:alav...@gmail.com
> >>     to autokey-user...@googlegroups.com <javascript:>
> >>     <mailto:autokey-user...@googlegroups.com
> <javascript:>>.
> >>     For more options, visit https://groups.google.com/d/optout
> <https://groups.google.com/d/optout>
> >>     <https://groups.google.com/d/optout
> <https://groups.google.com/d/optout>>.
> >>
> >>
> >> --
> >> 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 <javascript:>
> >> <mailto:autokey-user...@googlegroups.com <javascript:>>.

Joe

unread,
Mar 30, 2018, 3:47:44 PM3/30/18
to autoke...@googlegroups.com
Interesting. Can you provide a bit more context on how this is used?

Do you put it inside a loop and use it to break out?

Since it looks like it waits to time out, would you use it instead of a
time.sleep()?

If the mouse click happens before this function is called, will it
detect it somehow or do you have to click at just the right time?

Thanks.

Joe


On 03/25/2018 08:41 PM, tim cotter wrote:
> i needed to be able to stop a script.
> i chose to make it stop when i clicked the mouse.
> mouse.wait_for_click() should return True if a click happened and
> False if it timed out.
> this version does.
>
> import autokey.iomediator
>
> def my_wait_for_click(button, timeout):
>     w = iomediator.Waiter(None, None, button, timeout)
>     w.wait()
>     ret = w.event.is_set()
>     return ret
>
> enjoy.
>
> On Friday, December 22, 2017 at 4:12:55 PM UTC-8, Joe wrote:
>
> >> <alav...@gmail.com <javascript:> <mailto:alav...@gmail.com
> >>     to autokey-user...@googlegroups.com <javascript:>
> >>     <mailto:autokey-user...@googlegroups.com
> <javascript:>>.
> >>     For more options, visit https://groups.google.com/d/optout
> <https://groups.google.com/d/optout>
> >>     <https://groups.google.com/d/optout
> <https://groups.google.com/d/optout>>.
> >>
> >>
> >> --
> >> 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 <javascript:>
> >> <mailto:autokey-user...@googlegroups.com <javascript:>>.
Reply all
Reply to author
Forward
0 new messages