How do I tell which app stole my focus in OS X?

26

16

The active window on my machine occasionally loses focus. The active app remains the same -- if I was in Chrome before, I'm still in Chrome now -- but the active window is no longer active. No window is active. This is frustrating; it happened while typing this question, and my keystrokes suddenly stopped registering.

I believe that some other app is stealing focus, but that it itself has no UI to display, so the active window becomes not active, but the active app remains active.

The question is: How do I track down the offending app, so that I can angrily delete it? Normally in cases of focus theft, the culprit is obvious, because it has focus. In this case, I'm stumped.

Josh Bleecher Snyder

Posted 2014-03-26T21:09:34.003

Reputation: 363

Worked great. AirServer was stealing my focus. WHY!!!! – Michael Potter – 2019-04-24T23:14:06.827

You could try the Apple > Force Quit... menu to see if there is anything running that shouldn't be. – Michael Frank – 2014-03-26T21:18:59.730

1@MichaelFrank It won't show applications that have no menu bar (e.g. those with LSUIElement set to true in Info.plist). Those are perfectly capable of that behavior. – Daniel Beck – 2014-03-27T00:08:45.383

@DanielBeck Ahh, gotcha. That's handy to know. – Michael Frank – 2014-03-27T00:09:56.750

1

FYI, I asked basically this same question on Apple SE: Is there a way to detect what program is stealing focus on my Mac?

– Kevin Reid – 2014-03-27T00:12:07.030

tell application "System Events" to display alert ((name of first application process whose frontmost is true) as string) unfortunately does not consider processes without menu bar. – Daniel Beck – 2014-03-27T00:15:19.213

Does searching the list of applications started on login help? System Preferences » Users & Groups » (Your Username) » Login Items – Daniel Beck – 2014-03-27T00:16:17.027

@KevinReid thanks! Sad to see no ready answers. – Josh Bleecher Snyder – 2014-03-27T19:17:20.027

@DanielBeck this is a work laptop. It has all kinds of stuff running on it, many of them installed not by me. I'm afraid that inspection is unlikely to reveal the answer I need. I looked at the Login Items, but they all have a menu bar. I suppose I could write a script to trawl through my hard drive, parse Info.plist for LSUIElement set to false, and start there... – Josh Bleecher Snyder – 2014-03-27T19:20:40.870

Answers

36

Here's a script that will tell you which app is activating without telling you. I adapted it from an answer to @KevinReid's question over on Apple SE.

Leave it running in a terminal, wait for the rogue app to steal focus, and see which app is listed last. (For me: Google Drive. Others have reported Symantec AV stuff.)

#!/usr/bin/python                                                                                                       

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)

medmunds

Posted 2014-03-26T21:09:34.003

Reputation: 477

This script tells me my culprit is Google Drive [/Applications/Google Drive.app] – MarkHu – 2015-04-06T19:58:29.580

The culprit for me ended up being Symantec. – Josh Bleecher Snyder – 2015-06-19T21:29:03.127

1In my case it is SecurityAgent [/System/Library/Frameworks/Security.framework/Versions/A/MachServices/SecurityAgent.bundle] – Ed Randall – 2018-03-09T12:53:19.523

Offending app was Microsoft Update Assistant. I much appreciate this answer, this problem has been driving me nuts for a while. – jamesbev – 2019-01-04T15:25:23.443

2My culprit was JetBrains Toolbox [/Applications/JetBrains Toolbox.app/Contents/jetbrains-toolbox-cef.app], was trying to update itself and crashing in the process. Thank you sooooooo much!! It was also consuming a huge amount of resources. – Gabriel – 2019-03-22T11:00:48.603

For me it was Air Display Helper. Also, I mis-indented the sleep(1) when copy-pasting this, causing it to furiously loop without sleeping and nearly melted my computer. – tboyce12 – 2019-05-30T19:52:27.153

@EdRandall Did you find a way to deal with SecurityAgent stealing focus? – Mike – 2019-10-16T16:51:50.723

@Mike yes, complained to our desktop support team . – Ed Randall – 2019-10-16T17:06:35.810

1

This will sound silly and absurdly simple... I had the same problem with my laptop when I used the trackpad or built in keyboard. Had two separate laptops give similar experiences after being exposed to a bit of moisture (yes, I spilled on the keyboard).

Adding peripheral mouse and keyboard resolved it for me.

Paul E

Posted 2014-03-26T21:09:34.003

Reputation: 81

1Interesting. I use an external mouse and keyboard, but hopefully this helps someone else. Thanks. – Josh Bleecher Snyder – 2014-09-17T22:20:54.653