Skip to content

Instantly share code, notes, and snippets.

@holgr
Last active April 23, 2024 18:33
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save holgr/59f8df7f81aa2b74d67e0ab95e2fd28a to your computer and use it in GitHub Desktop.
Save holgr/59f8df7f81aa2b74d67e0ab95e2fd28a to your computer and use it in GitHub Desktop.
macOS process stealing window focus

Sean Harding finally put into words an issue that I've witnessed for a while now on macOS Sonoma.

This is it: "Weird MacOS thing: the window I am working in will sometimes lose focus (without the app losing focus). Suddenly, and without any obvious cause. It will be as if no window has focus. Sometimes happens as often as every few minutes, sometimes doesn't happen for a long time. I can’t figure out any common variable. But it can be very disruptive. Has anyone else seen this? Any ideas?"

I've adjusted the Python script mentioned in the thread to work with Python 3.x that you can get with Homebrew for example.

This is what the output looks like:

> python3.11 find_focus_stealer.py
2024-03-08 17:20:34: iTerm2 [/Applications/iTerm.app]
2024-03-08 17:20:37: Safari [/System/Volumes/Preboot/Cryptexes/App/System/Applications/Safari.app]
2024-03-08 17:25:17: CoreServicesUIAgent [/System/Library/CoreServices/CoreServicesUIAgent.app]
2024-03-08 17:25:51: Safari [/System/Volumes/Preboot/Cryptexes/App/System/Applications/Safari.app]
2024-03-08 17:25:52: iTerm2 [/Applications/iTerm.app]
#!/opt/homebrew/bin/python3
try:
from AppKit import NSWorkspace
except ImportError:
print("Can't import AppKit -- try installing it with:")
print("pip install pyobjc")
print("(Make sure your pip corresponds to your python install, e.g. python3.12 and pip3.12)")
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)
@abediali
Copy link

This is the most annoying bug I've ever encountered in my life. I've had to deal with this for months. MacBook Pro 2018 i9 and now even a 2023 MacBook Pro M3 Max. Is there a way we can drill deeper to see what apps are calling the CoreServicesUIAgent? I know there's some correlation between the autoupdates some of the apps have and this bug as well but who knows.

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