Launch Apple Script on App Focus

applescript

Is there a way to configure an Apple Script (or any script for that matter), to be run every time an application is focused?

For example, if I am on Safari currently, and I tap on a QuickTime window, I want it to run a script that I define.

Best Answer

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