SendKeys is fantastic
We use a database that doesn't let me delete multiple entries at once. ACK. Since the import mechanism is likewise crusty, I've ended up with several hundred new records that I want to back out. Shame on me for not being more careful with backups, but now I need in fact to delete hundreds of records at once.
This is a perfect use case for a GUI automator, of which there are several for Python on Windows. The one I've tried before is pywinauto (good list there of the others). I got it to work, but it felt as dirty as Windows itself. Consider this snippet from my pywinauto script, which saves a series of web pages opened as tabs in Firefox:
app = Application().start_(FIREFOX % url)SendKeys is much more intuitive. It always inputs to the active window, so you don't have to mess with app and window objects. Instead, you spend your time in a neat mini-language that closely mirrors the process of manually manipulating Windows. The documentation is concise and clear, and the module has no additional dependencies, not even ctypes!
time.sleep(3)
if not app.windows_(): # Firefox wasn't already running; we launched it
app = Application().connect_(title_re=".* - Mozilla Firefox")
firefox = app.window_(title_re=".* - Mozilla Firefox")
firefox.TypeKeys("%FA") # File -> Save As
while app.SaveAs.FileNameEdit.TextBlock() != save_as:
app.SaveAs.FileNameEdit.SetEditText(save_as)
app.SaveAs.TypeKeys("%S") # "Save" button
firefox.TypeKeys("%FC") # File -> Close Tab
Here is the above, ported to SendKeys (% is the alt modifier in both scripts):
import SendKeysGUI automation is like comedy: it's all in the timing. Watch both micro-timings and longer application-blocking actions. "Instantaneous" for a human means less than one-tenth of a second, but not for a computer, and you need to account for the micro-seconds between manual keystrokes: SendKeys' global
SendKeys.SendKeys("""
{LWIN}rFirefox.exe{ENTER} {PAUSE 3}
%fa {PAUSE .5}
New{SPACE}filename.html {PAUSE 1}
%s {PAUSE 1}
%fc {PAUSE 1}
""", pause=0.1)
pause argument is therefore a great touch. With actions that actually block the application, script for the worst case, because once a script gets out of sync with the current Windows state, there's no going back. So if a certain action takes between 1 and 2 seconds to complete, give it 2.25. A third of the work is getting the keystrokes right, the rest is tweaking the timing to be as fast as possible while observing zero tolerance for error.One trick is to prep as much as you can, like on a cooking show. For example, I needed to type in a reason for each record I was deleting. Having SendKeys type this for me takes maybe half a second, but putting it in the paste buffer and having SendKeys merely paste it for me is much closer to instantaneous. A half-second per iteration is over 8 minutes on 1000 iterations.
SendKeys could easily be developed into an outright shell, passing in automation scripts on the command line. You would need a mechanism for specifying the additional arguments like
pause (mime-style headers would suffice), and you would also want a commenting capability (just add # to the list of special characters). All in all, though, I highly recommend it when you need to hack together a GUI automation script.
0 comments:
Post a Comment