> [How do I tell which app stole my focus in OS X?](
https://superuser.com/questions/734007/how-do-i-tell-which-app-stole-my-focus-in-os-x)
```python
#!/usr/bin/python
try:
from AppKit import NSWorkspace
except ImportError:
print("Can't import AppKit -- try `pip install PyObjC`")
print("(see instructions for running in a venv with PyObjC)")
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)
```
1. Create a directory, and save the script above into it as "
focus-stealer.py"
2. In a terminal in that directory enter these commands:
```sh
/usr/bin/python3 -m venv venv
./venv/bin/python -m pip install PyObjC
```
(This creates a new, isolated Python virtual environment, just for this script, and installs PyObjC into it. This avoids modifying your system Python installation or needing to use sudo.)
3. Now you can run the script. In a terminal in that same directory run:
```sh
./venv/bin/python ./
focus-stealer.py```
(In the future, you can skip directly to this step—no need to reinstall things.)