I'm not sure this is possible using the built-in "actions" macro feature of PS. It is certainly possible with the GIMP which has complete programming support natively.
However, have you tried using something like AutoHotKey to generate a keyboard and/or mouse macro? It certainly has the logic and variable capabilities, you'd just need to make sure that you can control PS properly. I've done this with numerous applications in the past but not with PS.
Given your problems with windows not aligning, the only way AHK macros would work for you would be if everything can be done via keyboard. Though I assume that you noted the mouse control is relative to the parent window?
In GIMP, you can do macros using "script-fu", macros are written in a language called "scheme" though There are a number of sites with many examples. Here for example. If you need to go further, you can write your own plugins using Python. The GIMP documentation site has some details. Again there are plenty of sites with examples. Have a go and perhaps raise a new, more specific question as you get stuck ;)
For Photoshop, here is the reference I missed last time. Thanks to Alan for pointing it out, it's been a long time since I last used Photoshop.