V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX  ›  cyongfrank  ›  全部回复第 1 页 / 共 1 页
回复总数  4
客服说今天 35 ,不降价了。。。。
364 天前
回复了 dalang 创建的主题 MacBook Pro mac 使用时窗口经常无故自己切换
> [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.)
2023-05-09 21:41:00 +08:00
回复了 cyongfrank 创建的主题 Apple 新 Apple Watch Pride 表带 [可能]
关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   1063 人在线   最高记录 6543   ·     Select Language
创意工作者们的社区
World is powered by solitude
VERSION: 3.9.8.5 · 16ms · UTC 22:15 · PVG 06:15 · LAX 15:15 · JFK 18:15
Developed with CodeLauncher
♥ Do have faith in what you're doing.