Paste one character at a time for better screen recordings

I occasionally find myself making videos or animated gifs to illustrate help docs. While I can drag, drop, and click with the best of them, typing several lines of code without a single typo can be more challenging than I like to admit! I don't want to leave my errors in the examples, so how can I automate this?

After a bit of tinkering, I came up with the following. Unfortunately, I don't believe this will work on anything but OS X, but perhaps it will prove useful to someone. Save it as (or whatever you like). Then, simply copy the text you want, run python, and click over to where you want your text to appear.

import string 
import xerox
import time
import os
import re

#mystring = """Enter string here, comment out the next line if you don't want to use clipboard"""
mystring = xerox.paste()

for line in mystring.split("\n"):

    #send letters - called word because cruft
    for word in line:

        if word == "'":
            #may not work on international keyboards
            cmd = """osascript -e 'tell application "System Events" to key code 39 '"""
            #Shouldn't need to escape anything but quotation marks
            word = string.replace(word,'"', '\\"') 
            cmd = """osascript -e 'tell application "System Events" to keystroke "%s" '  """ % (word)

    cmd = """osascript -e 'tell application "System Events" to keystroke return ' """ 

And here it is in action, with the text that happens to be copied to my clipboard right now:

Slow Pasting in Action!

I should mention I used Licecap to make this gif. It's small, simple, and does its one task well.