如何返回最前面/凸起/聚焦终端窗口的tty?

时间:2017-08-18 22:32:34

标签: applescript

如何获得最前面(或凸起或聚焦)终端窗口的tty? (这是必需的,以便我可以确定脚本是在前台还是后台运行。)

1 个答案:

答案 0 :(得分:1)

以下脚本返回最前面的终端窗口的tty,如果所有这些窗口都被最小化/隐藏,则返回空字符串:

#!/usr/bin/env osascript

tell application "Terminal"
  if frontmost is true
    repeat with w in windows
      if (frontmost of w) is true then
        set t to (selected tab of w)
        return (tty of t) as string
      end if
    end repeat
  end if
end tell

用法:

$ frontmost-tty
/dev/ttys001
$ tty
/dev/ttys001
$ sleep 5 ; frontmost-tty # switch Terminals
/dev/ttys000