Skip to content

Instantly share code, notes, and snippets.

@holgr
Last active August 7, 2024 01:11
Show Gist options
  • 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.

@sashkab
Copy link

sashkab commented Aug 3, 2024

Were you able to identify which app steals the focus? Because I cannot. The moment I started to log active window -- nothing steals the focus.

Also, I slightly modified your code to use logging framework:

from time import sleep
import logging

from AppKit import NSWorkspace

logger = logging.getLogger('focusstealer')

logger.setLevel(logging.INFO)
stream_handler = logging.StreamHandler()
stream_handler.setFormatter(logging.Formatter("%(asctime)s %(levelname)-8s %(name)s: %(message)s", "%Y-%m-%d %H:%M:%S"))
logger.addHandler(stream_handler)

workspace = NSWorkspace.sharedWorkspace()
active_app = None
while True:
    prev_app, active_app = active_app, workspace.activeApplication()
    if prev_app != active_app:
        logger.info('Focus changed to: %s (%s)', active_app['NSApplicationName'], active_app['NSApplicationPath'])
    sleep(0.5)

@holgr
Copy link
Author

holgr commented Aug 3, 2024

Were you able to identify which app steals the focus? Because I cannot. The moment I started to log active window -- nothing steals the focus.

Yep, I removed Carrot and never had the issue again.

@sashkab
Copy link

sashkab commented Aug 3, 2024

Yep, I removed Carrot and never had the issue again.

Thanks. I have it installed, but it's not running in background. So it might be something else for me...

Update: indeed it was Carrot...

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