That's the same code I have.
I have never actually tried to use it (and Thomas is gone.)
Rereading it just now left me a bit confused. I'm not sure how to use it
correctly, but if you want to try, I'll help if I can.
Making an AutoKey script with the code to inject would be cool - after we
know what we're doing. It's doable, but emitting structured text is a PITA
because many apps (kate ...) tend to take liberties with it and Python
gets upset if your indentation isn't correct.
There was a thread about it somewhere recently. I thought I saved it, but
I don't see it now. The basic idea was to place the cursor at the start of
a properly indented line, count how many leading spaces there are and then
prefix each new emitted line with that many spaces.
I guess you can import time, but it cost you time. :)
Joe
Here's the whole note I had saved. Assume that the email system will screw
up the indentation.
Thomas Hess @luziferius 08:15
The API calls are injected into the script’s __globals__dictionary, before
execution starts. Everything in this dictionary can be used like the
normal builtins. If you import modules, those don’t get the API functions
injected automatically, thus can’t use the API. It is a bit unfortunate. I
think, we should override the import builtin somehow, so that it imports
the module and injects the API functionality. In the meanwhile, you can
add an injector function to your module, somewhat like this:
def load_api(api_keyboard, api_mouse, api_store, api_system, api_window,
api_clipboard, api_highlevel, api_dialog, api_engine):
global keyboard, mouse, store, system, window, clipboard, highlevel,
dialog, engine # Define the API class instances as globals
# then put the given instances into the script globals
keyboard = api_keyboard
mouse = api_mouse
store = api_store
system = api_system
window = api_window
clipboard = api_clipboard
highlevel = api_highlevel
dialog = api_dialog
engine = api_engine
Then in your script do this:
import my_module
my_module.load_api(keyboard, mouse, store, system, window, clipboard,
highlevel, dialog, engine)
You can’t use this with script style modules (that have executed code on
the top level that does API calls), only with modules that define
functions/classes for later usage by the importing script.
Thomas Hess @luziferius 08:21
For reference, the injection happens here:
https://github.com/autokey/autokey/blob/78570d02e8c5c57527e861c722056c1d8a73fc4a/lib/autokey/service.py#L439-L453
and
https://github.com/autokey/autokey/blob/78570d02e8c5c57527e861c722056c1d8a73fc4a/lib/autokey/service.py#L460
_
Thomas Hess @luziferius 05:00
You can also try: from autokey import scripting, scripting_highlevel
inside your module and then instantiate the desired API functions
yourself, like done in the first link above. I haven’t tried it, but
autokey is in the global Python module search path, so you can use it as a
library.
Well, I used it as a library to test stuff on the interactive prompt, so
it works that way.
If you get ImportError: cannot import name 'ConfigManager' or similar, you
run into circular dependencies that have to be broken by importing the
module containing the indicated name first. (Even if you don’t use it.)
Thomas Hess @luziferius 05:07
You then side-step the API provision mechanism and are on your own. It
breaks, if the code you are importing is moved during refactoring.
_
> --
> 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.
> To view this discussion on the web visit
>
https://groups.google.com/d/msgid/autokey-users/60ff120f.1c69fb81.98c7b.4397%40mx.google.com.
>