Search code examples
macosterminalapplescriptiterm

OSX: How can I programatically tell if I'm running in Terminal or iTerm?


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?


Solution

  • 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