I have created an application using pyobjc
to monitor the current application I'm using or current URL if Safari/Chrome are being used. I get the frontmost application using:
active_app_name = NSWorkspace.sharedWorkspace().frontmostApplication().localizedName()
which runs in a loop that retrieves info every second. Nevertheless, this solution seems inefficient, given the fact that I can lose info for over 30 seconds.
I am wondering if there is a solution using events, i.e., have an observer that listens when there is a change in the current frontmost application.
I have found this answer Mac OS X - How to monitor a window change event?, but there are two issues: 1. I only need info about the application and not about the specific window, and 2. I have no idea how to translate these messages into Python code.
An AppleScript solution is suitable, given that I could call it from Python using osascript
.
Thanks!