A quick googling reveals http://www.autohotkey.com/board/topic/85660-run-scripts-when-window-opensbecomes-active/ . Its essense is:
You have to check in a continuous loop:
There is no way to >run< a script when a program opens/is active. The reason is that there is "nobody home" to say 'wake up'.
Instead, you can have a script that is running all the time, and one of its functions is to watch for your target window(s) and then call a subroutine to "do whatever".
The "WinTrigger" script linked to further in the thread uses #Persistent
and SetTimer
which is essentially the same approach but is neater.