Presently, the script uses a polling loop to check the NSWorkspace's activeApplication (note: activeApplication is deprecated). It would be more efficient to register an observer for notification name NSWorkspaceDidActivateApplicationNotification, which notification is sent just as soon as an application is activated/focused.
I experimented with this approach in a currently private fork of this repository. Handling Ctrl+C / SIGINT was somewhat tricky, but I posted about how to do this on Stack Overflow:
https://stackoverflow.com/questions/49092405/how-to-register-a-sigint-handler-that-will-run-as-soon-as-ctrlc-is-pressed