Detecting changes in the front-most app in OS X with Python

I've created an app that uses pyobjc

to monitor the current app I'm using, or the current url if using Safari / Chrome. I get my first application using:

active_app_name = NSWorkspace.sharedWorkspace().frontmostApplication().localizedName()

      

which runs in a loop that fetches information every second. However, this solution seems ineffective given the fact that I could lose information for more than 30 seconds.

I'm wondering if there is a solution using events, i.e. have a watcher that listens when there is a change in the current furthest application.

I found this answer Mac OS X - How to track window change event? , but there are two problems: 1. I only want information about the application, not a specific window, and 2. I have no idea how to translate these messages into Python code.

AppleScript's solution is fine considering that I can call it from Python with osascript

.

Thank!

+3


source to share





All Articles