Autohotkey for Linux — Sort Of…

I’ve enjoyed using AutoHotKey on Windows. If you haven’t used it, it is kind of hard to explain exactly what it is. Its sort of a scripting language, but it can manipulate windows, send key strokes, mouse events, and so forth.

Its surprising that there isn’t really a good analog to this program in Linux. Sure you have all sorts of scripting languages including the shell. But I haven’t really seen any that do a good job manipulating windows.

However, in true Unix fashion, there are a few tools you can use to make your favorite scripting language work with your desktop — assuming you have a compatible window manager (which you probably do).

The two main weapons in the macro arsenal are xmacro and wmctrl (chances are you can download them right from your package repository). You might also want a utility that lets you make simple dialog boxes for scripts. I like Kommander, but that’s not as lightweight as, say, Zenity, but that’s purely optional.

The wmctrl package allows you to list the current windows. You can raise a window and do other basic operations like set the window title or switch desktops. Here’s a great page about wmctrl: http://jrandomhacker.info/Wmctrl_examples [broken link]. Note that the desktop stuff doesn’t seem to work with KDE4.1 (my testing) and blackbox (from that page).

The xmacro package lets you send keystrokes and mouse events to the X terminal. It can also record actions, but I rarely use that feature. If you need it though, you might need to add:

Load “Record”

In the Modules section of your xorg.conf. You can use the recorder to “start” a script by running:

xmacrorec2 >temp.macro

The program will ask you to pick a “stop” key — something you won’t use in the recording like the Escape key, for example. Then you do your actions — try not to make any mistakes — and press the “stop” key. The temp.macro file will have the actions recorded. You can edit them (cut out any mistakes, delay between them, activate windows, etc.).

The only real problem with xmacro is that the mouse positions are absolute. So you are better off if you can stick to keystroke commands. However, if your scripting skills are good enough, the wmctrl -l -G command gives you enough info to compute relative mouse coordinates for any window.

I used AutoHotKey to correct a missing feature in ThunderBird for Windows. I wanted to just print a PDF attachment in an e-mail message (something Outlook does easily). With the Linux version of ThunderBird I wrote the following quick and dirty script:


   # Simple script to print the first PDF attachment from an e-mail# in thunderbird
# assumes you are already on the message with the attachment
# and also assumes the attachment is a PDF and that the
# PDF reader is from Adobe
# but for debugging try this
#wmctrl -R thunderbird
sleep 2  # wait for user to let go of keyboard

xmacroplay -d 10 $DISPLAY << ~~~
KeyStrPress Alt_L
KeyStrPress f
KeyStrRelease f
KeyStrRelease Alt_L
KeyStrPress a
KeyStrRelease a
KeyStrPress 1
KeyStrRelease 1
KeyStrPress o
KeyStrRelease o
~~~
sleep 1
# adobe should be active but just in case
# either comment this out or change it if you want to
# use kpdf or ockular or whatever
wmctrl -R adobe
xmacroplay -d 10 $DISPLAY << ~~~
KeyStrPress Alt_L
KeyStrPress f
KeyStrRelease f
KeyStrRelease Alt_L
KeyStrPress p
KeyStrRelease p
KeyStrPress Alt_L
KeyStrPress o
KeyStrRelease o
Delay 2
KeyStrRelease Alt_L
KeyStrPress Alt_L
KeyStrPress f
KeyStrRelease f
KeyStrRelease Alt_L
KeyStrPress x
KeyStrRelease x
~~~
# change the x above to c if you don't want to quit Acrobat

I used the Window manager to bind the script to a key and its all done. If you don’t want to go that far, you can uncomment the wmctrl line that activates Thunderbird and start the program from a shell prompt or an icon. Naturally, you could use wmctrl -l to verify that the attachment opened, etc. But this will get you started.

Of course, this doesn’t approach some of the power of Autohotkey. But it is still pretty usable.

Extra note: If you do want to experiment with using the mouse, you might consider using xev to look at mouse coordinates. You can use wmctrl -l to get window IDs and then use xev’s -id option to attach it to a particular window and watch events. A grep is handy to filter out events you don’t care about, or catch it all in a file and examine it later.

Update: There is another tool I have found that seems to work well and integrates window control and keyboard control. Check out xdotool.

2 thoughts on “Autohotkey for Linux — Sort Of…

  1. You don’t mention AutoIT Script when writing about Windows automation in this blog entry. I used it in my last job. See http://www.autoitscript.com/

    Thanks for elaborating about xmacro’s mouse absolutes. It helps me confirm what I thought. I’ll use dcop to work around it in KDE.

Leave a Reply