Skip to content

Instantly share code, notes, and snippets.

@medmunds
Created February 6, 2015 00:51
Show Gist options
  • Save medmunds/60f79041b30060cf24d6 to your computer and use it in GitHub Desktop.
Save medmunds/60f79041b30060cf24d6 to your computer and use it in GitHub Desktop.
Find the Mac OSX app that's stealing the active window away
#!/usr/bin/python
# Print the active OSX app whenever it changes.
# (Use to figure out which ill-behaved app is stealing focus away from you.)
# Adapted from http://apple.stackexchange.com/a/148094/112614
try:
from AppKit import NSWorkspace
except ImportError:
print "Can't import AppKit -- maybe you're running python from brew?"
print "Try running with Apple's /usr/bin/python instead."
exit(1)
from datetime import datetime
from time import sleep
last_active_name = None
while True:
active_app = NSWorkspace.sharedWorkspace().activeApplication()
if active_app['NSApplicationName'] != last_active_name:
last_active_name = active_app['NSApplicationName']
print '%s: %s [%s]' % (
datetime.now().strftime('%Y-%m-%d %H:%M:%S'),
active_app['NSApplicationName'],
active_app['NSApplicationPath']
)
sleep(1)
@yehosef
Copy link

yehosef commented Nov 19, 2015

This is great! - how would you get the title. Specifically I'm interested in getting the URL to see data similar to what something like rescuetime gives.

@TwisterMc
Copy link

This is great code, but what if I want to get the window title as well? Is that possible?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment