Start a new topic

Using Autokey

This posting is in reponse to a request from user:idim in this thread here about AutoKey in Linux.

See also this thread on AutoKey.


AutoKey is a Linux equivalent of Windows' autohotkey (which I'm not personally familiar with), but it uses Python as a scripting language, making it extremely powerful and flexible.

There are two versions: AutoKey, which uses python 2 and autokey-py3, which uses Python 3. As I understand it, the former is no being developed or supported, the latter is still active.


I use AutoKey extensively for various usefully automatable tasks, but user:idim was particularly interested in using it for search.


There are lots of sample scripts out there, see for example here and here, the latter being the original source for my various search scripts.


I'll post here one of my basic search scripts, based as noted on code from the second of the above links. It does a Google search on the europa.eu domain (site:europa.eu). Note that I am (I think!) using the python 2 version:

 

import webbrowser
base="http://www.google.com/search?q="
phrase=clipboard.get_selection()

#Remove trailing or leading white space and find if there are multiple
#words.
phrase=phrase.strip()
singleWord=False
if phrase.find(' ')<0:
    singleWord=True

#Generate search URL.
if singleWord:
    search_url=base+phrase+"+site:europa.eu"

if (not singleWord):
    phrase='+'.join(phrase.split())
    search_url=base+phrase+"+site:europa.eu"
      
webbrowser.open_new_tab(search_url)
As you'll see, it's extremely simple and very easy to customise for other sites (as long as they support search terms embedded in the URL). I have scripts for searching Linguee, IATE, for ICD10 codes on who.int, thesaurus.com and LEO.

I also have scripts for converting dd.mm.yyyy dates to month dd, yyyy formated dates and scripts for converting German legal citations to an English format, which I'd be happy to share if anyone is interested, and a few little functions for various non-translation work processes (such as writing certain more or less standard emails).

If there's anything else you want to know, post here, and I'll do my best to answer.

Jeremy




Hi Alain,


off the top of my head, no I don't know how to do this, but it shouldn't be too difficult. Googling python open terminal gets you this, which in turn takes you to this discussion on stackoverflow. I'm pretty sure you'll find what you need there (including possibly an alternative way of achieving what you want to achieve).


As for pasting text, I use:
    keyboard.send_keys("Text to paste") or keyboard.send_keys(StringVariableContainingTextToPaste)

Note that there are other ways of pasting text – I find this works most reliably on my systems, but in the arcane world of Linux you may find something else works best for you.


Of course to get the clipboard text we just use:

    clipboard.get_selection()
as above.


HTH
Jeremy

Hi Jeremy,


Until now I can open the terminal and paste the text, like this:


os.system("gnome-terminal")
keyboard.send_keys("trans jpn:fr ")


I still cannot paste the clipboard contents at the end (I tried many things but I got an error message each and every time).


For the moment I use Ctrl-C before, and Ctrl-V after the autohotkey script. It works, but it’s three keyboard shortcuts instead on only one. :-(

Plus the enter key at the end!

So it’s four keyboard keys for the moment, much too long but better than nothing... :-)


Hi Alain,

without knowing the error message, its hard to help. My first suggestion would be to insert all the text to be pasted into a variable and then use keyboard.send_keys(VariableName).

I'm absolutely not a Python expert, and I've had to do quite a bit of Googling and trial and error to get most of my scripts to work.


Not related to the script in question, but in Linux, apart from the CLIPBOARD which uses the Ctrl-C and Ctrl-V sequence, you can also use the PRIMARY SELECTION.


Just highlight some text, and paste it anywhere with a click of the middle mouse button (or a two-finger tap on the trackpad).

Hi amos,

I just installed autokey and tried your script... it works perfectly, thanks!


I would like to know how to make a script with the clipboard content in the middle of the URL.


For example:


URL beginning

https://www.tradooit.com/info.php?q=


clipboard content


URL end

&btn-recherche.x=0&btn-recherche.y=0&font-size=12&langFrom=en&langTo=fr


Can you help me with that?

Hi Alain.

that's pretty straightforward:


import webbrowser
URLstart="URL start goes here"
phrase=clipboard.get_selection()
URLend="URL end goes here"

#Remove trailing or leading white space and find if there are multiple
#words.
phrase=phrase.strip()
singleWord=False
if phrase.find(' ')<0:
    singleWord=True

#Generate search URL.
if singleWord:
    search_url=URLstart+phrase+"+site:europa.eu"+URLend

if (not singleWord):
    phrase='+'.join(phrase.split())
    search_url=URLstart+phrase+"+site:europa.eu"+URLend
      
webbrowser.open_new_tab(search_url)

Replace the URLstart and URLend text as required and that's it.

Any problems, post here and I'll do my best to help.


Jeremy


Hello Jeremy,


Many thanks for this post. Just tested the script and it works just fine on my AutoKey-py3.


It is a very nice platform-wide solution for quickly launching various web searches based on clipboard content.


I remember the page on AutoKey sample scripts, but had missed the Google groups page.


Cheers,


Jean

Hi Jeremy,


Thank you so much!

I just had to delete the two

+"+site:europa.eu"

It works perfectly!

Hi Jeremy,


I’m trying to do a very simple thing...


Open the terminal

Paste this: trans jpn:fr [clipboard content] [enter]


I tried to find how to do it in the wiki... but it’s a very small wiki and I found nothing.

Do you know how to do that?

Login to post a comment