2016-01-12 32 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

干得好 - 感谢您与社区分享您的努力:-) –