2014-05-14 47 views
0

我希望我的控制台在终端没有看到任务完成时发出嘟嘟声。如何获得OSX中的活动(聚焦)窗口?

其实,我这一切在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,只需在焦点窗口切换时更改。

回答

1

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

来源: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} 
+1

谢谢,我没有考虑到的AppleScript作为一个选项,这应该工作不够好。 – ABentSpoon