Home > python, ubuntu, windows > Automate a Windows application from Linux using Python

Automate a Windows application from Linux using Python

Problem
For my everyday work I use Linux. I also have a Windows 7 in VirtualBox. Under Windows I’ve found a very nice movie catalogue called Movie Collector. It’s not free though; unfortunately I didn’t find any good open-source alternative for this task.

So, I had this software running in VirtualBox and I wanted to input a list of movies. I collected the list in a file. Movie Collector has a GUI interface where you can insert the title of a movie and it’s put in a queue (see the figure below). Then you can verify for each item if it found the correct movie.

The question is: how to insert the movie titles via the GUI interface in an automated fashion? I won’t type 100 titles by hand…

add_movies

Solution
It’s a perfect job for Python. There is a great module called autopy that includes functions for controlling the keyboard and mouse. I already had it under Linux. Fortunately, you can launch an autopy script under Linux and it can interact with a Windows GUI that runs in VirtualBox.

The idea is simple: read the input file, where there is a movie title in each line. Put the movie title on the clipboard (we are lucky again, if you put something on the clipboard under Linux, it’s available under Windows too in VirtualBox). Click in the Title text field, select everything with Ctrl+A, press Del to delete the content, paste from the clipboard and click on the Add to Queue button. Then repeat: take the next movie title, etc.

The following script does exactly this:

#!/usr/bin/env python

from autopy import key
from time import sleep
from jabbapylib.mouse import mouse
from jabbapylib.clipboard.clipboard import text_to_clipboards

TITLE = (68, 124)    # input text field's position
ADD = (575, 123)     # button's position

def delete():
    mouse.click_to(TITLE)
    key.tap('a', key.MOD_CONTROL)    # Ctrl+A
    key.tap(key.K_DELETE)            # Del
    sleep(.1)

def paste():
    mouse.click_to(TITLE)
    key.tap('v', key.MOD_CONTROL)    # Ctrl+V
    sleep(.1)

def add():
    mouse.click_to(ADD)
    sleep(.1)

def main():
    cnt = 0
    with open('movies.txt') as f:
        for title in f:
            title = title.rstrip('\n')
            text_to_clipboards(title)

            delete()
            paste()
            add()
            cnt += 1

    print cnt

#############################################################################

if __name__ == "__main__":
    print 'Switch to VirtualBox...'
    sleep(3)
    main()

The imported modules are here:

To figure out the position of GUI elements (to know where to click to), you can use this little helper.

  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: