How can I tell (in script) how many Terminals are open in mac os x?

痴心易碎 提交于 2019-12-12 11:17:53

问题


how can I tell how many Terminal windows (in mac os x) are currently opened? this needs to be done from a shell script.

thanks,


回答1:


This script does what you ask for, you use osascript to run it from the cmd line.

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

Edit by Bavarious: In order to use Adam’s AppleScript inside a shell script, you can do the following:

#!/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}")



回答2:


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


来源:https://stackoverflow.com/questions/6172663/how-can-i-tell-in-script-how-many-terminals-are-open-in-mac-os-x

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!