如何判断(在脚本中)在mac os x中打开了多少个终端?

时间:2011-05-30 05:44:05

标签: macos terminal

如何判断当前打开了多少个终端窗口(在mac os x中)? 这需要从shell脚本完成。

感谢,

2 个答案:

答案 0 :(得分:7)

此脚本按照您的要求执行操作,您使用osascript从cmd行运行它。

tell application "Terminal"
    set c to 0
    repeat with i from 1 to (count of windows)
        set c to c + (count of tabs in window i)
    end repeat
    c
end tell

由Bavarious编辑:为了在shell脚本中使用Adam的AppleScript,您可以执行以下操作:

#!/bin/bash
read -d '' OSASCRIPT << EOF
    tell application "Terminal"
        set c to 0
        repeat with i from 1 to (count of windows)
            set c to c + (count of tabs in window i)
        end repeat
        c
end tell
EOF

nwindows=$(osascript -e "${OSASCRIPT}")

答案 1 :(得分:1)

cnt=$(w -h | grep "^$(whoami) *s[^ ]* *-"|wc -l)
echo Your current terminal sessions: $cnt