2014-05-14 52 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