Script Debugging - autokey/autokey GitHub Wiki

AutoKey scripts are Python scripts that use and have access to an AutoKey API. There are two different ways to debug problems in your AutoKey scripts:

Develop your scripts in AutoKey and view the AutoKey error messages

AutoKey offers several ways to examine the error messages generated by your scripts:

  • In AutoKey (Gtk), choose View script error from the Tools menu in the main window of AutoKey.
    • Note that in AutoKey (Gtk) versions prior to 0.95.7, you'll need to open the main window to see the dialog (see #222 for more information).
  • In AutoKey (Qt), choose Show last script error from the Tools menu in the main window of AutoKey.
  • Right-click the AutoKey tray icon and choose View script error from the context menu.
  • Start AutoKey from a terminal window with the verbose option by using either the autokey-gtk --verbose or autokey-qt --verbose command to see your scripting errors logged in the console output.

Develop your scripts externally using Python and then port them to AutoKey

Write your code externally as a Python script such as "myscript.py" and execute it from within any Python interpreter or from the command line with something like the python myscript.py command.

If you'd like to include AutoKey API calls in your code, those won't be accessible from an external script, but you can use unittest.mock.MagicMock() to fake the API objects. It can be a bit complicated if your script also relies on the values returned by AutoKey API functions, but those can be faked by configuring the Mock object to return an expected value.

Below is a basic example of a snippet of code you can put into the beginning of your external scripts during development that don't rely on returned API values:

from unittest.mock import MagicMock
clipboard = MagicMock()
dialog = MagicMock()
keyboard = MagicMock()
mouse = MagicMock()
store = MagicMock()
system = MagicMock()
window = MagicMock()

The script should work without errors outside of AutoKey without accessing the actual API functionality. Once you've got it working smoothly in your external Python script, you can copy it into AutoKey, remove the Mock code, and replace it with the actual API calls.

Note that by default each AutoKey script is stored as a Python file in your home folder, such as ~/.config/autokey/data/My\ Phrases/ in Linux.