GUI automation is a hard problem. I'm impressed by Hammerspoon (OS X) and its nontrivial examples in the basic docs: https://www.hammerspoon.org/go/#winmovenethack