Integrating AutoKey, BASH, and Python, including passing arguments to subroutines

AutoKey

https://github.com/autokey/autokey

https://github.com/autokey-py3/autokey

is very useful for automating a host of mundane tasks.

Here is a list of the sorts of tasks for which I use it:

• Sending username and password to web and application challenges

• Making repetitive entries in event logs and websites
(e.g., https://www.outofmilk.com/)

• Triggering an action for and responding to the prompts of a host of         applications before shutting down and after a system reboot

• Switching among and triggering subsets of KDE activities

• Entering repetitive remarks and commands while coding

All of these tasks are of the boringly iterative kind, but have slight differences depending on the circumstances. These little differences can be resolved by constructing input parameters and decision keys, then passing these to a common engine.

The input arguments (e.g., username/password, item name/category, application name/response parameters, etc.) are unique to a particular circumstance, but the algorithm for processing the arguments is essentially the same for each type of interaction, varying with only small differences depending on the input. For example:

Username/password challenge pseudocode:
Input username;<tab>;input password;<tab>;<enter>

OOM item/category pseudocode:
Input item;<tab>x6;<category_character>xn;<enter>

Application response pseudocode:
Bring up the application window;enter response parameters;<enter>

The niceties of the pseudocode are considerably more complex, but doable with the appropriate tools. In particular, AutoKey is very sensitive to timing, so we need a generous supply of time.sleep(0.5) statements. Accomplishing the same task in a Python module or BASH script seems to obviate that sensitivity.

The more useful tools I have found include:

Python, in general:
• Learn the differences between Py2.7 and Py3.x

• PyCharm: https://www.jetbrains.com/pycharm/

if:...elif: case decision struct

• A host of windowing and process modules

- Wnck
- subprocess

autokey-qt (Py3 fork of AutoKey) API, in particular:
engine.run_script

BASH:
• Window tools
wmctrl -a {window title}
xdotool

We have been able to create functions with these such as:

• Implementing a number of xdotool functions:

xdo_find    A function to find the ID of the window whose title contains a string
xdo_get     A function to set the focus to a specified window
xdo_key     A function to send keystrokes to a specified window
xdo_write   A function to type a string

• Implementing a number of Wnck functions:

check_win   A function to determine if a window exists
min_windows A function to minimize all windows

We shrink the input and case parameters to a string in an AutoKey script that then calls another AutoKey script to implement the various BASH and Python routines.

The canonical structure is this:
AutoKey_calling_script:

arg1 = “xxxxx”
arg2 = "xx xxx xx"
...
argn = “zzzzz”
arg_string = arg1 + "," + arg2 + "," + ..., + "," + argn
clipboard.fill_clipboard (arg_string)
engine.run_script(implementing_script)

Autokey_called_script:

arg_string = clipboard.get_clipboard() # This is a string
args_list = arg_string.split(',') # This is now a sequence
arg0 = args_list[0]| # So you can pick and choose from the sequence
arg1 =args_list[1]
... # et cetera...
argn =args_list[n]
# Now go *do* something with the arguments

We then use these to add some fun to the boring parts of our life by making them at least a little bit magical:

• When ready to reboot or shut down I issue one single key sequence (;gcaca). It triggers an array of AutoKey, BASH, and Python code that then gracefully closes all windows and applications, responding to challenges about whether to save, etc., and either shutting down or rebooting the machine.

• When ready to restart I issue one single key sequence (;gcasa). It triggers an array of AutoKey, BASH, and Python code that starts up the network, logs in to various SSH sites, triggers maintenance actions on the remote hosts, starts all Linux activities and their respective applications, responds to password challenges, and does all that while I go start the coffee and wash whatever dishes were left in the sink.

• When done (it takes about five minutes on a very fast machine) I issue one final key sequence (;gcafa). It triggers an array of AutoKey, BASH, and Python code that answers system, application, and internet host challenges and brings up the first task window of the day, my work log page.

Here is an explicit example for use with OOM:

OOM_example

More to follow, In Due Course.

2 thoughts on “Integrating AutoKey, BASH, and Python, including passing arguments to subroutines”

  1. Leave the clipboard alone! Autokey has a feature to pass argument data to scripts:
    Instead of the clipboard to pass arguments, you can use the global store to pass arbitrary data to scripts.
    The builtin »store« object, imported into scripts by default, provides script-local persistence, but also has a GLOBAL attribute and »get_global_value«, »set_global_value« methods, that do what the name suggests.
    The GLOBAL attribute is just a python dict, which is shared across all scripts. You can use any hashable object as a key and put any python object as a value.
    Maybe use »store.set_global_value(“args”, [arg0, arg1, arg2])«?
    A simple example, only using strings as keys and values:
    https://github.com/luziferius/autokey_scripts/tree/master/NumpadIME
    (The linked example implements a cellphone-style keyboard using the keyboard numpad.)

    1. Thanks for the info, sorry for the delay. I’ll pass this on after I’ve digested it… 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *

WordPress spam blocked by CleanTalk.