Search code examples
pythonlinuxdebianautokey

Linux - Terminating AutoKey script using keybindings


Okay so I am new with AutoKey application on my elementaryOS device and I am just playing around with some custom scripts.

What I did find strange is that there is no simple option to terminate the running script.

So, is there any nice and simple method to achieve this.

Pardon the incompetency. ._.


Solution

  • Currently, there is no such method.

    Autokey uses a simple mechanism to run scripts concurrently: Each script is executed inside a separate Python thread. It uses this wrapper to run scripts using the ScriptRunner class. There are some methods to kill arbitrary, running Python threads, but those methods are neither nice nor simple. You can find answers for the generic case of this question on Is there any way to kill a Thread?

    There is one nice possibility, but it is not really simple and it needs to be supported by your scripts. You can send a stop signal to scripts using the global script store.

    Suppose, this is the script you want to interrupt.

    import time
    
    def crunch():
        time.sleep(0.01)
    
    def processor():
        for number in range(100_000_000):
            crunch(number)
    
    processor()
    

    Bind a stop script like this to a hotkey

    store.set_global_value("STOP", True)
    

    Modify your script to check that value.

    import time
    
    def crunch():
        time.sleep(0.01)
    
    def processor():
        for number in range(100_000_000):
            crunch(number)
            if store.GLOBALS.get("STOP", False):
                store.set_global_value("STOP", False)
                break
    
    processor()
    

    You should add that code to each hot or long running code path. This won't help if something deadlocks in your script.