1

I have a command line application running on OSX that would like to create several tabs in the current window using AppleScript.

How can I tell if my program is running in Terminal, iTerm, or another terminal program?

Mark Harrison
  • 297,451
  • 125
  • 333
  • 465

2 Answers2

3

The $TERM_PROGRAM env var is set to iTerm.app or Apple_Terminal so you can determine which AppleScript cmds to run if you pass it as an arg to osascript (assuming your shelling to osascript)

Example usage:

osascript ThisScriptName.scpt $TERM_PROGRAM

Example script:

--osascript ThisScriptName.scpt $TERM_PROGRAM
on run {TermType}
    if (TermType = "iTerm.app") then
        -- iTerm.app :-P
        tell application "iTerm"
            tell current window
                tell current session
                    set newSession to (split horizontally with default profile)
                end tell
            end tell
        end tell
    else if (TermType = "Apple_Terminal") then
        -- Terminal.app
        tell application "Terminal"
            do script "open 'https://iterm2.com/downloads.html'" in window 1
        end tell
    else
        -- Unknown terminal application
        return "Really? Not using iTerm.app or Terminal.app"
    end if
end run
SushiHangover
  • 73,120
  • 10
  • 106
  • 165
1
osascript -e 'tell application "Finder" to get the name of every process whose visible is true'

This will a list of running applications, assuming your only running one of terminal, iTerm, ..., this will work

Do what you want with the list

Hope this helps