简体   繁体   中英

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

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,

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}")
cnt=$(w -h | grep "^$(whoami) *s[^ ]* *-"|wc -l)
echo Your current terminal sessions: $cnt

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM