mac 经常无故失去焦点
后来发现其实是软件的问题,比如开机启动一直后台运行的ishot(截屏软件),会获得焦点。
推荐代码:使用Anaconda和pycharm,虚拟环境中安装依赖pip install --upgrade --force-reinstall PyObjC PyObjC-core
,并运行如下脚本:
#!/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)
参考网址:https://discussionschinese.apple.com/thread/254974399?sortBy=best