I know it's not the answer that you want, but google for "macro recorder," and you can probably save yourself a lot of coding.
I really don't know how to direct you to begin, but since this is one of those gray areas of computing, see if there's another way to accomplish your task like via a REST API.
jonsca
Quantitative Phrenologist
5,621 posts since Sep 2009
Reputation Points: 1,165
Solved Threads: 581