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

14,148

Solution 1

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)

Solution 2

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.

Share:
14,148

Related videos on Youtube

Josh Bleecher Snyder
Author by

Josh Bleecher Snyder

Updated on September 18, 2022

Comments

  • Josh Bleecher Snyder
    Josh Bleecher Snyder over 1 year

    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.

    • Michael Frank
      Michael Frank about 10 years
      You could try the Apple > Force Quit... menu to see if there is anything running that shouldn't be.
    • HikeMike
      HikeMike about 10 years
      @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.
    • Michael Frank
      Michael Frank about 10 years
      @DanielBeck Ahh, gotcha. That's handy to know.
    • user3540003
      user3540003 about 10 years
      FYI, I asked basically this same question on Apple SE: Is there a way to detect what program is stealing focus on my Mac?
    • HikeMike
      HikeMike about 10 years
      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.
    • HikeMike
      HikeMike about 10 years
      Does searching the list of applications started on login help? System Preferences » Users & Groups » (Your Username) » Login Items
    • Josh Bleecher Snyder
      Josh Bleecher Snyder about 10 years
      @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...
    • Be Kind To New Users
      Be Kind To New Users about 5 years
      Worked great. AirServer was stealing my focus. WHY!!!!
  • Josh Bleecher Snyder
    Josh Bleecher Snyder over 9 years
    Interesting. I use an external mouse and keyboard, but hopefully this helps someone else. Thanks.
  • MarkHu
    MarkHu about 9 years
    This script tells me my culprit is Google Drive [/Applications/Google Drive.app]
  • Ed Randall
    Ed Randall about 6 years
    In my case it is SecurityAgent [/System/Library/Frameworks/Security.framework/Versions/A/Ma‌​chServices/SecurityA‌​gent.bundle]
  • jamesbev
    jamesbev over 5 years
    Offending app was Microsoft Update Assistant. I much appreciate this answer, this problem has been driving me nuts for a while.
  • Gabriel
    Gabriel about 5 years
    My 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.
  • tboyce12
    tboyce12 almost 5 years
    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.
  • Mike
    Mike over 4 years
    @EdRandall Did you find a way to deal with SecurityAgent stealing focus?
  • Ed Randall
    Ed Randall over 4 years
    @Mike yes, complained to our desktop support team .
  • dlamblin
    dlamblin almost 4 years
    So if it's SystemUIServer how do I further identify which menu widget it is (suspecting zScaler)
  • Dae
    Dae about 3 years
    Thank you so much for the script. In my case it was Corel's updater (CUH.app). More on this.
  • Joe Black
    Joe Black over 2 years
    Thanks, it was CUH [/Library/Preferences/com.corel.CUH/CUH.app] in my case
  • rottweiler
    rottweiler about 2 years
    Amazing script. in my case it was /Applications/GlobalProtect.app (VPN client).