Is there a way to find when the activeApplication changes in OSX through Python and AppKit? I know how to find out launchedApplication and activeApplication ( please refer to my other question here: http://stackoverflow.com/questions/373020/finding-the-current-active-window-in-mac-os-x-using-python )
I've got an OS X app that does this by polling with an NSTimer. I tried searching for distributed notifications to see if I could find a better way to do it, but I couldn't see anything terribly useful.
I did get notifications when application were launched or quit. which is at least a little helpful. You can see the registration of these where my controller wakes up.
This application has been immensely helpful to me and even polling once a second uses nearly no CPU. If I could make it more event driven, I would, though. :)
I'm not aware of an 'official'/good way to do this, but one hackish way to go about this is to listen for any distributed notifications and see which ones are always fired when the frontmost app changes, so you can listen for that one:
You can set something like this up:
def awakeFromNib(self):
NSDistributedNotificationCenter.defaultCenter().addObserver_selector_name_object_(
self, 'someNotification:', None, None)
def someNotification_(self, notification):
NSLog(notification.name())
After you've found a notification that always fires when apps are switched, you can replace the first 'None' in the addObserver_etc_ call with the name of that notification and check for the frontmost app in your 'someNotification_' method.
In my case I noticed that the 'AppleSelectedInputSourcesChangedNotification' fired everytime I switched apps, so I would listen to that..
Keep in mind that this can break any moment and you'll prolly be checking for a change in the frontmost app more often than needed.
There must be a better way though.. hopefully :)