Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

terminal

macos

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,

like image 249
eleKtronaj Avatar asked May 30 '11 05:05

eleKtronaj


People also ask

How do I open multiple terminals in OS X?

Press Command-N. Choose Shell > New Window > New Window with Profile. The name of the profile that opens is concatenated to the end of the New Window with Profile menu item.

What Terminal does OS X use?

The Mac Terminal is a command line interface (CLI) for the macOS operating system (OS). Mac Terminal is typically used by network administrators and advanced technical users who want to initiate an action that is not supported by the operating system's graphical user interface (GUI).


1 Answers

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}")
like image 129
Adam Bergmark Avatar answered Sep 25 '22 00:09

Adam Bergmark