bnrwnjyw
V2EX  ›  macOS

macOS APP 窗口时常失去焦点

  •  
  •   bnrwnjyw · Jan 11, 2021 · 2943 views
    This topic created in 1972 days ago, the information mentioned may be changed or developed.

    如题。

    没做什么呢当前窗口就失焦了,比如打字打一半发现文字不上屏了,才发现左上角三个圆点灰掉了,不得不鼠标点一点重新聚焦,非常恼火。在多个 app 上都复现了这个问题,感觉是后台某个 app 没道理的抢占了“主动权”。

    有什么办法知道焦点跑哪去了吗?例如写个 applescript 监听窗口焦点变化?没写过不知道可不可行。

    4 replies    2025-02-26 14:17:09 +08:00
    starrystarry
        1
    starrystarry  
       Jan 11, 2021   ❤️ 1
    #!/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)
    starrystarry
        2
    starrystarry  
       Jan 11, 2021
    bnrwnjyw
        3
    bnrwnjyw  
    OP
       Jan 11, 2021 via iPhone
    @starrystarry 绝了,感谢,一会就试试
    Wxh16144
        4
    Wxh16144  
       Feb 26, 2025
    遇到了同样的问题,这段代码有效,我在 https://v2ex.com/t/976769?p=1#reply2 回复了我具体操作
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   2843 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 31ms · UTC 04:39 · PVG 12:39 · LAX 21:39 · JFK 00:39
    ♥ Do have faith in what you're doing.