Python AHK – Python automation package – Automation made simple

Python is a beautiful programming language, anybody that is not convinced please read my python article to get convinced… But one thing is missing, a python automation module to help automating tasks like autohotkey. I wish there was a single python ahk module.

Python and AHK

I discovered autohotkey in 2007. It was rather simple to learn and I did program several scripts to help me in my everyday life at the computer in no time. As simple as it was too learn, I soon hit its limitations. Autohotkey was pretty annoying to pass variables in functions and process any variable or text was just a pain.

Fortunately, I found python in 2009. It was superior to autohotkey in all circumstance except that I had a rather hard time doing the very simplest automation. I did countless hours of research and testing. I even developed my own module PYHK to master python automation. The result are 5 modules that will make python automation simple. I will call those 5 modules the python automation package. From now on you will be able to automate any task in python.

Python automation package

  • SendKeysCtypes
  • PYHK
  • win32gui
  • pywinauto
  • mouse


SendKeysCtypes allows you to send keystrokes and shortcuts to any window. It is very powerful. SenkeysCtypes is a new and more stable version of SendKeys. I have had some small issues with SendKeys in the past which SendKeysCtypes solves. There is no documentation on SendKeysCtypes, but you can use the documentation from Rutherfurd for his module SendKeys.


PYHK helps you receive global hotkeys and trigger functions. PYHK is based on pyHook and makes hotkey registration very simple. The project is developed and hosted by See the PYHK end user documentation for help.


I use win32gui for simple window handling such as moving and resizing. I personally prefer win32gui for short, simple tasks. There is one problem with win32gui, there is no real help file. Fortunately, there are plenty of examples out there. Google is your friend.


I use pywinauto for more complex window tasks. An example would be if I had to access a menu within a program (like File-New). For the most part it is enough to simply use win32gui. Please refer to the pywinauto documentation for help. If you are starting out with python automation, I recommend you to just use pywinauto.


Mouse is a small module to control the mouse. This is the most robust way I have found so far. The version I use is an extension of a module I found at stackoverflow – ctypes mouse_event. You can find more info on mouse in my blog post – Control your mouse in python.

The python automation package should allow you to automate any task in python. Let me know if you have any questions. Happy coding! – Control your mouse in python

Automation of user inputs always requires to click several buttons or menus. I personally prefer to use to automate and control my mouse in python. It was posted in a forum entry on stackoverlow

The module as is works great. I did however miss some functionality: Clicking without moving the cursor. I extended to click without moving the cursor. I have noticed that very rarely you still see the cursor moving. This only happens if my computer is rather busy. I remember using PostMessage back in my old AHK days, but unfortunately I can not get it to work.

An other function I included is clicking relative coordinates. I often would rather click coordinates in a window without having to deal with where that window is actually at. On a side note, a great tool for relative coordinates especially with multiple monitors is my module python screen coordinates. It does all the conversion of coordinates and has a hit test to determine what screen you are on.

Do you know of other ways to control your mouse in python? Please share any method you know. I would really like to see a PostMessage example work. I know TableCrab has a working solution, but I didnt get a chance yet to look through all the code.

Here is my extended version of

import win32gui, win32api, win32con, ctypes

class Mouse:
    """It simulates the mouse"""
    MOUSEEVENTF_MOVE = 0x0001 # mouse move
    MOUSEEVENTF_LEFTDOWN = 0x0002 # left button down
    MOUSEEVENTF_LEFTUP = 0x0004 # left button up
    MOUSEEVENTF_RIGHTDOWN = 0x0008 # right button down
    MOUSEEVENTF_RIGHTUP = 0x0010 # right button up
    MOUSEEVENTF_MIDDLEDOWN = 0x0020 # middle button down
    MOUSEEVENTF_MIDDLEUP = 0x0040 # middle button up
    MOUSEEVENTF_WHEEL = 0x0800 # wheel button rolled
    MOUSEEVENTF_ABSOLUTE = 0x8000 # absolute move

    def _do_event(self, flags, x_pos, y_pos, data, extra_info):
        """generate a mouse event"""
        x_calc = 65536L * x_pos / ctypes.windll.user32.GetSystemMetrics(self.SM_CXSCREEN) + 1
        y_calc = 65536L * y_pos / ctypes.windll.user32.GetSystemMetrics(self.SM_CYSCREEN) + 1
        return ctypes.windll.user32.mouse_event(flags, x_calc, y_calc, data, extra_info)

    def _get_button_value(self, button_name, button_up=False):
        """convert the name of the button into the corresponding value"""
        buttons = 0
        if button_name.find("right") >= 0:
            buttons = self.MOUSEEVENTF_RIGHTDOWN
        if button_name.find("left") >= 0:
            buttons = buttons + self.MOUSEEVENTF_LEFTDOWN
        if button_name.find("middle") >= 0:
            buttons = buttons + self.MOUSEEVENTF_MIDDLEDOWN
        if button_up:
            buttons = buttons << 1
        return buttons

    def move_mouse(self, pos):
        """move the mouse to the specified coordinates"""
        (x, y) = pos
        old_pos = self.get_position()
        x =  x if (x != -1) else old_pos[0]
        y =  y if (y != -1) else old_pos[1]
        self._do_event(self.MOUSEEVENTF_MOVE + self.MOUSEEVENTF_ABSOLUTE, x, y, 0, 0)

    def press_button(self, pos=(-1, -1), button_name="left", button_up=False):
        """push a button of the mouse"""
        self._do_event(self.get_button_value(button_name, button_up), 0, 0, 0, 0)

    def click(self, pos=(-1, -1), button_name= "left"):
        """Click at the specified placed"""
##        self.move_mouse(pos)
        self._do_event(self._get_button_value(button_name, False)+self._get_button_value(button_name, True), 0, 0, 0, 0)

    def double_click (self, pos=(-1, -1), button_name="left"):
        """Double click at the specifed placed"""
        for i in xrange(2):
  , button_name)

    def get_position(self):
        """get mouse position"""
        return win32api.GetCursorPos()
#Added functions
    def invisible_click(self,pos=(-1, -1), button_name="left"):
        """Click in specified place without moving mouse"""
        xcur,ycur = win32gui.GetCursorPos()

    def invisible_click_rel(self,handle,pos, button_name="left"):
        """Click in window coordinates without moving mouse"""
        #get window info
        xleft, ytop, xright, ybottom = win32gui.GetWindowRect(handle)

        xcur,ycur = win32gui.GetCursorPos()


if __name__ == '__main__':
##    p = (210,260)
##    print win32gui.GetForegroundWindow()
##    mouse = Mouse()
##    mouse.invisible_click_rel(win32gui.GetForegroundWindow(),p)

I hope it helps you, please let me know about questions. Comments are always welcome!

Index your WordPress tags – Create a clean tag index

Tag clouds are all good and nice, but I personally always liked looking at an index of a book. It gives every book a structured clean look and feel. There is also the advantage of finding things you didnt know you were looking for. The first step of every search is finding the right terminology to find what you are looking for. An index can often be of great help and guide you in the right direction.

A really good plugin to get this job done is: Multi-column Tag Map. It makes it really simple to create a nice and clean looking index. See my Index in action for an example.

To get you started, go download the plugin, activate it and than use the following short code where you want your index:

[ mctagmap columns="3" more="- show all -" hide="yes" num_show="5" toggle="- hide -" show_empty="no" ]

To find more information about Multi-column Tag Map, visit

Rss feed to twitter – How to auto post your blog to twitter

A simple way of posting your rss feed to twitter

I publish and promote most of my blogs and new content along all social media. For the most part I end up doing a lot of repetitive work – I am allergic to that.

This is why I will show you how I publish my rss feed to twitter. I use The setup is within seconds and so far it is working flawless.

Advice on settings

Make sure you edit the advanced settings and select an update frequency that doesnt make your twitter feed make look automated.

In general I use post content – title only and no post prefix. I hash tag my tweets in the post suffix. Wait, you dont know what my standard hash tag is? Probably a good opportunity to follow my twitter account. [twitter_follow username=”schurpf” language=”en”]

A word of warning

Auto posting can be decreasing your followers twitter experience. Make sure your feed doesnt look like you are trying to spam your followers by overdoing the self promotion. You can find more reasons why auto posting might not be a good idea in a post by Jeremy Lindh.

Auto posting rss feed of quality bloggers

I got a pretty good idea from Katie Freiling from her youtube video: Twitter Automation: How To “Auto-Tweet” Blog Posts . Her idea is to publish rss feeds of trusted high quality bloggers to your twitter account. This will serve several purposes:

  1. Your audience gets more quality content
  2. You can promote and bond with a high quality blogger
  3. And all this without extra work

Just be careful whose rss feed you chose to auto post.


Show active plugins in WordPress

Here is a pretty neat way to list all your current active plugins in your WordPress install. I always wonder what plugins people use on their WordPress site. It is also a way of showing your appreciation to plugin authors by providing a link to their site – they deserve it!

To show all active plugins in WordPress I use a plugin called Plugin List. You can find more info on it in the list below. In order to actually display active plugins in a post you have to use a second plugin to allow to display php. I currently use Allow PHP in post and pages. It allows you to use
[php] code [/php]
All together use the code:
[php] wp_list_all_active_plugins(); [/php]
and you are all set.

I personally dont consider it a good idea to reveal what plugin version you are using. Assume you are a little lazy and dont update your plugin on a regular basis or lets just say you forget – it is a security problem if you tell people what version you are using. In order to take the version number out you have to edit the Plugin List plugin. Do the following:

  1. Find the plugin directory of Plugin List. The plugin folder is called wp-plugin-lister and is in “your wp install directotry”/wp-content/plugins/wp-plugin-lister
  2. Open plugin_lister.php
  3. Change
    $plugin_list .= "<h4><a href='".$d['PluginURI']."' target='_new'>".$d['Title']."</a> (Version: ".$d['Version'].")</h4>";


    $plugin_list .= "<h4><a href='".$d['PluginURI']."' target='_new'>".$d['Title']."</a> </h4>";

and you are all set!

Please let me know if it works for you. I appreciate comments.

Acitve plugins on

[php]wp_list_all_active_plugins(); [/php]