【问题标题】:How can I get the active (focused) window in OSX?如何在 OSX 中获得活动(聚焦)窗口?
【发布时间】:2014-05-14 16:03:24
【问题描述】:

我希望我的控制台在我不在看的终端中完成任务时发出哔哔声。

我实际上已经在 Linux 中解决了所有问题:

function beeper_preexec {
  focus_window=`xdotool getwindowfocus`
} 

function beeper_precmd {
  retval=$?

  if [[ $focus_window -ne `xdotool getwindowfocus` ]]; then
    if [[ $retval -ne 0 ]]; then
      beep -f 329.6
    else
      beep
    fi
  fi
} 

function beeper_setup {
  add-zsh-hook precmd beeper_precmd
  add-zsh-hook preexec beeper_preexec
}

有谁知道我可以在 OS X 上用什么来替换 xdotool getwindowfocus 吗?我并不特别关心它是否返回 PID 或窗口 id,它只需要在焦点窗口切换时更改。

【问题讨论】:

    标签: macos command-line zsh


    【解决方案1】:

    我不确定我将如何处理这个问题,但我做了少量研究,似乎您可以使用 AppleScript 获取当前窗口标题,然后将其与终端的预期窗口标题进行比较。

    发件人:MacOSX: get foremost window title

    global frontApp, frontAppName, windowTitle
    
    set windowTitle to ""
    tell application "System Events"
        set frontApp to first application process whose frontmost is true
        set frontAppName to name of frontApp
        tell process frontAppName
            tell (1st window whose value of attribute "AXMain" is true)
                set windowTitle to value of attribute "AXTitle"
            end tell
        end tell
    end tell
    
    return {frontAppName, windowTitle}
    

    【讨论】:

    • 谢谢,我没有考虑将 applescript 作为一个选项,这应该可以很好地工作。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-23
    • 1970-01-01
    • 2010-10-01
    • 1970-01-01
    相关资源
    最近更新 更多