AppKit not dynamically sensing the Frontmost App
I'm using Appkit in Python to have the computer sense the frontmost app. It will correctly sense the frontmost app at first but when I change the frontmost app and leave the terminal running, it will not update the new information. For example, the following code will work:
from AppKit import NSWorkspace
active_app_name = NSWorkspace.sharedWorkspace().frontmostApplication().localizedName()
But now suppose I make Chrome rather than Excel the frontmost app and run the following program on terminal. Then after the terminal prints 'Chrome', I click on the app 'Excel', the terminal will continue to print 'Chrome' and it will never sense that Excel is the frontmost app.
from AppKit import NSWorkspace
while True:
active_app_name = NSWorkspace.sharedWorkspace().frontmostApplication().localizedName()
print (active_app_name)
if active_app_name == 'Microsoft Excel':
pass
# do stuff
else:
time.sleep(5)
I want AppKit to be able to sense the new frontmost app when I make changes to which the frontmost app is.
MacBook Pro, OS X Yosemite (10.10.1)