macOS: Application stealing focus

By | 29th March 2021

A few weeks ago I had an issue with a macOS system which focus kept being stolen by an unknown application. All the windows were displaying as inactive so it had to be a background process.

A StackExchange user called medmunds had adapted a script from another post, that seems to have been modified from another from some Apple forum. Isn’t the Internet great?

His script very quickly displayed the culprit. Very useful indeed.


	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."

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]' % (
  '%Y-%m-%d %H:%M:%S'),
image_printPrint this page

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.