2016-01-12 30 views
1

我有一個簡單的shell/python腳本來打開其他窗口。當腳本完成時,我想將腳本運行的終端放到前臺。如何將進程窗口帶到OS X的前臺?

我知道我的父窗口的進程ID。 如何將特定窗口帶到前臺?我想我必須從PID中找出窗口名稱。

+0

我不認爲你的窗口較少過程中有什麼做Terminal.app的主窗口。也許你可以找到一種讓終端專注的方式,獨立於你的命令行程序。 –

+0

我確定桌面可可中有這樣一個API;只是不記得它。 –

+1

@NicolasMiari我的無窗口進程是終端應用程序的子進程。 – mikemaccana

回答

1

不知道是否有一個正確的方式,但是這對我的作品:

osascript<<EOF 
tell application "System Events" 
    set processList to every process whose unix id is 350 
    repeat with proc in processList 
     set the frontmost of proc to true 
    end repeat 
end tell 
EOF 

你可以用osacript -e '...'也做到這一點。

顯然改變350你想要的PID。

3

感謝馬克爲他真棒答案! 擴展上一點點:

# Look up the parent of the given PID. 
# From http://stackoverflow.com/questions/3586888/how-do-i-find-the-top-level-parent-pid-of-a-given-process-using-bash 
function get-top-parent-pid() { 
    PID=${1:-$$} 
    PARENT=$(ps -p $PID -o ppid=) 

    # /sbin/init always has a PID of 1, so if you reach that, the current PID is 
    # the top-level parent. Otherwise, keep looking. 
    if [[ ${PARENT} -eq 1 ]] ; then 
     echo ${PID} 
    else 
     get-top-parent-pid ${PARENT} 
    fi 
} 

function bring-window-to-top() { 
    osascript<<EOF 
    tell application "System Events" 
     set processList to every process whose unix id is ${1} 
     repeat with proc in processList 
      set the frontmost of proc to true 
     end repeat 
    end tell 
EOF 
} 

然後,您可以運行:

bring-window-to-top $(get-top-parent-pid) 

使用快速測試:

sleep 5; bring-window-to-top $(get-top-parent-pid) 

而且交換到別的東西。 5秒後,運行腳本的終端將被髮送到頂端。

+1

幹得好 - 感謝您與社區分享您的努力:-) –