Importing existing AutoKey scripts into new AutoKey scripts

1.185 weergaven
Naar het eerste ongelezen bericht

Timothy Myers

ongelezen,
28 jul 2015, 12:46:5028-07-2015
aan autokey-users
I would like to create a system in scripts in Autokey that will allow me to minimize code duplication.
For example, I would like to have the following:

In fine GeneralSetup.py

def enableDevice(...):
    pass


Then in another file called SpecificSetup.py:

from GeneralSetup import enableDevice

#specific things
enableDevice(string, bool)


Does AutoKey allow me to do this, or am I forced to duplicate home written library functionality?

Thanks!

Joe

ongelezen,
28 jul 2015, 16:36:3728-07-2015
aan autoke...@googlegroups.com
Welcome to the list.

I don't know enough Python to be sure I understand your question, but I
think this may be the answer.

If you open the AutoKey main window and select Edit->Preferences->Script
Engine, it allows you to define a user modules folder. Any modules
placed in that folder can be directly imported into AutoKey macros.

I think that will do what you want. I defined the folder, but haven't
tried to use it.

See if it works for you and post back here so we can learn from your
experience - or help you to get it to work if it doesn't.

I haven't gotten into defining Python modules yet, but I think it's easy
to define a module with several "parts" in it and then access them as
you have outlined.

Joe
> --
> 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.

Timothy Myers

ongelezen,
28 jul 2015, 17:54:5628-07-2015
aan autokey-users, jos...@main.nc.us
Thank you very much. I believe that worked, although now I am running into another issue that I will post about separately.

I was able to load that module into my AutoKey script but then the python script had an error.
I think that this may have been due to the fact that the procedure that I imported attempted to write things out through the keyboard using keyboard.send_keys, is this not allowed?
I would expect that they would be if the entirety of the scripts are being run in the AutoKey engine, but I do not know how you are adding your Autokey namespaces to the scripts that are written.

Thanks again.
Tim

Joe

ongelezen,
28 jul 2015, 21:19:1128-07-2015
aan autoke...@googlegroups.com
You're welcome.

I think I know what a namespace is (another name for scope of
variables), but I have no idea how they work in Python.

When you run into a problem like this and staring at it intently for a
suitable interval of time doesn't provide a solution, there are two ways
to proceed which I use.

1) If I can run AutoKey and the script from within a terminal (in the
foreground instead of detached in the background as usual), I may just
add some print statements to my script which will send text to the
terminal telling me where I am in the script and what the values of key
variables are - in other words, standard debugging.

I used this to find a nasty little bug in AutoKey - maybe still in
0.90.4, but fixed in git and in 0.90.4-1 (which is only available in the
14.04 PPA for Ubuntu). It let you accidentally add a null trigger phrase
for a phrase or macro. Then, as soon as anything made AutoKey look for a
trigger phrase, it would crash. I lost the use of AutoKey for about 8
months over that one and nobody could duplicate my problem because we
hadn't identified it.

When I finally got the idea, I just looked at the normal backtrace and
went into the affected modules and added a few print statements to see
what values were getting passed to the unhappy function and the whole
thing was solved almost immediately. Made me feel pretty stupid and
happy at the same time.

Of course, that assumes that you got a backtrace or, in the case of a
bug in your own macro, you just know where to look for trouble.

When anything gui (not just AutoKey) messes up inexplicably, the first
thing to try (if possible) is to start it from a terminal so that any
messages it might be sending to stdout or stderr have some place to go
where you can see them.

2) The other method, which is probably the place to start in your case,
is to run an AutoKey trace. See instruction boilerplate below.

Joe

----

Standard reply to all such questions:

FAQ #1: When AutoKey does something unfortunate, document your problem
by specifying:
exactly what you did (including your macro source if possible and relevant)
what it did,
what you expected it to do,
*and*
providing us with a trace log and a description of:

your OS/version
desktop manager/version
AutoKey version

and anything else you think might be relevant.

Quit AutoKey if it is currently running.

Start AutoKey from a terminal window.
Then, do the minimum required to recreate your issue and then stop AutoKey.

autokey-gtk -l
or
autokey-qt -l

depending on which version of autokey you have installed. That's a lower
case "ell".

This will produce a log in the terminal window of what AutoKey is doing.

It will probably show you what is wrong.

If it doesn't make sense or you have further questions, copy and paste
the log output from the terminal window into a post to this list and
we'll take a look at it.

Joe
> > an email to autokey-user...@googlegroups.com <javascript:>
> > <mailto:autokey-user...@googlegroups.com <javascript:>>.

Timothy Myers

ongelezen,
29 jul 2015, 09:00:1729-07-2015
aan autokey-users, jos...@main.nc.us
I appreciate your time on these.

I have some output here that was generated using the autokey -l option on the  command line, I will highlight the relevant part for this thread:
Traceback (most recent call last):
  File "/usr/local/lib/python2.7/dist-packages/autokey/service.py", line 454, in execute
    exec script.code in scope
  File "<string>", line 40, in <module>
  File "/home/tmmyers/tftp-root/AutoKey/Work/GeneralProvision.py", line 3, in enableT1
    keyboard.send_keys("int t1 "+ssp+"<enter>")
NameError: global name 'keyboard' is not defined

2015-07-29 07:38:18,338 DEBUG - interface - Sending string: u'configure terminal'
2015-07-29 07:38:18,344 DEBUG - interface - Send special key: [u'<enter>']
Exception in thread Phrase-thread:
Traceback (most recent call last):
  File "/usr/lib/python2.7/threading.py", line 810, in __bootstrap_inner
    self.run()
  File "/usr/lib/python2.7/threading.py", line 763, in run
    self.__target(*self.__args, **self.__kwargs)
  File "/usr/local/lib/python2.7/dist-packages/autokey/service.py", line 464, in execute
    self.app.notify_error(_("The script '%s' encountered an error") % script.description)
  File "/usr/local/lib/python2.7/dist-packages/autokey/gtkapp.py", line 230, in notify_error
    self.notifier.notify_error(message)
  File "/usr/local/lib/python2.7/dist-packages/autokey/gtkui/notifier.py", line 145, in notify_error
    self.show_notify(message, Gtk.STOCK_DIALOG_ERROR)
  File "/usr/local/lib/python2.7/dist-packages/autokey/gtkui/notifier.py", line 160, in show_notify
    n.attach_to_status_icon(self.icon)
AttributeError: 'Notification' object has no attribute 'attach_to_status_icon'


This looks like by default you cannot use the AutoKey API in python files that you import.
After looking at the autokey service.py file that is installed on my system, I find this a little strange, but I have never
used the execute keyword in any of my python work. I will need to research how execute behaves when given a scope as
Autokey does it.

Andy Lavarre

ongelezen,
25 nov 2016, 17:29:4125-11-2016
aan autokey-users
Joe hi, below is an old post, but still not answered and I have the same need: 

     Code reuse. 

The solution you suggested would be great, but it doesn't work. Details below. Please resurrect the thread to see if anyone has sorted it out in the last year and a half.

Thanks again for all, Happy Thanksgiving.

Cheers, Andy

=====

My AutoKey is 0.91.1, openSUSE Leap 42.1.

I wish to engage in code reuse by calling other scripts from a particular script.

I understand there is a concept (but I cannot find an implementation) of the "USER MODULES" folder.

Where and how might we define the User Module folder??

And then, given that we have established such and written Python modules therein, how do we import them and call them?

Do we do that within each script? 

-----

It would be good to do something like:

import /usr/lib/python2.7/site-packages/autokey/usermodules/keyboard.py
keyboard.sendbackspaces
in order to achieve:
#Delete keystrokes
keyboard.send_keys("<backspace>")
keyboard.send_keys("<backspace>")
keyboard.send_keys("<backspace>")
keyboard.send_keys("<backspace>")
keyboard.send_keys("<backspace>")
keyboard.send_keys("<backspace>")
keyboard.send_keys("<backspace>")

Etc.

-----

I followed the idea below. The paths have changed, but at present both
     Settings > Configure AutoKey > Script Engine
and the underlying file
    /usr/lib/python2.7/site-packages/autokey/qtui/enginesettings.py
        offer
     
But clicking on Browse has no effect.

-----

FWIW (For what it's worth), there is a list of a bunch of API calls listed in
     /usr/lib/python2.7/site-packages/autokey/qtui/data/api.txt

Additionally, while some references to the API have disappeared, I have refound it at

Additionally, I am crawling rather delightfully through PyCharm, having already done the useful Python tutorial at

But, regardless, I haven't found the answer yet.

==========

Joe

ongelezen,
25 nov 2016, 19:37:2325-11-2016
aan autoke...@googlegroups.com
OK. Hopefully, this will help.

I have a folder named Modules (it can be any name) in ~/autokey_scripts.
(I keep all my macros in an external directory so they are unaffected by
any changes made to AutoKey itself - like reinstalling or purging.)

I told AutoKey about it in Settings->Configure AutoKey->Script Engine.

In that folder, I placed a file called typing_aids which contains a
function (not a macro) called type_slow.

def type_slow(string, delay=0.1):
...

I wrote a test macro in AutoKey

import typing_aids

typing_aids.type_slow("abc", 0.1)

This runs the function.

That's the good news.

The bad news is that the way AutoKey is implemented, functions in the
modules directory do not have access to the AutoKey API.

This means that you can't do things like keyboard.send_keys() in a
function in a module.

(Since that is what I was trying to do with my function, it bombs, but
it does show me that I was able to get it to run.)

So if you have functions that don't need to use the API, it works great.

You can abbreviate this process slightly by

import typing_aids as ta

ta.type_slow("abc", 0.1)


Joe


On 11/25/2016 05:29 PM, Andy Lavarre wrote:
> Joe hi, below is an old post, but still not answered and I have the
> same need:
>
> Code reuse.
>
> The solution you suggested would be great, but it doesn't work.
> Details below. Please resurrect the thread to see if anyone has sorted
> it out in the last year and a half.
>
> Thanks again for all, Happy Thanksgiving.
>
> Cheers, Andy
>
> =====
>
> My AutoKey is 0.91.1, openSUSE Leap 42.1.
>
> I wish to engage in code reuse by calling other scripts from a
> particular script.
>
> I understand there is a concept (but I cannot find an implementation)
> of the "USER MODULES" folder.
>
> Where and how might we define the User Module folder??
>
> And then, given that we have established such and written Python
> modules therein, how do we *import* them and call them?
>
> Do we do that within each script?
>
> -----
>
> It would be good to do something like:
>
> *import
> */usr/lib/python2.7/site-packages/autokey/usermodules/*keyboard.py*
>
> *keyboard.sendbackspaces*
>
> in order to achieve:
>
> #Delete keystrokes
>
> keyboard.send_keys("<backspace>")
>
> keyboard.send_keys("<backspace>")
>
> keyboard.send_keys("<backspace>")
>
> keyboard.send_keys("<backspace>")
>
> keyboard.send_keys("<backspace>")
>
> keyboard.send_keys("<backspace>")
>
> keyboard.send_keys("<backspace>")
>
>
> Etc.
> *
> *
> -----
>
> I followed the idea below. The paths have changed, but at present both
> *Settings > Configure AutoKey > Script Engine*
> and the underlying file
> /usr/lib/python2.7/site-packages/autokey/qtui/enginesettings.py
> offer
>
> **<https://lh3.googleusercontent.com/-IRGQsi2rqno/WDi4BkAXwoI/AAAAAAAABQk/O16DQo15OUISG_PtoOOy7-RTjxvgfB39ACLcB/s1600/Screenshot_20161125_171413.png>
>
> But clicking on *Browse* has no effect.

Joe

ongelezen,
25 nov 2016, 19:57:2725-11-2016
aan autoke...@googlegroups.com
As an after thought:

I imagine it would work perfectly fine if your "Modules" directory
contained hard links or symlinks to modules stored elsewhere on your
system so you wouldn't have to duplicate them there.

Joe

Andy Lavarre

ongelezen,
26 nov 2016, 13:24:3426-11-2016
aan autokey-users

Joe hi. Thank you.

>I told AutoKey about it in Settings->Configure AutoKey->Script Engine. 
 
That is my immediate problem: 

 *Settings > Configure AutoKey > Script Engine*  offers to remember the User Module folder, but clicking on *Browse* has no effect in my 0.91.1 installation. The Browse button gets highlighted and shows itself being pressed (moves slightly) but no file dialog is raised so there is no way to assign a folder as User Module folder. dmesg does not show any errors.

/usr/lib/python2.7/site-packages/autokey/qtui/enginesettings.py seems to be the script called by t*Settings > Configure AutoKey > Script Engine. It defines a function retranslateUi

     def retranslateUi(self, Form):

          This function creates the dialog with the Browse button. This file also defines another function setupUi that calls retranslateUi:

          self.retranslateUi(Form)

     It then calls QtCore and ends:

          QtCore.QMetaObject.connectSlotsByName(Form)
 
I can go down the rabbit hole of tracking down QtCore objects and methods to see where te process breaks but if someone has a shorter answer of why the Browse button is broken that would be nice.

Once I get past this glitch then I can take on the rest.

-----
>import typing_aids 
>typing_aids.type_slow("abc", 0.1) 
>This runs the function. 

Good, thank you. So I can import and call a function within an individual script.py

-----
>The bad news is that ..

Well, that makes sense... You'd have to import AutoKey scripts into Python to have them run in an imported Python module... :-) A circle of imports...

-----
>I imagine it would work perfectly fine if your "Modules" directory 
>contained hard links or symlinks to modules stored elsewhere

Yes, indeed. I also make extensive use of soft links to keep data unduplicated and "safe", (along with frequent backups!) :-)

-----
One step at a time.

Thanks again, Andy

Joe

ongelezen,
26 nov 2016, 18:19:0126-11-2016
aan autoke...@googlegroups.com
Inlne.

We'd love it if you'd like to become an AutoKey developer. No need to
take on any more than you want to.

Right now, we need to create a testing version on GitHub and get a
routine going for installing patches to it.
I already have one submitted patch to apply for testing.

Going forward, you can do as much or as little as you like.

What I'd personally like is someone who could help answer questions on
this list where the answers involve knowledge of the code.

Examples of things I'd like to see done: (most of which do not involve
heavy coding)

There is currently an issue in Ubuntu 16.04 where phrases get their case
shifted - my email phrase emits a 2 instead of an @ - but only in some
applications. We have no idea who or what is causing this. Works fine in
Thunderbird, but not in kate or konsole.

We'd also like to know what, if anything, would be lost if we just
switch to autokey-py3. Would we be abandoning lots of users, etc..

What will have to be done to get our new version (if we create one) into
the distros which currently offer it. Or if we go to autokey-py3, what
has to be done so those distros get the memo.

I've taken a brief look at autokey-py3 and I didn't see documentation or
a support list like this one.

If we do switch over, we'll need to update our documentation. There are
two or three of us willing to do some of the writing, but we will need
someone who can look into the code and make sure what we are writing
about it is accurate.

Joe

On 11/26/2016 01:24 PM, Andy Lavarre wrote:
>
>
> Joe hi. Thank you./
> /
>
> />I told AutoKey about it in Settings->Configure AutoKey->Script Engine./
> That is my immediate problem:
>
> **Setti**ngs > Configure AutoKey > Script Engine** offers to
> remember the User Module folder, but clicking on **Browse** has no
> effect in my 0.91.1 installation. The Browse button gets highlighted
> and shows itself being pressed (moves slightly) but no file dialog is
> raised so there is no way to assign a folder as User Module folder.
> *dmesg* does not show any errors.
Confirmed this problem.

Workaround:

Shut down autokey and directly edit (with a text editor - not a word
processor)

~/.config/autokey/autokey.json

Find a line that looks like

"userCodeDir": "",

and put your directory path in manually.

"userCodeDir": "/home/bigbird/autokey_scripts/Modules",

>
> /usr/lib/python2.7/site-packages/autokey/qtui/*enginesettings.py*
> seems to be the script called by t**Setti**ngs > Configure AutoKey >
> Script Engine** . It defines a function retranslateUi
>
> def retranslateUi(self, Form):
> *
> *
> **This function creates the dialog with the Browse button. This file
> also defines another function setupUi that calls retranslateUi:
>
> self.retranslateUi(Form)
>
> It then calls *QtCore* and ends:
> QtCore.QMetaObject.connectSlotsByName(Form)
> **
> I can go down the rabbit hole of tracking down*QtCore* objects and
> methods to see where te process breaks but if someone has a shorter
> answer of why the *Browse* button is broken that would be nice.
>
> Once I get past this glitch then I can take on the rest.
No idea, what's causing this. I didn't notice it until you just pointed
it out. AFAIK, it wasn't a problem in 12.04.

With anything like this, I might be tempted to ask the autokey-py3
developers if they have encountered it, but first, we have to decide if
we should keep working on this version or just switch to theirs.
>
> -----
> >import typing_aids
> >typing_aids.type_slow("abc", 0.1)
> >This runs the function.
>
> Good, thank you. So I can import and call a function within an
> individual script.py
>
> -----
> >The bad news is that ..
>
> Well, that makes sense... You'd have to import AutoKey scripts into
> Python to have them run in an imported Python module... :-) A circle
> of imports...
I thought it might be something like that.
>
> -----
> >I imagine it would work perfectly fine if your "Modules" directory
> >contained hard links or symlinks to modules stored elsewhere
>
> Yes, indeed. I also make extensive use of soft links to keep data
> unduplicated and "safe", (along with frequent backups!) :-)
>
> -----
> One step at a time.
>
> Thanks again, Andy

Andy Lavarre

ongelezen,
26 nov 2016, 22:20:2626-11-2016
aan autokey-users
  Joe hi, thanks for the prompt reply.

All good, one step at a time...


>Inlne. 

>We'd love it if you'd like to become an AutoKey developer. No need to 
>take on any more than you want to. 

I don't think you're talking to me, as I'm just getting started in Python, but am enjoying it so far. :-)

But once I have up and running I may take you up. The program is awesome and makes my life so much better in so many ways...

Hey! It's inspired me to start learning Python... Can't get much better than that. :-)

Your list of goals is good.


>There is currently an issue in Ubuntu 16.04 where phrases get their case shifted
We have discussed this earlier. The same occurs under openSUSE, so probably is not an OS issue. Basically,
     clipboard.fill_clipboard() works correctly but keyboard.send_keys() does not. 


> We'd also like to know what, if anything, would be lost if we just 
> switch to autokey-py3. Would we be abandoning lots of users, etc.. 
     Good question. My sense is yes, move on to Python 3, encourage others to keep up. As I understand (and have relearned today) PY3 is mostly backward compatible with PY2.7, but is more discipline-demanding (space discipline being one) and offers new skills...


> Or if we go to autokey-py3, what has to be done so those distros get the memo. 
    No clue. 'Way off my scope... :-(


> I've taken a brief look at autokey-py3 and I didn't see documentation or 
> a support list like this one. 
    Correct, which just argues more for assimilation. You have a rich history here, it should be propagated forward for the PY3 folks. There is a host of citations of the “AutoKey API” but most give a 404.
     We need a central repository. The best I've found to date is
        https://rawgit.com/autokey/autokey/master/doc/scripting/index.html


> If we do switch over, we'll need to update our documentation. There are 
> two or three of us willing to do some of the writing, but we will need 
> someone who can look into the code and make sure what we are writing 
> about it is accurate. 
    Good points for planning, but not in my present abilities


>>>I told AutoKey about it in Settings->Configure AutoKey->Script Engine./ 
>> That is my immediate problem: 
>Confirmed this problem. 
    Thank you for the credibility check.

>Workaround: 


>~/.config/autokey/autokey.json 
>Find a line that looks like 
>    "userCodeDir": "", 
>and put your directory path in manually. 
    Thank you, that works:
        "userCodeDir": "/data/develop/macros/autokey/user_modules",
    I'll work with this tomorrow and let you know.

>> Once I get past this glitch then I can take on the rest. 
>No idea, what's causing this. I didn't notice it until you just pointed 
>it out. AFAIK, it wasn't a problem in 12.04. 
    Thanks for the affirmation. The workaround above obviates it, but 'twould be nice to understand.


>With anything like this, I might be tempted to ask the autokey-py3 
>developers if they have encountered it, but first, we have to decide if 
>we should keep working on this version or just switch to theirs. 
    Recommend switch. There is no morality involved and they have the knowledgeable people... :-)


>> Well, that makes sense... You'd have to import AutoKey scripts into 
>> Python to have them run in an imported Python module... :-) A circle 
>> of imports... 
>I thought it might be something like that. 
:-)


One step at a time.

Thanks for the workaround and all.

Cheers, Andy
Bericht is verwijderd

jos...@main.nc.us

ongelezen,
20 mrt 2022, 18:36:4120-03-2022
aan autoke...@googlegroups.com
I wrote a function like that quite a while ago. There's a version of it
in our wiki. But ...

First, I'm not sure why it's not being found if you defined your modules
directory and put it there. I'm not sure about the difference between
Python modules and the functions they contain. I'm going to ask about that
on Gitter.

However, even if you get that working, your function won't work as is
anyway because AutoKey doesn't expose the API to functions invoked from
the Modules directory. There is an enhancement issue filed to fix this,
but it's been there a long time.
https://github.com/autokey/autokey/issues/248

So, you either have to paste a new copy of your function into every script
that needs it or there's a moderately ugly workaround that you can add
that hooks your script up to the API. It's detailed in the above issue.

Found a small bug in your function - see below.

Also, FYI: keyboard.send_keys() can't deal with multibyte characters, so,
if your text contains anything other than EN-US characters, it won't work
correctly.

Joe

> I try to call an AutoKey Script from an other one.
>
> Backgrond is simple:
> AutoKey sometimes sends keys too quick and the speed has to be slowed
> down.
> the slowing down speed script will be called by many scripts e. g. filling
> in name, address, phone number, etc.
>
> I set the Script Engine in Preferences to the path to my AutoKey sripts
> (checkt many times):
> /home/albrecht/_Data_AD/_NotPacked/AutoKey_Files/My_Scripts/
>
> My Scripts in the above mentioned folder are:
>
> - File "my_MainPGM.py"
> # Enter script code
> import my_SubPGM
> SendAtSlowSpeed("Test Text")
>
> - File "my_SubPGM.py"
> def SendAtSlowSpeed(Text2Send):
> #send each char with delay of
> myDelay =0.1
> for EachSign in Text2Send:
> keyboard.send_keys(Text2Send)
keyboard.send_keys(EachSign) ## Fix this
> time.sleep(myDelay)
>
> return
>
> The executing the main script scrip causes the error
> ModuleNotFoundError: No module named 'my_SubPGM'
>
> What am I doing wrong?
>
> Thanks 4 ur help in advance! A L
> --
> 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/8752cdf3-c337-434d-ba9c-1bab3277ff1dn%40googlegroups.com.
>


Sidney Coelho

ongelezen,
6 okt 2023, 13:01:1506-10-2023
aan autokey-users
I know it's been a long time since the last update on this thread, but I'd like to add something here.
If you are allowed to import custom modules from any folder, you could just create functions in those modules which receive the modules you need as arguments. E.g.

**/Autokey_Scripts/GeneralSetup.py

def alert(message, dialog):
    dialog.input_dialog(message)


In a regular script:

from GeneralSetup import alert

alert("Test successful", GtkDialog)


jos...@main.nc.us

ongelezen,
11 okt 2023, 19:06:3411-10-2023
aan autoke...@googlegroups.com
Welcome to the AutoKey community, Sidney Coelho!

Something similar to this was suggested in a discussion on one of our
issues on GitHub. Looks like it will work.

By default, AutoKey will only find personal modules that are placed in its
Modules folder (optionally set in the GUI).

For lots more info on this, see

https://github.com/autokey/autokey/wiki/Modules

If you experiment with this and get it to work, you can add example(s) to
our wiki where others are more likely to find them.

Joe
> --
> 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/d98ecac7-ee3e-4b5e-9fca-812e92d0fb02n%40googlegroups.com.



Allen beantwoorden
Auteur beantwoorden
Doorsturen
0 nieuwe berichten