Archive
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…
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.
Getting an Artec e+ Pro Scanner work
Problem
I have an Artec e+ Pro scanner that I used years ago under Windows XP without any problem. It’s a great scanner, I liked it. I found it a few weeks ago and today I wanted to use it. Well, I was too naive again…
(The exact text I have on its box: Artec e+ Pro Scanner, 1200×2400 dpi, 48 bits true color quality).
The official driver for this model is up to Windows XP. I only have Windows 7 and Linux, so first I tried to use it under Windows 7. On the installation CD there is a SETUP.EXE that I opened in Windows XP compatibility mode. The installation of the driver went smoothly but when I opened the application and clicked on “Scan”, it told me that a Twain driver is missing. I was looking for it everywhere but I didn’t find it. In the forums I read that Artec didn’t make a Windows 7 driver for this model.
Solution
OK, it won’t work under Windows 7. Then I thought: “Hey, let’s try it under Linux too!” And you know what? It worked! :) Here is what to do.
There is a scanner howto for Ubuntu (see here). I installed xsane but it dropped me an error message similar to this: “Failed to open device 'artec_eplus48u:libusb:001:003': Invalid argument.” Fortunately there is a page that explains how to configure this particular scanner for xsane (see here).
The page ArtecEplus48uConf explains the configuration of a 48U scanner. However, my scanner must be a variation of this scanner, its official name is “e+ Pro”. But they are very similar. Here is what I did to resurrect my e+ Pro scanner:
- download the firmware
1200.usband extract it (you can also copy it from the official CD) - copy the file
1200.usbto /etc/sane.d/ - edit the file
artec_eplus48u.confand modify the part shown below
# This section is for the Artec E+ Pro # Note, that the name of the firmware file is called 1200.usb for # this device usb 0x05d8 0x4004 option artecFirmwareFile /etc/sane.d/1200.usb option ePlusPro 1 option vendorString "Artec" option modelString "E+ Pro"
I only had to edit just one line to precise the path of the file 1200.usb.
Now xsane should be able to handle your scanner correctly :)
Update (20120916)
There is a solution to make it work under Windows 7. Install XP Mode, which is a virtual Windows XP inside your Windows 7. In this XP I could install the official CD and I could use the scanner.
When I launched the scanner application, it still said that the Twain driver is missing. But then XP noticed the new hardware and asked for a location from where it could install the driver. After specifying the XP folder on the CD, the Twain driver got installed.
Auto Login to Windows 7
Problem
I have a Windows 7 in VirtualBox. When I launch it, I don’t want to spend time with logging in. How to skip this step?
Solution
I found the solution here.
In short: launch netplwiz, in the Users tab, uncheck the box next to “Users must enter a user name and password to use this computer”, press Apply, provide the password, then OK, OK.
Insert syntax highlighted source in Powerpoint
Problem
You want to have syntax highlighted source in Powerpoint.
Solution
First, create an RTF file from the source:
pygmentize -f rtf -o hello.rtf hello.py
Open the RTF file in Word, select the source, then insert it in Powerpoint.
I made a basic script to automate the conversion. Usage:
py2rtf hello.py
The script is available here.
Increase the size of a Virtualbox hard drive (.vdi)
The following entry is based on this post.
Problem
I have a Linux host with a Windows 7 guest. The hard disk of the Windows guest was set to 30 GB but it got full. How could I extend its size to 40 GB for instance?
Solution
Locate the .vdi file of the virtual system. To be safe, make a backup copy of it. After that you can increase its size:
sudo VBoxManage modifyhd Windows7.vdi --resize 40000
Here Windows7.vdi is the name of my .vdi file and 40000 is given in megabytes, which is roughly 40 GB.
Now, if you boot the system and you verify the size of the C: drive, it will still be the old value. The increased size appears as a new unformatted and unused partition. You can join it with the C: partition the following way:
Go to the Control Panel, then System and Security, then Administrative Tools. Here you will see an icon called Computer Management. Start it. Choose Disk Management on the left side. Right click on drive C: and choose “extend volume“. It will be extended in a second. Done.
Powerpoint to PDF
I just discovered that Microsoft Powerpoint 2007 supports PDF export. By default this plugin is not installed. Click on the Office button, and under Save as... there will be a menu point about exporting to PDF and XPS. Click on it and you will be redirected to a help file from where you can visit a download page of this export plugin. Once installed, this feature is available under Save as…

