我有一个简单的 shell/python 脚本,可以打开其他窗口。我想在脚本完成后将运行脚本的终端带到前台。
我知道我的父窗口的进程 ID。如何将给定的窗口置于前台?我想我必须一路从PID中找出窗口名称。
我有一个简单的 shell/python 脚本,可以打开其他窗口。我想在脚本完成后将运行脚本的终端带到前台。
我知道我的父窗口的进程 ID。如何将给定的窗口置于前台?我想我必须一路从PID中找出窗口名称。
不确定是否有正确的方法,但这对我有用:
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。
感谢马克的精彩回答!稍微扩展一下:
# 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 秒后,运行脚本的终端将被发送到顶部。