Launch Apple Script on App Focus
The following related questions suggest that there doesn't appear to be an easy way for you to do this. 

https://stackoverflow.com/questions/7217724/mac-os-x-how-to-monitor-a-window-change-event

https://stackoverflow.com/questions/853833/how-can-my-app-detect-a-change-to-another-apps-window

The best you can do is query the current active window in a loop and react when you notice a change.

HTH